##// END OF EJS Templates
Merge pull request #13336 from meeseeksmachine/auto-backport-of-pr-13334-on-7.x...
Matthias Bussonnier -
r27170:696d7c56 merge
parent child Browse files
Show More
@@ -1,121 +1,121 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)
12 ensure_dir_exists, fs_encoding)
13 from IPython.utils import py3compat
13 from IPython.utils import py3compat
14
14
15 def get_ipython_dir() -> str:
15 def get_ipython_dir() -> str:
16 """Get the IPython directory for this platform and user.
16 """Get the IPython directory for this platform and user.
17
17
18 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
19 and then adds .ipython to the end of the path.
19 and then adds .ipython to the end of the path.
20 """
20 """
21
21
22 env = os.environ
22 env = os.environ
23 pjoin = os.path.join
23 pjoin = os.path.join
24
24
25
25
26 ipdir_def = '.ipython'
26 ipdir_def = '.ipython'
27
27
28 home_dir = get_home_dir()
28 home_dir = get_home_dir()
29 xdg_dir = get_xdg_dir()
29 xdg_dir = get_xdg_dir()
30
30
31 if 'IPYTHON_DIR' in env:
31 if 'IPYTHON_DIR' in env:
32 warn('The environment variable IPYTHON_DIR is deprecated since IPython 3.0. '
32 warn('The environment variable IPYTHON_DIR is deprecated since IPython 3.0. '
33 'Please use IPYTHONDIR instead.', DeprecationWarning)
33 'Please use IPYTHONDIR instead.', DeprecationWarning)
34 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
34 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
35 if ipdir is None:
35 if ipdir is None:
36 # not set explicitly, use ~/.ipython
36 # not set explicitly, use ~/.ipython
37 ipdir = pjoin(home_dir, ipdir_def)
37 ipdir = pjoin(home_dir, ipdir_def)
38 if xdg_dir:
38 if xdg_dir:
39 # Several IPython versions (up to 1.x) defaulted to .config/ipython
39 # Several IPython versions (up to 1.x) defaulted to .config/ipython
40 # on Linux. We have decided to go back to using .ipython everywhere
40 # on Linux. We have decided to go back to using .ipython everywhere
41 xdg_ipdir = pjoin(xdg_dir, 'ipython')
41 xdg_ipdir = pjoin(xdg_dir, 'ipython')
42
42
43 if _writable_dir(xdg_ipdir):
43 if _writable_dir(xdg_ipdir):
44 cu = compress_user
44 cu = compress_user
45 if os.path.exists(ipdir):
45 if os.path.exists(ipdir):
46 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
46 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
47 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
47 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
48 elif os.path.islink(xdg_ipdir):
48 elif os.path.islink(xdg_ipdir):
49 warn(('{0} is deprecated. Move link to {1} to '
49 warn(('{0} is deprecated. Move link to {1} to '
50 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
50 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
51 else:
51 else:
52 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
52 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
53 shutil.move(xdg_ipdir, ipdir)
53 shutil.move(xdg_ipdir, ipdir)
54
54
55 ipdir = os.path.normpath(os.path.expanduser(ipdir))
55 ipdir = os.path.normpath(os.path.expanduser(ipdir))
56
56
57 if os.path.exists(ipdir) and not _writable_dir(ipdir):
57 if os.path.exists(ipdir) and not _writable_dir(ipdir):
58 # ipdir exists, but is not writable
58 # ipdir exists, but is not writable
59 warn("IPython dir '{0}' is not a writable location,"
59 warn("IPython dir '{0}' is not a writable location,"
60 " using a temp directory.".format(ipdir))
60 " using a temp directory.".format(ipdir))
61 ipdir = tempfile.mkdtemp()
61 ipdir = tempfile.mkdtemp()
62 elif not os.path.exists(ipdir):
62 elif not os.path.exists(ipdir):
63 parent = os.path.dirname(ipdir)
63 parent = os.path.dirname(ipdir)
64 if not _writable_dir(parent):
64 if not _writable_dir(parent):
65 # ipdir does not exist and parent isn't writable
65 # ipdir does not exist and parent isn't writable
66 warn("IPython parent '{0}' is not a writable location,"
66 warn("IPython parent '{0}' is not a writable location,"
67 " using a temp directory.".format(parent))
67 " using a temp directory.".format(parent))
68 ipdir = tempfile.mkdtemp()
68 ipdir = tempfile.mkdtemp()
69 else:
69 else:
70 os.makedirs(ipdir)
70 os.makedirs(ipdir, exist_ok=True)
71 assert isinstance(ipdir, str), "all path manipulation should be str(unicode), but are not."
71 assert isinstance(ipdir, str), "all path manipulation should be str(unicode), but are not."
72 return ipdir
72 return ipdir
73
73
74
74
75 def get_ipython_cache_dir() -> str:
75 def get_ipython_cache_dir() -> str:
76 """Get the cache directory it is created if it does not exist."""
76 """Get the cache directory it is created if it does not exist."""
77 xdgdir = get_xdg_cache_dir()
77 xdgdir = get_xdg_cache_dir()
78 if xdgdir is None:
78 if xdgdir is None:
79 return get_ipython_dir()
79 return get_ipython_dir()
80 ipdir = os.path.join(xdgdir, "ipython")
80 ipdir = os.path.join(xdgdir, "ipython")
81 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
81 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
82 ensure_dir_exists(ipdir)
82 ensure_dir_exists(ipdir)
83 elif not _writable_dir(xdgdir):
83 elif not _writable_dir(xdgdir):
84 return get_ipython_dir()
84 return get_ipython_dir()
85
85
86 return ipdir
86 return ipdir
87
87
88
88
89 def get_ipython_package_dir() -> str:
89 def get_ipython_package_dir() -> str:
90 """Get the base directory where IPython itself is installed."""
90 """Get the base directory where IPython itself is installed."""
91 ipdir = os.path.dirname(IPython.__file__)
91 ipdir = os.path.dirname(IPython.__file__)
92 assert isinstance(ipdir, str)
92 assert isinstance(ipdir, str)
93 return ipdir
93 return ipdir
94
94
95
95
96 def get_ipython_module_path(module_str):
96 def get_ipython_module_path(module_str):
97 """Find the path to an IPython module in this version of IPython.
97 """Find the path to an IPython module in this version of IPython.
98
98
99 This will always find the version of the module that is in this importable
99 This will always find the version of the module that is in this importable
100 IPython package. This will always return the path to the ``.py``
100 IPython package. This will always return the path to the ``.py``
101 version of the module.
101 version of the module.
102 """
102 """
103 if module_str == 'IPython':
103 if module_str == 'IPython':
104 return os.path.join(get_ipython_package_dir(), '__init__.py')
104 return os.path.join(get_ipython_package_dir(), '__init__.py')
105 mod = import_item(module_str)
105 mod = import_item(module_str)
106 the_path = mod.__file__.replace('.pyc', '.py')
106 the_path = mod.__file__.replace('.pyc', '.py')
107 the_path = the_path.replace('.pyo', '.py')
107 the_path = the_path.replace('.pyo', '.py')
108 return py3compat.cast_unicode(the_path, fs_encoding)
108 return py3compat.cast_unicode(the_path, fs_encoding)
109
109
110 def locate_profile(profile='default'):
110 def locate_profile(profile='default'):
111 """Find the path to the folder associated with a given profile.
111 """Find the path to the folder associated with a given profile.
112
112
113 I.e. find $IPYTHONDIR/profile_whatever.
113 I.e. find $IPYTHONDIR/profile_whatever.
114 """
114 """
115 from IPython.core.profiledir import ProfileDir, ProfileDirError
115 from IPython.core.profiledir import ProfileDir, ProfileDirError
116 try:
116 try:
117 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
117 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
118 except ProfileDirError:
118 except ProfileDirError:
119 # IOError makes more sense when people are expecting a path
119 # IOError makes more sense when people are expecting a path
120 raise IOError("Couldn't find profile %r" % profile)
120 raise IOError("Couldn't find profile %r" % profile)
121 return pd.location
121 return pd.location
General Comments 0
You need to be logged in to leave comments. Login now