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