##// END OF EJS Templates
Merge pull request #12615 from MrMino/pathlib_in_core_magics
Matthias Bussonnier -
r26084:9996a0af merge
parent child Browse files
Show More
@@ -40,6 +40,7 b' from IPython.utils.timing import clock, clock2'
40 from warnings import warn
40 from warnings import warn
41 from logging import error
41 from logging import error
42 from io import StringIO
42 from io import StringIO
43 from pathlib import Path
43
44
44 if sys.version_info > (3,8):
45 if sys.version_info > (3,8):
45 from ast import Module
46 from ast import Module
@@ -362,8 +363,7 b' class ExecutionMagics(Magics):'
362 print('\n*** Profile stats marshalled to file',\
363 print('\n*** Profile stats marshalled to file',\
363 repr(dump_file)+'.',sys_exit)
364 repr(dump_file)+'.',sys_exit)
364 if text_file:
365 if text_file:
365 with open(text_file, 'w') as pfile:
366 Path(text_file).write_text(output)
366 pfile.write(output)
367 print('\n*** Profile printout saved to text file',\
367 print('\n*** Profile printout saved to text file',\
368 repr(text_file)+'.',sys_exit)
368 repr(text_file)+'.',sys_exit)
369
369
@@ -724,7 +724,7 b' class ExecutionMagics(Magics):'
724 sys.argv = [filename] + args # put in the proper filename
724 sys.argv = [filename] + args # put in the proper filename
725
725
726 if 'n' in opts:
726 if 'n' in opts:
727 name = os.path.splitext(os.path.basename(filename))[0]
727 name = Path(filename).stem
728 else:
728 else:
729 name = '__main__'
729 name = '__main__'
730
730
@@ -8,37 +8,38 b''
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 import os
12 import re
11 import re
13 import shlex
12 import shlex
14 import sys
13 import sys
15
14
15 from pathlib import Path
16 from IPython.core.magic import Magics, magics_class, line_magic
16 from IPython.core.magic import Magics, magics_class, line_magic
17
17
18
18
19 def _is_conda_environment():
19 def _is_conda_environment():
20 """Return True if the current Python executable is in a conda env"""
20 """Return True if the current Python executable is in a conda env"""
21 # TODO: does this need to change on windows?
21 # TODO: does this need to change on windows?
22 conda_history = os.path.join(sys.prefix, 'conda-meta', 'history')
22 return Path(sys.prefix, "conda-meta", "history").exists()
23 return os.path.exists(conda_history)
24
23
25
24
26 def _get_conda_executable():
25 def _get_conda_executable():
27 """Find the path to the conda executable"""
26 """Find the path to the conda executable"""
28 # Check if there is a conda executable in the same directory as the Python executable.
27 # Check if there is a conda executable in the same directory as the Python executable.
29 # This is the case within conda's root environment.
28 # This is the case within conda's root environment.
30 conda = os.path.join(os.path.dirname(sys.executable), 'conda')
29 conda = Path(sys.executable).parent / "conda"
31 if os.path.isfile(conda):
30 if conda.isfile():
32 return conda
31 return str(conda)
33
32
34 # Otherwise, attempt to extract the executable from conda history.
33 # Otherwise, attempt to extract the executable from conda history.
35 # This applies in any conda environment.
34 # This applies in any conda environment.
36 R = re.compile(r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]")
35 history = Path(sys.prefix, "conda-meta", "history").read_text()
37 with open(os.path.join(sys.prefix, 'conda-meta', 'history')) as f:
36 match = re.search(
38 for line in f:
37 r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]",
39 match = R.match(line)
38 history,
40 if match:
39 flags=re.MULTILINE,
41 return match.groupdict()['command']
40 )
41 if match:
42 return match.groupdict()["command"]
42
43
43 # Fallback: assume conda is available on the system path.
44 # Fallback: assume conda is available on the system path.
44 return "conda"
45 return "conda"
General Comments 0
You need to be logged in to leave comments. Login now