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