##// END OF EJS Templates
rm imports
Srinivas Reddy Thatiparthy -
Show More
@@ -1,120 +1,119 b''
1 """Find files and directories which IPython uses.
1 """Find files and directories which IPython uses.
2 """
2 """
3 import os.path
3 import os.path
4 import shutil
4 import shutil
5 import tempfile
5 import tempfile
6 from warnings import warn
6 from warnings import warn
7
7
8 import IPython
8 import IPython
9 from IPython.utils.importstring import import_item
9 from IPython.utils.importstring import import_item
10 from IPython.utils.path import (
10 from IPython.utils.path import (
11 get_home_dir, get_xdg_dir, get_xdg_cache_dir, compress_user, _writable_dir,
11 get_home_dir, get_xdg_dir, get_xdg_cache_dir, compress_user, _writable_dir,
12 ensure_dir_exists, fs_encoding, filefind
12 ensure_dir_exists, fs_encoding)
13 )
14 from IPython.utils import py3compat
13 from IPython.utils import py3compat
15
14
16 def get_ipython_dir():
15 def get_ipython_dir():
17 """Get the IPython directory for this platform and user.
16 """Get the IPython directory for this platform and user.
18
17
19 This uses the logic in `get_home_dir` to find the home directory
18 This uses the logic in `get_home_dir` to find the home directory
20 and then adds .ipython to the end of the path.
19 and then adds .ipython to the end of the path.
21 """
20 """
22
21
23 env = os.environ
22 env = os.environ
24 pjoin = os.path.join
23 pjoin = os.path.join
25
24
26
25
27 ipdir_def = '.ipython'
26 ipdir_def = '.ipython'
28
27
29 home_dir = get_home_dir()
28 home_dir = get_home_dir()
30 xdg_dir = get_xdg_dir()
29 xdg_dir = get_xdg_dir()
31
30
32 # import pdb; pdb.set_trace() # dbg
31 # import pdb; pdb.set_trace() # dbg
33 if 'IPYTHON_DIR' in env:
32 if 'IPYTHON_DIR' in env:
34 warn('The environment variable IPYTHON_DIR is deprecated. '
33 warn('The environment variable IPYTHON_DIR is deprecated. '
35 'Please use IPYTHONDIR instead.')
34 'Please use IPYTHONDIR instead.')
36 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
35 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
37 if ipdir is None:
36 if ipdir is None:
38 # not set explicitly, use ~/.ipython
37 # not set explicitly, use ~/.ipython
39 ipdir = pjoin(home_dir, ipdir_def)
38 ipdir = pjoin(home_dir, ipdir_def)
40 if xdg_dir:
39 if xdg_dir:
41 # Several IPython versions (up to 1.x) defaulted to .config/ipython
40 # Several IPython versions (up to 1.x) defaulted to .config/ipython
42 # on Linux. We have decided to go back to using .ipython everywhere
41 # on Linux. We have decided to go back to using .ipython everywhere
43 xdg_ipdir = pjoin(xdg_dir, 'ipython')
42 xdg_ipdir = pjoin(xdg_dir, 'ipython')
44
43
45 if _writable_dir(xdg_ipdir):
44 if _writable_dir(xdg_ipdir):
46 cu = compress_user
45 cu = compress_user
47 if os.path.exists(ipdir):
46 if os.path.exists(ipdir):
48 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
47 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
49 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
48 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
50 elif os.path.islink(xdg_ipdir):
49 elif os.path.islink(xdg_ipdir):
51 warn(('{0} is deprecated. Move link to {1} to '
50 warn(('{0} is deprecated. Move link to {1} to '
52 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
51 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
53 else:
52 else:
54 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
53 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
55 shutil.move(xdg_ipdir, ipdir)
54 shutil.move(xdg_ipdir, ipdir)
56
55
57 ipdir = os.path.normpath(os.path.expanduser(ipdir))
56 ipdir = os.path.normpath(os.path.expanduser(ipdir))
58
57
59 if os.path.exists(ipdir) and not _writable_dir(ipdir):
58 if os.path.exists(ipdir) and not _writable_dir(ipdir):
60 # ipdir exists, but is not writable
59 # ipdir exists, but is not writable
61 warn("IPython dir '{0}' is not a writable location,"
60 warn("IPython dir '{0}' is not a writable location,"
62 " using a temp directory.".format(ipdir))
61 " using a temp directory.".format(ipdir))
63 ipdir = tempfile.mkdtemp()
62 ipdir = tempfile.mkdtemp()
64 elif not os.path.exists(ipdir):
63 elif not os.path.exists(ipdir):
65 parent = os.path.dirname(ipdir)
64 parent = os.path.dirname(ipdir)
66 if not _writable_dir(parent):
65 if not _writable_dir(parent):
67 # ipdir does not exist and parent isn't writable
66 # ipdir does not exist and parent isn't writable
68 warn("IPython parent '{0}' is not a writable location,"
67 warn("IPython parent '{0}' is not a writable location,"
69 " using a temp directory.".format(parent))
68 " using a temp directory.".format(parent))
70 ipdir = tempfile.mkdtemp()
69 ipdir = tempfile.mkdtemp()
71
70
72 return py3compat.cast_unicode(ipdir, fs_encoding)
71 return py3compat.cast_unicode(ipdir, fs_encoding)
73
72
74
73
75 def get_ipython_cache_dir():
74 def get_ipython_cache_dir():
76 """Get the cache directory it is created if it does not exist."""
75 """Get the cache directory it is created if it does not exist."""
77 xdgdir = get_xdg_cache_dir()
76 xdgdir = get_xdg_cache_dir()
78 if xdgdir is None:
77 if xdgdir is None:
79 return get_ipython_dir()
78 return get_ipython_dir()
80 ipdir = os.path.join(xdgdir, "ipython")
79 ipdir = os.path.join(xdgdir, "ipython")
81 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
80 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
82 ensure_dir_exists(ipdir)
81 ensure_dir_exists(ipdir)
83 elif not _writable_dir(xdgdir):
82 elif not _writable_dir(xdgdir):
84 return get_ipython_dir()
83 return get_ipython_dir()
85
84
86 return py3compat.cast_unicode(ipdir, fs_encoding)
85 return py3compat.cast_unicode(ipdir, fs_encoding)
87
86
88
87
89 def get_ipython_package_dir():
88 def get_ipython_package_dir():
90 """Get the base directory where IPython itself is installed."""
89 """Get the base directory where IPython itself is installed."""
91 ipdir = os.path.dirname(IPython.__file__)
90 ipdir = os.path.dirname(IPython.__file__)
92 return py3compat.cast_unicode(ipdir, fs_encoding)
91 return py3compat.cast_unicode(ipdir, fs_encoding)
93
92
94
93
95 def get_ipython_module_path(module_str):
94 def get_ipython_module_path(module_str):
96 """Find the path to an IPython module in this version of IPython.
95 """Find the path to an IPython module in this version of IPython.
97
96
98 This will always find the version of the module that is in this importable
97 This will always find the version of the module that is in this importable
99 IPython package. This will always return the path to the ``.py``
98 IPython package. This will always return the path to the ``.py``
100 version of the module.
99 version of the module.
101 """
100 """
102 if module_str == 'IPython':
101 if module_str == 'IPython':
103 return os.path.join(get_ipython_package_dir(), '__init__.py')
102 return os.path.join(get_ipython_package_dir(), '__init__.py')
104 mod = import_item(module_str)
103 mod = import_item(module_str)
105 the_path = mod.__file__.replace('.pyc', '.py')
104 the_path = mod.__file__.replace('.pyc', '.py')
106 the_path = the_path.replace('.pyo', '.py')
105 the_path = the_path.replace('.pyo', '.py')
107 return py3compat.cast_unicode(the_path, fs_encoding)
106 return py3compat.cast_unicode(the_path, fs_encoding)
108
107
109 def locate_profile(profile='default'):
108 def locate_profile(profile='default'):
110 """Find the path to the folder associated with a given profile.
109 """Find the path to the folder associated with a given profile.
111
110
112 I.e. find $IPYTHONDIR/profile_whatever.
111 I.e. find $IPYTHONDIR/profile_whatever.
113 """
112 """
114 from IPython.core.profiledir import ProfileDir, ProfileDirError
113 from IPython.core.profiledir import ProfileDir, ProfileDirError
115 try:
114 try:
116 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
115 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
117 except ProfileDirError:
116 except ProfileDirError:
118 # IOError makes more sense when people are expecting a path
117 # IOError makes more sense when people are expecting a path
119 raise IOError("Couldn't find profile %r" % profile)
118 raise IOError("Couldn't find profile %r" % profile)
120 return pd.location
119 return pd.location
@@ -1,78 +1,73 b''
1 """cli-specific implementation of process utilities.
1 """cli-specific implementation of process utilities.
2
2
3 cli - Common Language Infrastructure for IronPython. Code
3 cli - Common Language Infrastructure for IronPython. Code
4 can run on any operating system. Check os.name for os-
4 can run on any operating system. Check os.name for os-
5 specific settings.
5 specific settings.
6
6
7 This file is only meant to be imported by process.py, not by end-users.
7 This file is only meant to be imported by process.py, not by end-users.
8
8
9 This file is largely untested. To become a full drop-in process
9 This file is largely untested. To become a full drop-in process
10 interface for IronPython will probably require you to help fill
10 interface for IronPython will probably require you to help fill
11 in the details.
11 in the details.
12 """
12 """
13
13
14 # Import cli libraries:
15 import clr
16 import System
17
14
18 # Import Python libraries:
15 import System
19 import os
16 import os
20
21 # Import IPython libraries:
22 from IPython.utils import py3compat
17 from IPython.utils import py3compat
23 from ._process_common import arg_split
18
24
19
25 def _find_cmd(cmd):
20 def _find_cmd(cmd):
26 """Find the full path to a command using which."""
21 """Find the full path to a command using which."""
27 paths = System.Environment.GetEnvironmentVariable("PATH").Split(os.pathsep)
22 paths = System.Environment.GetEnvironmentVariable("PATH").Split(os.pathsep)
28 for path in paths:
23 for path in paths:
29 filename = os.path.join(path, cmd)
24 filename = os.path.join(path, cmd)
30 if System.IO.File.Exists(filename):
25 if System.IO.File.Exists(filename):
31 return py3compat.bytes_to_str(filename)
26 return py3compat.bytes_to_str(filename)
32 raise OSError("command %r not found" % cmd)
27 raise OSError("command %r not found" % cmd)
33
28
34 def system(cmd):
29 def system(cmd):
35 """
30 """
36 system(cmd) should work in a cli environment on Mac OSX, Linux,
31 system(cmd) should work in a cli environment on Mac OSX, Linux,
37 and Windows
32 and Windows
38 """
33 """
39 psi = System.Diagnostics.ProcessStartInfo(cmd)
34 psi = System.Diagnostics.ProcessStartInfo(cmd)
40 psi.RedirectStandardOutput = True
35 psi.RedirectStandardOutput = True
41 psi.RedirectStandardError = True
36 psi.RedirectStandardError = True
42 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
37 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
43 psi.UseShellExecute = False
38 psi.UseShellExecute = False
44 # Start up process:
39 # Start up process:
45 reg = System.Diagnostics.Process.Start(psi)
40 reg = System.Diagnostics.Process.Start(psi)
46
41
47 def getoutput(cmd):
42 def getoutput(cmd):
48 """
43 """
49 getoutput(cmd) should work in a cli environment on Mac OSX, Linux,
44 getoutput(cmd) should work in a cli environment on Mac OSX, Linux,
50 and Windows
45 and Windows
51 """
46 """
52 psi = System.Diagnostics.ProcessStartInfo(cmd)
47 psi = System.Diagnostics.ProcessStartInfo(cmd)
53 psi.RedirectStandardOutput = True
48 psi.RedirectStandardOutput = True
54 psi.RedirectStandardError = True
49 psi.RedirectStandardError = True
55 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
50 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
56 psi.UseShellExecute = False
51 psi.UseShellExecute = False
57 # Start up process:
52 # Start up process:
58 reg = System.Diagnostics.Process.Start(psi)
53 reg = System.Diagnostics.Process.Start(psi)
59 myOutput = reg.StandardOutput
54 myOutput = reg.StandardOutput
60 output = myOutput.ReadToEnd()
55 output = myOutput.ReadToEnd()
61 myError = reg.StandardError
56 myError = reg.StandardError
62 error = myError.ReadToEnd()
57 error = myError.ReadToEnd()
63 return output
58 return output
64
59
65 def check_pid(pid):
60 def check_pid(pid):
66 """
61 """
67 Check if a process with the given PID (pid) exists
62 Check if a process with the given PID (pid) exists
68 """
63 """
69 try:
64 try:
70 System.Diagnostics.Process.GetProcessById(pid)
65 System.Diagnostics.Process.GetProcessById(pid)
71 # process with given pid is running
66 # process with given pid is running
72 return True
67 return True
73 except System.InvalidOperationException:
68 except System.InvalidOperationException:
74 # process wasn't started by this object (but is running)
69 # process wasn't started by this object (but is running)
75 return True
70 return True
76 except System.ArgumentException:
71 except System.ArgumentException:
77 # process with given pid isn't running
72 # process with given pid isn't running
78 return False
73 return False
@@ -1,25 +1,21 b''
1 """
1 """
2 This module has been deprecated since IPython 6.0.
2 This module has been deprecated since IPython 6.0.
3
3
4 Wrapper around linecache which decodes files to unicode according to PEP 263.
4 Wrapper around linecache which decodes files to unicode according to PEP 263.
5 """
5 """
6 import functools
6 import functools
7 import linecache
7 import linecache
8 import sys
9 from warnings import warn
8 from warnings import warn
10
9
11 from IPython.utils import py3compat
12 from IPython.utils import openpy
13
14 getline = linecache.getline
10 getline = linecache.getline
15
11
16 # getlines has to be looked up at runtime, because doctests monkeypatch it.
12 # getlines has to be looked up at runtime, because doctests monkeypatch it.
17 @functools.wraps(linecache.getlines)
13 @functools.wraps(linecache.getlines)
18 def getlines(filename, module_globals=None):
14 def getlines(filename, module_globals=None):
19 """
15 """
20 Deprecated since IPython 6.0
16 Deprecated since IPython 6.0
21 """
17 """
22 warn(("`IPython.utils.ulinecache.getlines` is deprecated since"
18 warn(("`IPython.utils.ulinecache.getlines` is deprecated since"
23 " IPython 6.0 and will be removed in future versions."),
19 " IPython 6.0 and will be removed in future versions."),
24 DeprecationWarning, stacklevel=2)
20 DeprecationWarning, stacklevel=2)
25 return linecache.getlines(filename, module_globals=module_globals)
21 return linecache.getlines(filename, module_globals=module_globals)
General Comments 0
You need to be logged in to leave comments. Login now