Show More
@@ -1,200 +1,204 b'' | |||||
1 | import errno |
|
1 | import errno | |
2 | import os |
|
2 | import os | |
3 | import shutil |
|
3 | import shutil | |
4 | import sys |
|
4 | import sys | |
5 | import tempfile |
|
5 | import tempfile | |
6 | import warnings |
|
6 | import warnings | |
7 | from unittest.mock import patch |
|
7 | from unittest.mock import patch | |
8 |
|
8 | |||
9 | import nose.tools as nt |
|
9 | import nose.tools as nt | |
10 | from testpath import modified_env, assert_isdir, assert_isfile |
|
10 | from testpath import modified_env, assert_isdir, assert_isfile | |
11 |
|
11 | |||
12 | from IPython import paths |
|
12 | from IPython import paths | |
13 | from IPython.testing.decorators import skip_win32 |
|
13 | from IPython.testing.decorators import skip_win32 | |
14 | from IPython.utils.tempdir import TemporaryDirectory |
|
14 | from IPython.utils.tempdir import TemporaryDirectory | |
15 |
|
15 | |||
16 | TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp()) |
|
16 | TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp()) | |
17 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") |
|
17 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") | |
18 | XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir") |
|
18 | XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir") | |
19 | XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir") |
|
19 | XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir") | |
20 | IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') |
|
20 | IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') | |
21 |
|
21 | |||
22 | def setup_module(): |
|
22 | def setup_module(): | |
23 | """Setup testenvironment for the module: |
|
23 | """Setup testenvironment for the module: | |
24 |
|
24 | |||
25 | - Adds dummy home dir tree |
|
25 | - Adds dummy home dir tree | |
26 | """ |
|
26 | """ | |
27 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
27 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
28 | # problem because that exception is only defined on Windows... |
|
28 | # problem because that exception is only defined on Windows... | |
29 | os.makedirs(IP_TEST_DIR) |
|
29 | os.makedirs(IP_TEST_DIR) | |
30 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
30 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) | |
31 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) |
|
31 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def teardown_module(): |
|
34 | def teardown_module(): | |
35 | """Teardown testenvironment for the module: |
|
35 | """Teardown testenvironment for the module: | |
36 |
|
36 | |||
37 | - Remove dummy home dir tree |
|
37 | - Remove dummy home dir tree | |
38 | """ |
|
38 | """ | |
39 | # Note: we remove the parent test dir, which is the root of all test |
|
39 | # Note: we remove the parent test dir, which is the root of all test | |
40 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
40 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
41 | # that non-empty directories are all recursively removed. |
|
41 | # that non-empty directories are all recursively removed. | |
42 | shutil.rmtree(TMP_TEST_DIR) |
|
42 | shutil.rmtree(TMP_TEST_DIR) | |
43 |
|
43 | |||
44 | def patch_get_home_dir(dirpath): |
|
44 | def patch_get_home_dir(dirpath): | |
45 | return patch.object(paths, 'get_home_dir', return_value=dirpath) |
|
45 | return patch.object(paths, 'get_home_dir', return_value=dirpath) | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | def test_get_ipython_dir_1(): |
|
48 | def test_get_ipython_dir_1(): | |
49 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
49 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
50 | env_ipdir = os.path.join("someplace", ".ipython") |
|
50 | env_ipdir = os.path.join("someplace", ".ipython") | |
51 | with patch.object(paths, '_writable_dir', return_value=True), \ |
|
51 | with patch.object(paths, '_writable_dir', return_value=True), \ | |
52 | modified_env({'IPYTHONDIR': env_ipdir}): |
|
52 | modified_env({'IPYTHONDIR': env_ipdir}): | |
53 | ipdir = paths.get_ipython_dir() |
|
53 | ipdir = paths.get_ipython_dir() | |
54 |
|
54 | |||
55 | nt.assert_equal(ipdir, env_ipdir) |
|
55 | nt.assert_equal(ipdir, env_ipdir) | |
56 |
|
56 | |||
57 | def test_get_ipython_dir_2(): |
|
57 | def test_get_ipython_dir_2(): | |
58 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
58 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
59 | with patch_get_home_dir('someplace'), \ |
|
59 | with patch_get_home_dir('someplace'), \ | |
60 | patch.object(paths, 'get_xdg_dir', return_value=None), \ |
|
60 | patch.object(paths, 'get_xdg_dir', return_value=None), \ | |
61 | patch.object(paths, '_writable_dir', return_value=True), \ |
|
61 | patch.object(paths, '_writable_dir', return_value=True), \ | |
62 | patch('os.name', "posix"), \ |
|
62 | patch('os.name', "posix"), \ | |
63 | modified_env({'IPYTHON_DIR': None, |
|
63 | modified_env({'IPYTHON_DIR': None, | |
64 | 'IPYTHONDIR': None, |
|
64 | 'IPYTHONDIR': None, | |
65 | 'XDG_CONFIG_HOME': None |
|
65 | 'XDG_CONFIG_HOME': None | |
66 | }): |
|
66 | }): | |
67 | ipdir = paths.get_ipython_dir() |
|
67 | ipdir = paths.get_ipython_dir() | |
68 |
|
68 | |||
69 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) |
|
69 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) | |
70 |
|
70 | |||
71 | def test_get_ipython_dir_3(): |
|
71 | def test_get_ipython_dir_3(): | |
72 | """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" |
|
72 | """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" | |
73 | tmphome = TemporaryDirectory() |
|
73 | tmphome = TemporaryDirectory() | |
74 | try: |
|
74 | try: | |
75 | with patch_get_home_dir(tmphome.name), \ |
|
75 | with patch_get_home_dir(tmphome.name), \ | |
76 | patch('os.name', 'posix'), \ |
|
76 | patch('os.name', 'posix'), \ | |
77 | modified_env({ |
|
77 | modified_env({ | |
78 | 'IPYTHON_DIR': None, |
|
78 | 'IPYTHON_DIR': None, | |
79 | 'IPYTHONDIR': None, |
|
79 | 'IPYTHONDIR': None, | |
80 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
80 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, | |
81 | }), warnings.catch_warnings(record=True) as w: |
|
81 | }), warnings.catch_warnings(record=True) as w: | |
82 | ipdir = paths.get_ipython_dir() |
|
82 | ipdir = paths.get_ipython_dir() | |
83 |
|
83 | |||
84 | nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython")) |
|
84 | nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython")) | |
85 | if sys.platform != 'darwin': |
|
85 | if sys.platform != 'darwin': | |
86 | nt.assert_equal(len(w), 1) |
|
86 | nt.assert_equal(len(w), 1) | |
87 | nt.assert_in('Moving', str(w[0])) |
|
87 | nt.assert_in('Moving', str(w[0])) | |
88 | finally: |
|
88 | finally: | |
89 | tmphome.cleanup() |
|
89 | tmphome.cleanup() | |
90 |
|
90 | |||
91 | def test_get_ipython_dir_4(): |
|
91 | def test_get_ipython_dir_4(): | |
92 | """test_get_ipython_dir_4, warn if XDG and home both exist.""" |
|
92 | """test_get_ipython_dir_4, warn if XDG and home both exist.""" | |
93 | with patch_get_home_dir(HOME_TEST_DIR), \ |
|
93 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
94 | patch('os.name', 'posix'): |
|
94 | patch('os.name', 'posix'): | |
95 | try: |
|
95 | try: | |
96 | os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
96 | os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) | |
97 | except OSError as e: |
|
97 | except OSError as e: | |
98 | if e.errno != errno.EEXIST: |
|
98 | if e.errno != errno.EEXIST: | |
99 | raise |
|
99 | raise | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | with modified_env({ |
|
102 | with modified_env({ | |
103 | 'IPYTHON_DIR': None, |
|
103 | 'IPYTHON_DIR': None, | |
104 | 'IPYTHONDIR': None, |
|
104 | 'IPYTHONDIR': None, | |
105 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
105 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, | |
106 | }), warnings.catch_warnings(record=True) as w: |
|
106 | }), warnings.catch_warnings(record=True) as w: | |
107 | ipdir = paths.get_ipython_dir() |
|
107 | ipdir = paths.get_ipython_dir() | |
108 |
|
108 | |||
109 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) |
|
109 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) | |
110 | if sys.platform != 'darwin': |
|
110 | if sys.platform != 'darwin': | |
111 | nt.assert_equal(len(w), 1) |
|
111 | nt.assert_equal(len(w), 1) | |
112 | nt.assert_in('Ignoring', str(w[0])) |
|
112 | nt.assert_in('Ignoring', str(w[0])) | |
113 |
|
113 | |||
114 | def test_get_ipython_dir_5(): |
|
114 | def test_get_ipython_dir_5(): | |
115 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
115 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" | |
116 | with patch_get_home_dir(HOME_TEST_DIR), \ |
|
116 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
117 | patch('os.name', 'posix'): |
|
117 | patch('os.name', 'posix'): | |
118 | try: |
|
118 | try: | |
119 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
119 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) | |
120 | except OSError as e: |
|
120 | except OSError as e: | |
121 | if e.errno != errno.ENOENT: |
|
121 | if e.errno != errno.ENOENT: | |
122 | raise |
|
122 | raise | |
123 |
|
123 | |||
124 | with modified_env({ |
|
124 | with modified_env({ | |
125 | 'IPYTHON_DIR': None, |
|
125 | 'IPYTHON_DIR': None, | |
126 | 'IPYTHONDIR': None, |
|
126 | 'IPYTHONDIR': None, | |
127 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
127 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, | |
128 | }): |
|
128 | }): | |
129 | ipdir = paths.get_ipython_dir() |
|
129 | ipdir = paths.get_ipython_dir() | |
130 |
|
130 | |||
131 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
131 | nt.assert_equal(ipdir, IP_TEST_DIR) | |
132 |
|
132 | |||
133 | def test_get_ipython_dir_6(): |
|
133 | def test_get_ipython_dir_6(): | |
134 | """test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" |
|
134 | """test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" | |
135 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') |
|
135 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | |
136 | os.mkdir(xdg) |
|
136 | os.mkdir(xdg) | |
137 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) |
|
137 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | |
138 | print(paths._writable_dir) |
|
138 | print(paths._writable_dir) | |
139 | with patch_get_home_dir(HOME_TEST_DIR), \ |
|
139 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
140 | patch.object(paths, 'get_xdg_dir', return_value=xdg), \ |
|
140 | patch.object(paths, 'get_xdg_dir', return_value=xdg), \ | |
141 | patch('os.name', 'posix'), \ |
|
141 | patch('os.name', 'posix'), \ | |
142 | modified_env({ |
|
142 | modified_env({ | |
143 | 'IPYTHON_DIR': None, |
|
143 | 'IPYTHON_DIR': None, | |
144 | 'IPYTHONDIR': None, |
|
144 | 'IPYTHONDIR': None, | |
145 | 'XDG_CONFIG_HOME': None, |
|
145 | 'XDG_CONFIG_HOME': None, | |
146 | }), warnings.catch_warnings(record=True) as w: |
|
146 | }), warnings.catch_warnings(record=True) as w: | |
147 | ipdir = paths.get_ipython_dir() |
|
147 | ipdir = paths.get_ipython_dir() | |
148 |
|
148 | |||
149 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython')) |
|
149 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython')) | |
150 | nt.assert_equal(len(w), 0) |
|
150 | nt.assert_equal(len(w), 0) | |
151 |
|
151 | |||
152 | def test_get_ipython_dir_7(): |
|
152 | def test_get_ipython_dir_7(): | |
153 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" |
|
153 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" | |
154 | home_dir = os.path.normpath(os.path.expanduser('~')) |
|
154 | home_dir = os.path.normpath(os.path.expanduser('~')) | |
155 | with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ |
|
155 | with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ | |
156 | patch.object(paths, '_writable_dir', return_value=True): |
|
156 | patch.object(paths, '_writable_dir', return_value=True): | |
157 | ipdir = paths.get_ipython_dir() |
|
157 | ipdir = paths.get_ipython_dir() | |
158 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) |
|
158 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) | |
159 |
|
159 | |||
160 | @skip_win32 |
|
160 | @skip_win32 | |
161 | def test_get_ipython_dir_8(): |
|
161 | def test_get_ipython_dir_8(): | |
162 | """test_get_ipython_dir_8, test / home directory""" |
|
162 | """test_get_ipython_dir_8, test / home directory""" | |
|
163 | if not os.access("/", os.W_OK): | |||
|
164 | # test only when HOME directory actually writable | |||
|
165 | return | |||
|
166 | ||||
163 | with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ |
|
167 | with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ | |
164 | patch.object(paths, 'get_xdg_dir', return_value=None), \ |
|
168 | patch.object(paths, 'get_xdg_dir', return_value=None), \ | |
165 | modified_env({ |
|
169 | modified_env({ | |
166 | 'IPYTHON_DIR': None, |
|
170 | 'IPYTHON_DIR': None, | |
167 | 'IPYTHONDIR': None, |
|
171 | 'IPYTHONDIR': None, | |
168 | 'HOME': '/', |
|
172 | 'HOME': '/', | |
169 | }): |
|
173 | }): | |
170 | nt.assert_equal(paths.get_ipython_dir(), '/.ipython') |
|
174 | nt.assert_equal(paths.get_ipython_dir(), '/.ipython') | |
171 |
|
175 | |||
172 |
|
176 | |||
173 | def test_get_ipython_cache_dir(): |
|
177 | def test_get_ipython_cache_dir(): | |
174 | with modified_env({'HOME': HOME_TEST_DIR}): |
|
178 | with modified_env({'HOME': HOME_TEST_DIR}): | |
175 | if os.name == 'posix' and sys.platform != 'darwin': |
|
179 | if os.name == 'posix' and sys.platform != 'darwin': | |
176 | # test default |
|
180 | # test default | |
177 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) |
|
181 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | |
178 | with modified_env({'XDG_CACHE_HOME': None}): |
|
182 | with modified_env({'XDG_CACHE_HOME': None}): | |
179 | ipdir = paths.get_ipython_cache_dir() |
|
183 | ipdir = paths.get_ipython_cache_dir() | |
180 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), |
|
184 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), | |
181 | ipdir) |
|
185 | ipdir) | |
182 | assert_isdir(ipdir) |
|
186 | assert_isdir(ipdir) | |
183 |
|
187 | |||
184 | # test env override |
|
188 | # test env override | |
185 | with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): |
|
189 | with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): | |
186 | ipdir = paths.get_ipython_cache_dir() |
|
190 | ipdir = paths.get_ipython_cache_dir() | |
187 | assert_isdir(ipdir) |
|
191 | assert_isdir(ipdir) | |
188 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) |
|
192 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) | |
189 | else: |
|
193 | else: | |
190 | nt.assert_equal(paths.get_ipython_cache_dir(), |
|
194 | nt.assert_equal(paths.get_ipython_cache_dir(), | |
191 | paths.get_ipython_dir()) |
|
195 | paths.get_ipython_dir()) | |
192 |
|
196 | |||
193 | def test_get_ipython_package_dir(): |
|
197 | def test_get_ipython_package_dir(): | |
194 | ipdir = paths.get_ipython_package_dir() |
|
198 | ipdir = paths.get_ipython_package_dir() | |
195 | assert_isdir(ipdir) |
|
199 | assert_isdir(ipdir) | |
196 |
|
200 | |||
197 |
|
201 | |||
198 | def test_get_ipython_module_path(): |
|
202 | def test_get_ipython_module_path(): | |
199 | ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') |
|
203 | ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') | |
200 | assert_isfile(ipapp_path) |
|
204 | assert_isfile(ipapp_path) |
@@ -1,125 +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: | |||
|
75 | os.makedirs(ipdir) | |||
74 | 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." | |
75 | return ipdir |
|
77 | return ipdir | |
76 |
|
78 | |||
77 |
|
79 | |||
78 | def get_ipython_cache_dir() -> str: |
|
80 | def get_ipython_cache_dir() -> str: | |
79 | """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.""" | |
80 | xdgdir = get_xdg_cache_dir() |
|
82 | xdgdir = get_xdg_cache_dir() | |
81 | if xdgdir is None: |
|
83 | if xdgdir is None: | |
82 | return get_ipython_dir() |
|
84 | return get_ipython_dir() | |
83 | ipdir = os.path.join(xdgdir, "ipython") |
|
85 | ipdir = os.path.join(xdgdir, "ipython") | |
84 | if not os.path.exists(ipdir) and _writable_dir(xdgdir): |
|
86 | if not os.path.exists(ipdir) and _writable_dir(xdgdir): | |
85 | ensure_dir_exists(ipdir) |
|
87 | ensure_dir_exists(ipdir) | |
86 | elif not _writable_dir(xdgdir): |
|
88 | elif not _writable_dir(xdgdir): | |
87 | return get_ipython_dir() |
|
89 | return get_ipython_dir() | |
88 |
|
90 | |||
89 | return ipdir |
|
91 | return ipdir | |
90 |
|
92 | |||
91 |
|
93 | |||
92 | def get_ipython_package_dir() -> str: |
|
94 | def get_ipython_package_dir() -> str: | |
93 | """Get the base directory where IPython itself is installed.""" |
|
95 | """Get the base directory where IPython itself is installed.""" | |
94 | ipdir = os.path.dirname(IPython.__file__) |
|
96 | ipdir = os.path.dirname(IPython.__file__) | |
95 | assert isinstance(ipdir, str) |
|
97 | assert isinstance(ipdir, str) | |
96 | return ipdir |
|
98 | return ipdir | |
97 |
|
99 | |||
98 |
|
100 | |||
99 | def get_ipython_module_path(module_str): |
|
101 | def get_ipython_module_path(module_str): | |
100 | """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. | |
101 |
|
103 | |||
102 | 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 | |
103 | IPython package. This will always return the path to the ``.py`` |
|
105 | IPython package. This will always return the path to the ``.py`` | |
104 | version of the module. |
|
106 | version of the module. | |
105 | """ |
|
107 | """ | |
106 | if module_str == 'IPython': |
|
108 | if module_str == 'IPython': | |
107 | return os.path.join(get_ipython_package_dir(), '__init__.py') |
|
109 | return os.path.join(get_ipython_package_dir(), '__init__.py') | |
108 | mod = import_item(module_str) |
|
110 | mod = import_item(module_str) | |
109 | the_path = mod.__file__.replace('.pyc', '.py') |
|
111 | the_path = mod.__file__.replace('.pyc', '.py') | |
110 | the_path = the_path.replace('.pyo', '.py') |
|
112 | the_path = the_path.replace('.pyo', '.py') | |
111 | return the_path |
|
113 | return the_path | |
112 |
|
114 | |||
113 |
|
115 | |||
114 | def locate_profile(profile='default'): |
|
116 | def locate_profile(profile='default'): | |
115 | """Find the path to the folder associated with a given profile. |
|
117 | """Find the path to the folder associated with a given profile. | |
116 |
|
118 | |||
117 | I.e. find $IPYTHONDIR/profile_whatever. |
|
119 | I.e. find $IPYTHONDIR/profile_whatever. | |
118 | """ |
|
120 | """ | |
119 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
121 | from IPython.core.profiledir import ProfileDir, ProfileDirError | |
120 | try: |
|
122 | try: | |
121 | pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) |
|
123 | pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) | |
122 | except ProfileDirError as e: |
|
124 | except ProfileDirError as e: | |
123 | # IOError makes more sense when people are expecting a path |
|
125 | # IOError makes more sense when people are expecting a path | |
124 | raise IOError("Couldn't find profile %r" % profile) from e |
|
126 | raise IOError("Couldn't find profile %r" % profile) from e | |
125 | return pd.location |
|
127 | return pd.location |
General Comments 0
You need to be logged in to leave comments.
Login now