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