Show More
@@ -1,383 +1,393 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for path handling. |
|
3 | Utilities for path handling. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2009 The IPython Development Team |
|
7 | # Copyright (C) 2008-2009 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import os |
|
17 | import os | |
18 | import sys |
|
18 | import sys | |
19 |
|
19 | |||
20 | import IPython |
|
20 | import IPython | |
21 | from IPython.utils.process import system |
|
21 | from IPython.utils.process import system | |
22 | from IPython.utils.importstring import import_item |
|
22 | from IPython.utils.importstring import import_item | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Code |
|
25 | # Code | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | def _get_long_path_name(path): |
|
29 | def _get_long_path_name(path): | |
30 | """Dummy no-op.""" |
|
30 | """Dummy no-op.""" | |
31 | return path |
|
31 | return path | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | if sys.platform == 'win32': |
|
34 | if sys.platform == 'win32': | |
35 | def _get_long_path_name(path): |
|
35 | def _get_long_path_name(path): | |
36 | """Get a long path name (expand ~) on Windows using ctypes. |
|
36 | """Get a long path name (expand ~) on Windows using ctypes. | |
37 |
|
37 | |||
38 | Examples |
|
38 | Examples | |
39 | -------- |
|
39 | -------- | |
40 |
|
40 | |||
41 | >>> get_long_path_name('c:\\docume~1') |
|
41 | >>> get_long_path_name('c:\\docume~1') | |
42 | u'c:\\\\Documents and Settings' |
|
42 | u'c:\\\\Documents and Settings' | |
43 |
|
43 | |||
44 | """ |
|
44 | """ | |
45 | try: |
|
45 | try: | |
46 | import ctypes |
|
46 | import ctypes | |
47 | except ImportError: |
|
47 | except ImportError: | |
48 | raise ImportError('you need to have ctypes installed for this to work') |
|
48 | raise ImportError('you need to have ctypes installed for this to work') | |
49 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
49 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW | |
50 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
50 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, | |
51 | ctypes.c_uint ] |
|
51 | ctypes.c_uint ] | |
52 |
|
52 | |||
53 | buf = ctypes.create_unicode_buffer(260) |
|
53 | buf = ctypes.create_unicode_buffer(260) | |
54 | rv = _GetLongPathName(path, buf, 260) |
|
54 | rv = _GetLongPathName(path, buf, 260) | |
55 | if rv == 0 or rv > 260: |
|
55 | if rv == 0 or rv > 260: | |
56 | return path |
|
56 | return path | |
57 | else: |
|
57 | else: | |
58 | return buf.value |
|
58 | return buf.value | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def get_long_path_name(path): |
|
61 | def get_long_path_name(path): | |
62 | """Expand a path into its long form. |
|
62 | """Expand a path into its long form. | |
63 |
|
63 | |||
64 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
64 | On Windows this expands any ~ in the paths. On other platforms, it is | |
65 | a null operation. |
|
65 | a null operation. | |
66 | """ |
|
66 | """ | |
67 | return _get_long_path_name(path) |
|
67 | return _get_long_path_name(path) | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | def get_py_filename(name): |
|
70 | def get_py_filename(name): | |
71 | """Return a valid python filename in the current directory. |
|
71 | """Return a valid python filename in the current directory. | |
72 |
|
72 | |||
73 | If the given name is not a file, it adds '.py' and searches again. |
|
73 | If the given name is not a file, it adds '.py' and searches again. | |
74 | Raises IOError with an informative message if the file isn't found.""" |
|
74 | Raises IOError with an informative message if the file isn't found.""" | |
75 |
|
75 | |||
76 | name = os.path.expanduser(name) |
|
76 | name = os.path.expanduser(name) | |
77 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
77 | if not os.path.isfile(name) and not name.endswith('.py'): | |
78 | name += '.py' |
|
78 | name += '.py' | |
79 | if os.path.isfile(name): |
|
79 | if os.path.isfile(name): | |
80 | return name |
|
80 | return name | |
81 | else: |
|
81 | else: | |
82 | raise IOError,'File `%s` not found.' % name |
|
82 | raise IOError,'File `%s` not found.' % name | |
83 |
|
83 | |||
84 |
|
84 | |||
85 | def filefind(filename, path_dirs=None): |
|
85 | def filefind(filename, path_dirs=None): | |
86 | """Find a file by looking through a sequence of paths. |
|
86 | """Find a file by looking through a sequence of paths. | |
87 |
|
87 | |||
88 | This iterates through a sequence of paths looking for a file and returns |
|
88 | This iterates through a sequence of paths looking for a file and returns | |
89 | the full, absolute path of the first occurence of the file. If no set of |
|
89 | the full, absolute path of the first occurence of the file. If no set of | |
90 | path dirs is given, the filename is tested as is, after running through |
|
90 | path dirs is given, the filename is tested as is, after running through | |
91 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: |
|
91 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: | |
92 |
|
92 | |||
93 | filefind('myfile.txt') |
|
93 | filefind('myfile.txt') | |
94 |
|
94 | |||
95 | will find the file in the current working dir, but:: |
|
95 | will find the file in the current working dir, but:: | |
96 |
|
96 | |||
97 | filefind('~/myfile.txt') |
|
97 | filefind('~/myfile.txt') | |
98 |
|
98 | |||
99 | Will find the file in the users home directory. This function does not |
|
99 | Will find the file in the users home directory. This function does not | |
100 | automatically try any paths, such as the cwd or the user's home directory. |
|
100 | automatically try any paths, such as the cwd or the user's home directory. | |
101 |
|
101 | |||
102 | Parameters |
|
102 | Parameters | |
103 | ---------- |
|
103 | ---------- | |
104 | filename : str |
|
104 | filename : str | |
105 | The filename to look for. |
|
105 | The filename to look for. | |
106 | path_dirs : str, None or sequence of str |
|
106 | path_dirs : str, None or sequence of str | |
107 | The sequence of paths to look for the file in. If None, the filename |
|
107 | The sequence of paths to look for the file in. If None, the filename | |
108 | need to be absolute or be in the cwd. If a string, the string is |
|
108 | need to be absolute or be in the cwd. If a string, the string is | |
109 | put into a sequence and the searched. If a sequence, walk through |
|
109 | put into a sequence and the searched. If a sequence, walk through | |
110 | each element and join with ``filename``, calling :func:`expandvars` |
|
110 | each element and join with ``filename``, calling :func:`expandvars` | |
111 | and :func:`expanduser` before testing for existence. |
|
111 | and :func:`expanduser` before testing for existence. | |
112 |
|
112 | |||
113 | Returns |
|
113 | Returns | |
114 | ------- |
|
114 | ------- | |
115 | Raises :exc:`IOError` or returns absolute path to file. |
|
115 | Raises :exc:`IOError` or returns absolute path to file. | |
116 | """ |
|
116 | """ | |
117 |
|
117 | |||
118 | # If paths are quoted, abspath gets confused, strip them... |
|
118 | # If paths are quoted, abspath gets confused, strip them... | |
119 | filename = filename.strip('"').strip("'") |
|
119 | filename = filename.strip('"').strip("'") | |
120 | # If the input is an absolute path, just check it exists |
|
120 | # If the input is an absolute path, just check it exists | |
121 | if os.path.isabs(filename) and os.path.isfile(filename): |
|
121 | if os.path.isabs(filename) and os.path.isfile(filename): | |
122 | return filename |
|
122 | return filename | |
123 |
|
123 | |||
124 | if path_dirs is None: |
|
124 | if path_dirs is None: | |
125 | path_dirs = ("",) |
|
125 | path_dirs = ("",) | |
126 | elif isinstance(path_dirs, basestring): |
|
126 | elif isinstance(path_dirs, basestring): | |
127 | path_dirs = (path_dirs,) |
|
127 | path_dirs = (path_dirs,) | |
128 |
|
128 | |||
129 | for path in path_dirs: |
|
129 | for path in path_dirs: | |
130 | if path == '.': path = os.getcwd() |
|
130 | if path == '.': path = os.getcwd() | |
131 | testname = expand_path(os.path.join(path, filename)) |
|
131 | testname = expand_path(os.path.join(path, filename)) | |
132 | if os.path.isfile(testname): |
|
132 | if os.path.isfile(testname): | |
133 | return os.path.abspath(testname) |
|
133 | return os.path.abspath(testname) | |
134 |
|
134 | |||
135 | raise IOError("File %r does not exist in any of the search paths: %r" % |
|
135 | raise IOError("File %r does not exist in any of the search paths: %r" % | |
136 | (filename, path_dirs) ) |
|
136 | (filename, path_dirs) ) | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | class HomeDirError(Exception): |
|
139 | class HomeDirError(Exception): | |
140 | pass |
|
140 | pass | |
141 |
|
141 | |||
142 |
|
142 | |||
143 | def get_home_dir(): |
|
143 | def get_home_dir(): | |
144 | """Return the closest possible equivalent to a 'home' directory. |
|
144 | """Return the closest possible equivalent to a 'home' directory. | |
145 |
|
145 | |||
146 | * On POSIX, we try $HOME. |
|
146 | * On POSIX, we try $HOME. | |
147 | * On Windows we try: |
|
147 | * On Windows we try: | |
148 | - %HOMESHARE% |
|
148 | - %HOMESHARE% | |
149 | - %HOMEDRIVE\%HOMEPATH% |
|
149 | - %HOMEDRIVE\%HOMEPATH% | |
150 | - %USERPROFILE% |
|
150 | - %USERPROFILE% | |
151 | - Registry hack for My Documents |
|
151 | - Registry hack for My Documents | |
152 | - %HOME%: rare, but some people with unix-like setups may have defined it |
|
152 | - %HOME%: rare, but some people with unix-like setups may have defined it | |
153 | * On Dos C:\ |
|
153 | * On Dos C:\ | |
154 |
|
154 | |||
155 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
155 | Currently only Posix and NT are implemented, a HomeDirError exception is | |
156 | raised for all other OSes. |
|
156 | raised for all other OSes. | |
157 | """ |
|
157 | """ | |
158 |
|
158 | |||
159 | isdir = os.path.isdir |
|
159 | isdir = os.path.isdir | |
160 | env = os.environ |
|
160 | env = os.environ | |
161 |
|
161 | |||
162 | # first, check py2exe distribution root directory for _ipython. |
|
162 | # first, check py2exe distribution root directory for _ipython. | |
163 | # This overrides all. Normally does not exist. |
|
163 | # This overrides all. Normally does not exist. | |
164 |
|
164 | |||
165 | if hasattr(sys, "frozen"): #Is frozen by py2exe |
|
165 | if hasattr(sys, "frozen"): #Is frozen by py2exe | |
166 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file |
|
166 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file | |
167 | root, rest = IPython.__file__.lower().split('library.zip') |
|
167 | root, rest = IPython.__file__.lower().split('library.zip') | |
168 | else: |
|
168 | else: | |
169 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
169 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") | |
170 | root=os.path.abspath(root).rstrip('\\') |
|
170 | root=os.path.abspath(root).rstrip('\\') | |
171 | if isdir(os.path.join(root, '_ipython')): |
|
171 | if isdir(os.path.join(root, '_ipython')): | |
172 | os.environ["IPYKITROOT"] = root |
|
172 | os.environ["IPYKITROOT"] = root | |
173 | return root.decode(sys.getfilesystemencoding()) |
|
173 | return root.decode(sys.getfilesystemencoding()) | |
174 |
|
174 | |||
175 | if os.name == 'posix': |
|
175 | if os.name == 'posix': | |
176 | # Linux, Unix, AIX, OS X |
|
176 | # Linux, Unix, AIX, OS X | |
177 | try: |
|
177 | try: | |
178 | homedir = env['HOME'] |
|
178 | homedir = env['HOME'] | |
179 | except KeyError: |
|
179 | except KeyError: | |
180 | raise HomeDirError('Undefined $HOME, IPython cannot proceed.') |
|
180 | # Last-ditch attempt at finding a suitable $HOME, on systems where | |
|
181 | # it may not be defined in the environment but the system shell | |||
|
182 | # still knows it - reported once as: | |||
|
183 | # https://github.com/ipython/ipython/issues/154 | |||
|
184 | from subprocess import Popen, PIPE | |||
|
185 | homedir = Popen('echo $HOME', shell=True, | |||
|
186 | stdout=PIPE).communicate()[0].strip() | |||
|
187 | if homedir: | |||
|
188 | return homedir.decode(sys.getfilesystemencoding()) | |||
|
189 | else: | |||
|
190 | raise HomeDirError('Undefined $HOME, IPython cannot proceed.') | |||
181 | else: |
|
191 | else: | |
182 | return homedir.decode(sys.getfilesystemencoding()) |
|
192 | return homedir.decode(sys.getfilesystemencoding()) | |
183 | elif os.name == 'nt': |
|
193 | elif os.name == 'nt': | |
184 | # Now for win9x, XP, Vista, 7? |
|
194 | # Now for win9x, XP, Vista, 7? | |
185 | # For some strange reason all of these return 'nt' for os.name. |
|
195 | # For some strange reason all of these return 'nt' for os.name. | |
186 | # First look for a network home directory. This will return the UNC |
|
196 | # First look for a network home directory. This will return the UNC | |
187 | # path (\\server\\Users\%username%) not the mapped path (Z:\). This |
|
197 | # path (\\server\\Users\%username%) not the mapped path (Z:\). This | |
188 | # is needed when running IPython on cluster where all paths have to |
|
198 | # is needed when running IPython on cluster where all paths have to | |
189 | # be UNC. |
|
199 | # be UNC. | |
190 | try: |
|
200 | try: | |
191 | homedir = env['HOMESHARE'] |
|
201 | homedir = env['HOMESHARE'] | |
192 | except KeyError: |
|
202 | except KeyError: | |
193 | pass |
|
203 | pass | |
194 | else: |
|
204 | else: | |
195 | if isdir(homedir): |
|
205 | if isdir(homedir): | |
196 | return homedir.decode(sys.getfilesystemencoding()) |
|
206 | return homedir.decode(sys.getfilesystemencoding()) | |
197 |
|
207 | |||
198 | # Now look for a local home directory |
|
208 | # Now look for a local home directory | |
199 | try: |
|
209 | try: | |
200 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
210 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) | |
201 | except KeyError: |
|
211 | except KeyError: | |
202 | pass |
|
212 | pass | |
203 | else: |
|
213 | else: | |
204 | if isdir(homedir): |
|
214 | if isdir(homedir): | |
205 | return homedir.decode(sys.getfilesystemencoding()) |
|
215 | return homedir.decode(sys.getfilesystemencoding()) | |
206 |
|
216 | |||
207 | # Now the users profile directory |
|
217 | # Now the users profile directory | |
208 | try: |
|
218 | try: | |
209 | homedir = os.path.join(env['USERPROFILE']) |
|
219 | homedir = os.path.join(env['USERPROFILE']) | |
210 | except KeyError: |
|
220 | except KeyError: | |
211 | pass |
|
221 | pass | |
212 | else: |
|
222 | else: | |
213 | if isdir(homedir): |
|
223 | if isdir(homedir): | |
214 | return homedir.decode(sys.getfilesystemencoding()) |
|
224 | return homedir.decode(sys.getfilesystemencoding()) | |
215 |
|
225 | |||
216 | # Use the registry to get the 'My Documents' folder. |
|
226 | # Use the registry to get the 'My Documents' folder. | |
217 | try: |
|
227 | try: | |
218 | import _winreg as wreg |
|
228 | import _winreg as wreg | |
219 | key = wreg.OpenKey( |
|
229 | key = wreg.OpenKey( | |
220 | wreg.HKEY_CURRENT_USER, |
|
230 | wreg.HKEY_CURRENT_USER, | |
221 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |
|
231 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" | |
222 | ) |
|
232 | ) | |
223 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
233 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
224 | key.Close() |
|
234 | key.Close() | |
225 | except: |
|
235 | except: | |
226 | pass |
|
236 | pass | |
227 | else: |
|
237 | else: | |
228 | if isdir(homedir): |
|
238 | if isdir(homedir): | |
229 | return homedir.decode(sys.getfilesystemencoding()) |
|
239 | return homedir.decode(sys.getfilesystemencoding()) | |
230 |
|
240 | |||
231 | # A user with a lot of unix tools in win32 may have defined $HOME. |
|
241 | # A user with a lot of unix tools in win32 may have defined $HOME. | |
232 | # Try this as a last ditch option. |
|
242 | # Try this as a last ditch option. | |
233 | try: |
|
243 | try: | |
234 | homedir = env['HOME'] |
|
244 | homedir = env['HOME'] | |
235 | except KeyError: |
|
245 | except KeyError: | |
236 | pass |
|
246 | pass | |
237 | else: |
|
247 | else: | |
238 | if isdir(homedir): |
|
248 | if isdir(homedir): | |
239 | return homedir.decode(sys.getfilesystemencoding()) |
|
249 | return homedir.decode(sys.getfilesystemencoding()) | |
240 |
|
250 | |||
241 | # If all else fails, raise HomeDirError |
|
251 | # If all else fails, raise HomeDirError | |
242 | raise HomeDirError('No valid home directory could be found') |
|
252 | raise HomeDirError('No valid home directory could be found') | |
243 | elif os.name == 'dos': |
|
253 | elif os.name == 'dos': | |
244 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
254 | # Desperate, may do absurd things in classic MacOS. May work under DOS. | |
245 | return 'C:\\'.decode(sys.getfilesystemencoding()) |
|
255 | return 'C:\\'.decode(sys.getfilesystemencoding()) | |
246 | else: |
|
256 | else: | |
247 | raise HomeDirError('No valid home directory could be found for your OS') |
|
257 | raise HomeDirError('No valid home directory could be found for your OS') | |
248 |
|
258 | |||
249 | def get_xdg_dir(): |
|
259 | def get_xdg_dir(): | |
250 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. |
|
260 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. | |
251 |
|
261 | |||
252 | This is only for posix (Linux,Unix,OS X, etc) systems. |
|
262 | This is only for posix (Linux,Unix,OS X, etc) systems. | |
253 | """ |
|
263 | """ | |
254 |
|
264 | |||
255 | isdir = os.path.isdir |
|
265 | isdir = os.path.isdir | |
256 | env = os.environ |
|
266 | env = os.environ | |
257 |
|
267 | |||
258 | if os.name == 'posix': |
|
268 | if os.name == 'posix': | |
259 | # Linux, Unix, AIX, OS X |
|
269 | # Linux, Unix, AIX, OS X | |
260 | # use ~/.config if not set OR empty |
|
270 | # use ~/.config if not set OR empty | |
261 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
271 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') | |
262 | if xdg and isdir(xdg): |
|
272 | if xdg and isdir(xdg): | |
263 | return xdg.decode(sys.getfilesystemencoding()) |
|
273 | return xdg.decode(sys.getfilesystemencoding()) | |
264 |
|
274 | |||
265 | return None |
|
275 | return None | |
266 |
|
276 | |||
267 |
|
277 | |||
268 | def get_ipython_dir(): |
|
278 | def get_ipython_dir(): | |
269 | """Get the IPython directory for this platform and user. |
|
279 | """Get the IPython directory for this platform and user. | |
270 |
|
280 | |||
271 | This uses the logic in `get_home_dir` to find the home directory |
|
281 | This uses the logic in `get_home_dir` to find the home directory | |
272 | and the adds .ipython to the end of the path. |
|
282 | and the adds .ipython to the end of the path. | |
273 | """ |
|
283 | """ | |
274 |
|
284 | |||
275 | env = os.environ |
|
285 | env = os.environ | |
276 | pjoin = os.path.join |
|
286 | pjoin = os.path.join | |
277 | exists = os.path.exists |
|
287 | exists = os.path.exists | |
278 |
|
288 | |||
279 | ipdir_def = '.ipython' |
|
289 | ipdir_def = '.ipython' | |
280 | xdg_def = 'ipython' |
|
290 | xdg_def = 'ipython' | |
281 |
|
291 | |||
282 | home_dir = get_home_dir() |
|
292 | home_dir = get_home_dir() | |
283 | xdg_dir = get_xdg_dir() |
|
293 | xdg_dir = get_xdg_dir() | |
284 | # import pdb; pdb.set_trace() # dbg |
|
294 | # import pdb; pdb.set_trace() # dbg | |
285 | ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None)) |
|
295 | ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None)) | |
286 | if ipdir is None: |
|
296 | if ipdir is None: | |
287 | # not set explicitly, use XDG_CONFIG_HOME or HOME |
|
297 | # not set explicitly, use XDG_CONFIG_HOME or HOME | |
288 | home_ipdir = pjoin(home_dir, ipdir_def) |
|
298 | home_ipdir = pjoin(home_dir, ipdir_def) | |
289 | if xdg_dir: |
|
299 | if xdg_dir: | |
290 | # use XDG, as long as the user isn't already |
|
300 | # use XDG, as long as the user isn't already | |
291 | # using $HOME/.ipython and *not* XDG/ipython |
|
301 | # using $HOME/.ipython and *not* XDG/ipython | |
292 |
|
302 | |||
293 | xdg_ipdir = pjoin(xdg_dir, xdg_def) |
|
303 | xdg_ipdir = pjoin(xdg_dir, xdg_def) | |
294 |
|
304 | |||
295 | if exists(xdg_ipdir) or not exists(home_ipdir): |
|
305 | if exists(xdg_ipdir) or not exists(home_ipdir): | |
296 | ipdir = xdg_ipdir |
|
306 | ipdir = xdg_ipdir | |
297 |
|
307 | |||
298 | if ipdir is None: |
|
308 | if ipdir is None: | |
299 | # not using XDG |
|
309 | # not using XDG | |
300 | ipdir = home_ipdir |
|
310 | ipdir = home_ipdir | |
301 |
|
311 | |||
302 | return ipdir.decode(sys.getfilesystemencoding()) |
|
312 | return ipdir.decode(sys.getfilesystemencoding()) | |
303 |
|
313 | |||
304 |
|
314 | |||
305 | def get_ipython_package_dir(): |
|
315 | def get_ipython_package_dir(): | |
306 | """Get the base directory where IPython itself is installed.""" |
|
316 | """Get the base directory where IPython itself is installed.""" | |
307 | ipdir = os.path.dirname(IPython.__file__) |
|
317 | ipdir = os.path.dirname(IPython.__file__) | |
308 | return ipdir.decode(sys.getfilesystemencoding()) |
|
318 | return ipdir.decode(sys.getfilesystemencoding()) | |
309 |
|
319 | |||
310 |
|
320 | |||
311 | def get_ipython_module_path(module_str): |
|
321 | def get_ipython_module_path(module_str): | |
312 | """Find the path to an IPython module in this version of IPython. |
|
322 | """Find the path to an IPython module in this version of IPython. | |
313 |
|
323 | |||
314 | This will always find the version of the module that is in this importable |
|
324 | This will always find the version of the module that is in this importable | |
315 | IPython package. This will always return the path to the ``.py`` |
|
325 | IPython package. This will always return the path to the ``.py`` | |
316 | version of the module. |
|
326 | version of the module. | |
317 | """ |
|
327 | """ | |
318 | if module_str == 'IPython': |
|
328 | if module_str == 'IPython': | |
319 | return os.path.join(get_ipython_package_dir(), '__init__.py') |
|
329 | return os.path.join(get_ipython_package_dir(), '__init__.py') | |
320 | mod = import_item(module_str) |
|
330 | mod = import_item(module_str) | |
321 | the_path = mod.__file__.replace('.pyc', '.py') |
|
331 | the_path = mod.__file__.replace('.pyc', '.py') | |
322 | the_path = the_path.replace('.pyo', '.py') |
|
332 | the_path = the_path.replace('.pyo', '.py') | |
323 | return the_path.decode(sys.getfilesystemencoding()) |
|
333 | return the_path.decode(sys.getfilesystemencoding()) | |
324 |
|
334 | |||
325 |
|
335 | |||
326 | def expand_path(s): |
|
336 | def expand_path(s): | |
327 | """Expand $VARS and ~names in a string, like a shell |
|
337 | """Expand $VARS and ~names in a string, like a shell | |
328 |
|
338 | |||
329 | :Examples: |
|
339 | :Examples: | |
330 |
|
340 | |||
331 | In [2]: os.environ['FOO']='test' |
|
341 | In [2]: os.environ['FOO']='test' | |
332 |
|
342 | |||
333 | In [3]: expand_path('variable FOO is $FOO') |
|
343 | In [3]: expand_path('variable FOO is $FOO') | |
334 | Out[3]: 'variable FOO is test' |
|
344 | Out[3]: 'variable FOO is test' | |
335 | """ |
|
345 | """ | |
336 | # This is a pretty subtle hack. When expand user is given a UNC path |
|
346 | # This is a pretty subtle hack. When expand user is given a UNC path | |
337 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes |
|
347 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes | |
338 | # the $ to get (\\server\share\%username%). I think it considered $ |
|
348 | # the $ to get (\\server\share\%username%). I think it considered $ | |
339 | # alone an empty var. But, we need the $ to remains there (it indicates |
|
349 | # alone an empty var. But, we need the $ to remains there (it indicates | |
340 | # a hidden share). |
|
350 | # a hidden share). | |
341 | if os.name=='nt': |
|
351 | if os.name=='nt': | |
342 | s = s.replace('$\\', 'IPYTHON_TEMP') |
|
352 | s = s.replace('$\\', 'IPYTHON_TEMP') | |
343 | s = os.path.expandvars(os.path.expanduser(s)) |
|
353 | s = os.path.expandvars(os.path.expanduser(s)) | |
344 | if os.name=='nt': |
|
354 | if os.name=='nt': | |
345 | s = s.replace('IPYTHON_TEMP', '$\\') |
|
355 | s = s.replace('IPYTHON_TEMP', '$\\') | |
346 | return s |
|
356 | return s | |
347 |
|
357 | |||
348 |
|
358 | |||
349 | def target_outdated(target,deps): |
|
359 | def target_outdated(target,deps): | |
350 | """Determine whether a target is out of date. |
|
360 | """Determine whether a target is out of date. | |
351 |
|
361 | |||
352 | target_outdated(target,deps) -> 1/0 |
|
362 | target_outdated(target,deps) -> 1/0 | |
353 |
|
363 | |||
354 | deps: list of filenames which MUST exist. |
|
364 | deps: list of filenames which MUST exist. | |
355 | target: single filename which may or may not exist. |
|
365 | target: single filename which may or may not exist. | |
356 |
|
366 | |||
357 | If target doesn't exist or is older than any file listed in deps, return |
|
367 | If target doesn't exist or is older than any file listed in deps, return | |
358 | true, otherwise return false. |
|
368 | true, otherwise return false. | |
359 | """ |
|
369 | """ | |
360 | try: |
|
370 | try: | |
361 | target_time = os.path.getmtime(target) |
|
371 | target_time = os.path.getmtime(target) | |
362 | except os.error: |
|
372 | except os.error: | |
363 | return 1 |
|
373 | return 1 | |
364 | for dep in deps: |
|
374 | for dep in deps: | |
365 | dep_time = os.path.getmtime(dep) |
|
375 | dep_time = os.path.getmtime(dep) | |
366 | if dep_time > target_time: |
|
376 | if dep_time > target_time: | |
367 | #print "For target",target,"Dep failed:",dep # dbg |
|
377 | #print "For target",target,"Dep failed:",dep # dbg | |
368 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
378 | #print "times (dep,tar):",dep_time,target_time # dbg | |
369 | return 1 |
|
379 | return 1 | |
370 | return 0 |
|
380 | return 0 | |
371 |
|
381 | |||
372 |
|
382 | |||
373 | def target_update(target,deps,cmd): |
|
383 | def target_update(target,deps,cmd): | |
374 | """Update a target with a given command given a list of dependencies. |
|
384 | """Update a target with a given command given a list of dependencies. | |
375 |
|
385 | |||
376 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
386 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
377 |
|
387 | |||
378 | This is just a wrapper around target_outdated() which calls the given |
|
388 | This is just a wrapper around target_outdated() which calls the given | |
379 | command if target is outdated.""" |
|
389 | command if target is outdated.""" | |
380 |
|
390 | |||
381 | if target_outdated(target,deps): |
|
391 | if target_outdated(target,deps): | |
382 | system(cmd) |
|
392 | system(cmd) | |
383 |
|
393 |
@@ -1,358 +1,359 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Tests for IPython.utils.path.py""" |
|
2 | """Tests for IPython.utils.path.py""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2008 The IPython Development Team |
|
5 | # Copyright (C) 2008 The IPython Development Team | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | # Distributed under the terms of the BSD License. The full license is in | |
8 | # the file COPYING, distributed as part of this software. |
|
8 | # the file COPYING, distributed as part of this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | import os |
|
15 | import os | |
16 | import shutil |
|
16 | import shutil | |
17 | import sys |
|
17 | import sys | |
18 | import tempfile |
|
18 | import tempfile | |
19 |
|
19 | |||
20 | from os.path import join, abspath, split |
|
20 | from os.path import join, abspath, split | |
21 |
|
21 | |||
22 | import nose.tools as nt |
|
22 | import nose.tools as nt | |
23 |
|
23 | |||
24 | from nose import with_setup |
|
24 | from nose import with_setup | |
25 |
|
25 | |||
26 | import IPython |
|
26 | import IPython | |
27 | from IPython.testing import decorators as dec |
|
27 | from IPython.testing import decorators as dec | |
28 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
28 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 | |
29 | from IPython.utils import path |
|
29 | from IPython.utils import path | |
30 |
|
30 | |||
31 | # Platform-dependent imports |
|
31 | # Platform-dependent imports | |
32 | try: |
|
32 | try: | |
33 | import _winreg as wreg |
|
33 | import _winreg as wreg | |
34 | except ImportError: |
|
34 | except ImportError: | |
35 | #Fake _winreg module on none windows platforms |
|
35 | #Fake _winreg module on none windows platforms | |
36 | import new |
|
36 | import new | |
37 | sys.modules["_winreg"] = new.module("_winreg") |
|
37 | sys.modules["_winreg"] = new.module("_winreg") | |
38 | import _winreg as wreg |
|
38 | import _winreg as wreg | |
39 | #Add entries that needs to be stubbed by the testing code |
|
39 | #Add entries that needs to be stubbed by the testing code | |
40 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
40 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | |
41 |
|
41 | |||
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 | # Globals |
|
43 | # Globals | |
44 | #----------------------------------------------------------------------------- |
|
44 | #----------------------------------------------------------------------------- | |
45 | env = os.environ |
|
45 | env = os.environ | |
46 | TEST_FILE_PATH = split(abspath(__file__))[0] |
|
46 | TEST_FILE_PATH = split(abspath(__file__))[0] | |
47 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
47 | TMP_TEST_DIR = tempfile.mkdtemp() | |
48 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
48 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") | |
49 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") |
|
49 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") | |
50 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') |
|
50 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') | |
51 | # |
|
51 | # | |
52 | # Setup/teardown functions/decorators |
|
52 | # Setup/teardown functions/decorators | |
53 | # |
|
53 | # | |
54 |
|
54 | |||
55 | def setup(): |
|
55 | def setup(): | |
56 | """Setup testenvironment for the module: |
|
56 | """Setup testenvironment for the module: | |
57 |
|
57 | |||
58 | - Adds dummy home dir tree |
|
58 | - Adds dummy home dir tree | |
59 | """ |
|
59 | """ | |
60 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
60 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
61 | # problem because that exception is only defined on Windows... |
|
61 | # problem because that exception is only defined on Windows... | |
62 | os.makedirs(IP_TEST_DIR) |
|
62 | os.makedirs(IP_TEST_DIR) | |
63 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
63 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def teardown(): |
|
66 | def teardown(): | |
67 | """Teardown testenvironment for the module: |
|
67 | """Teardown testenvironment for the module: | |
68 |
|
68 | |||
69 | - Remove dummy home dir tree |
|
69 | - Remove dummy home dir tree | |
70 | """ |
|
70 | """ | |
71 | # Note: we remove the parent test dir, which is the root of all test |
|
71 | # Note: we remove the parent test dir, which is the root of all test | |
72 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
72 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
73 | # that non-empty directories are all recursively removed. |
|
73 | # that non-empty directories are all recursively removed. | |
74 | shutil.rmtree(TMP_TEST_DIR) |
|
74 | shutil.rmtree(TMP_TEST_DIR) | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | def setup_environment(): |
|
77 | def setup_environment(): | |
78 | """Setup testenvironment for some functions that are tested |
|
78 | """Setup testenvironment for some functions that are tested | |
79 | in this module. In particular this functions stores attributes |
|
79 | in this module. In particular this functions stores attributes | |
80 | and other things that we need to stub in some test functions. |
|
80 | and other things that we need to stub in some test functions. | |
81 | This needs to be done on a function level and not module level because |
|
81 | This needs to be done on a function level and not module level because | |
82 | each testfunction needs a pristine environment. |
|
82 | each testfunction needs a pristine environment. | |
83 | """ |
|
83 | """ | |
84 | global oldstuff, platformstuff |
|
84 | global oldstuff, platformstuff | |
85 | oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__) |
|
85 | oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__) | |
86 |
|
86 | |||
87 | if os.name == 'nt': |
|
87 | if os.name == 'nt': | |
88 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
88 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | def teardown_environment(): |
|
91 | def teardown_environment(): | |
92 | """Restore things that were remebered by the setup_environment function |
|
92 | """Restore things that were remebered by the setup_environment function | |
93 | """ |
|
93 | """ | |
94 | (oldenv, os.name, get_home_dir, IPython.__file__,) = oldstuff |
|
94 | (oldenv, os.name, get_home_dir, IPython.__file__,) = oldstuff | |
95 |
|
95 | |||
96 | for key in env.keys(): |
|
96 | for key in env.keys(): | |
97 | if key not in oldenv: |
|
97 | if key not in oldenv: | |
98 | del env[key] |
|
98 | del env[key] | |
99 | env.update(oldenv) |
|
99 | env.update(oldenv) | |
100 | if hasattr(sys, 'frozen'): |
|
100 | if hasattr(sys, 'frozen'): | |
101 | del sys.frozen |
|
101 | del sys.frozen | |
102 | if os.name == 'nt': |
|
102 | if os.name == 'nt': | |
103 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
103 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | |
104 |
|
104 | |||
105 | # Build decorator that uses the setup_environment/setup_environment |
|
105 | # Build decorator that uses the setup_environment/setup_environment | |
106 | with_environment = with_setup(setup_environment, teardown_environment) |
|
106 | with_environment = with_setup(setup_environment, teardown_environment) | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | @skip_if_not_win32 |
|
109 | @skip_if_not_win32 | |
110 | @with_environment |
|
110 | @with_environment | |
111 | def test_get_home_dir_1(): |
|
111 | def test_get_home_dir_1(): | |
112 | """Testcase for py2exe logic, un-compressed lib |
|
112 | """Testcase for py2exe logic, un-compressed lib | |
113 | """ |
|
113 | """ | |
114 | sys.frozen = True |
|
114 | sys.frozen = True | |
115 |
|
115 | |||
116 | #fake filename for IPython.__init__ |
|
116 | #fake filename for IPython.__init__ | |
117 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
117 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) | |
118 |
|
118 | |||
119 | home_dir = path.get_home_dir() |
|
119 | home_dir = path.get_home_dir() | |
120 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
120 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
121 |
|
121 | |||
122 |
|
122 | |||
123 | @skip_if_not_win32 |
|
123 | @skip_if_not_win32 | |
124 | @with_environment |
|
124 | @with_environment | |
125 | def test_get_home_dir_2(): |
|
125 | def test_get_home_dir_2(): | |
126 | """Testcase for py2exe logic, compressed lib |
|
126 | """Testcase for py2exe logic, compressed lib | |
127 | """ |
|
127 | """ | |
128 | sys.frozen = True |
|
128 | sys.frozen = True | |
129 | #fake filename for IPython.__init__ |
|
129 | #fake filename for IPython.__init__ | |
130 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
130 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() | |
131 |
|
131 | |||
132 | home_dir = path.get_home_dir() |
|
132 | home_dir = path.get_home_dir() | |
133 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) |
|
133 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) | |
134 |
|
134 | |||
135 |
|
135 | |||
136 | @with_environment |
|
136 | @with_environment | |
137 | @skip_win32 |
|
137 | @skip_win32 | |
138 | def test_get_home_dir_3(): |
|
138 | def test_get_home_dir_3(): | |
139 | """Testcase $HOME is set, then use its value as home directory.""" |
|
139 | """Testcase $HOME is set, then use its value as home directory.""" | |
140 | env["HOME"] = HOME_TEST_DIR |
|
140 | env["HOME"] = HOME_TEST_DIR | |
141 | home_dir = path.get_home_dir() |
|
141 | home_dir = path.get_home_dir() | |
142 | nt.assert_equal(home_dir, env["HOME"]) |
|
142 | nt.assert_equal(home_dir, env["HOME"]) | |
143 |
|
143 | |||
144 |
|
144 | |||
145 | @with_environment |
|
145 | @with_environment | |
146 | def test_get_home_dir_4(): |
|
146 | def test_get_home_dir_4(): | |
147 | """Testcase $HOME is not set, os=='posix'. |
|
147 | """Testcase $HOME is not set, os=='posix'. | |
148 | This should fail with HomeDirError""" |
|
148 | This should fail with HomeDirError""" | |
149 |
|
149 | |||
150 | os.name = 'posix' |
|
150 | os.name = 'posix' | |
151 | if 'HOME' in env: del env['HOME'] |
|
151 | if 'HOME' in env: del env['HOME'] | |
152 | nt.assert_raises(path.HomeDirError, path.get_home_dir) |
|
152 | nt.assert_raises(path.HomeDirError, path.get_home_dir) | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | @skip_if_not_win32 |
|
155 | @skip_if_not_win32 | |
156 | @with_environment |
|
156 | @with_environment | |
157 | def test_get_home_dir_5(): |
|
157 | def test_get_home_dir_5(): | |
158 | """Using HOMEDRIVE + HOMEPATH, os=='nt'. |
|
158 | """Using HOMEDRIVE + HOMEPATH, os=='nt'. | |
159 |
|
159 | |||
160 | HOMESHARE is missing. |
|
160 | HOMESHARE is missing. | |
161 | """ |
|
161 | """ | |
162 |
|
162 | |||
163 | os.name = 'nt' |
|
163 | os.name = 'nt' | |
164 | env.pop('HOMESHARE', None) |
|
164 | env.pop('HOMESHARE', None) | |
165 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR) |
|
165 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR) | |
166 | home_dir = path.get_home_dir() |
|
166 | home_dir = path.get_home_dir() | |
167 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
167 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
168 |
|
168 | |||
169 |
|
169 | |||
170 | @skip_if_not_win32 |
|
170 | @skip_if_not_win32 | |
171 | @with_environment |
|
171 | @with_environment | |
172 | def test_get_home_dir_6(): |
|
172 | def test_get_home_dir_6(): | |
173 | """Using USERPROFILE, os=='nt'. |
|
173 | """Using USERPROFILE, os=='nt'. | |
174 |
|
174 | |||
175 | HOMESHARE, HOMEDRIVE, HOMEPATH are missing. |
|
175 | HOMESHARE, HOMEDRIVE, HOMEPATH are missing. | |
176 | """ |
|
176 | """ | |
177 |
|
177 | |||
178 | os.name = 'nt' |
|
178 | os.name = 'nt' | |
179 | env.pop('HOMESHARE', None) |
|
179 | env.pop('HOMESHARE', None) | |
180 | env.pop('HOMEDRIVE', None) |
|
180 | env.pop('HOMEDRIVE', None) | |
181 | env.pop('HOMEPATH', None) |
|
181 | env.pop('HOMEPATH', None) | |
182 | env["USERPROFILE"] = abspath(HOME_TEST_DIR) |
|
182 | env["USERPROFILE"] = abspath(HOME_TEST_DIR) | |
183 | home_dir = path.get_home_dir() |
|
183 | home_dir = path.get_home_dir() | |
184 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
184 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
185 |
|
185 | |||
186 |
|
186 | |||
187 | @skip_if_not_win32 |
|
187 | @skip_if_not_win32 | |
188 | @with_environment |
|
188 | @with_environment | |
189 | def test_get_home_dir_7(): |
|
189 | def test_get_home_dir_7(): | |
190 | """Using HOMESHARE, os=='nt'.""" |
|
190 | """Using HOMESHARE, os=='nt'.""" | |
191 |
|
191 | |||
192 | os.name = 'nt' |
|
192 | os.name = 'nt' | |
193 | env["HOMESHARE"] = abspath(HOME_TEST_DIR) |
|
193 | env["HOMESHARE"] = abspath(HOME_TEST_DIR) | |
194 | home_dir = path.get_home_dir() |
|
194 | home_dir = path.get_home_dir() | |
195 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
195 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
|
196 | ||||
196 |
|
197 | |||
197 | # Should we stub wreg fully so we can run the test on all platforms? |
|
198 | # Should we stub wreg fully so we can run the test on all platforms? | |
198 | @skip_if_not_win32 |
|
199 | @skip_if_not_win32 | |
199 | @with_environment |
|
200 | @with_environment | |
200 | def test_get_home_dir_8(): |
|
201 | def test_get_home_dir_8(): | |
201 | """Using registry hack for 'My Documents', os=='nt' |
|
202 | """Using registry hack for 'My Documents', os=='nt' | |
202 |
|
203 | |||
203 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. |
|
204 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. | |
204 | """ |
|
205 | """ | |
205 | os.name = 'nt' |
|
206 | os.name = 'nt' | |
206 | # Remove from stub environment all keys that may be set |
|
207 | # Remove from stub environment all keys that may be set | |
207 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: |
|
208 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: | |
208 | env.pop(key, None) |
|
209 | env.pop(key, None) | |
209 |
|
210 | |||
210 | #Stub windows registry functions |
|
211 | #Stub windows registry functions | |
211 | def OpenKey(x, y): |
|
212 | def OpenKey(x, y): | |
212 | class key: |
|
213 | class key: | |
213 | def Close(self): |
|
214 | def Close(self): | |
214 | pass |
|
215 | pass | |
215 | return key() |
|
216 | return key() | |
216 | def QueryValueEx(x, y): |
|
217 | def QueryValueEx(x, y): | |
217 | return [abspath(HOME_TEST_DIR)] |
|
218 | return [abspath(HOME_TEST_DIR)] | |
218 |
|
219 | |||
219 | wreg.OpenKey = OpenKey |
|
220 | wreg.OpenKey = OpenKey | |
220 | wreg.QueryValueEx = QueryValueEx |
|
221 | wreg.QueryValueEx = QueryValueEx | |
221 |
|
222 | |||
222 | home_dir = path.get_home_dir() |
|
223 | home_dir = path.get_home_dir() | |
223 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
224 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
224 |
|
225 | |||
225 |
|
226 | |||
226 | @with_environment |
|
227 | @with_environment | |
227 | def test_get_ipython_dir_1(): |
|
228 | def test_get_ipython_dir_1(): | |
228 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
229 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
229 | env['IPYTHON_DIR'] = "someplace/.ipython" |
|
230 | env['IPYTHON_DIR'] = "someplace/.ipython" | |
230 | ipdir = path.get_ipython_dir() |
|
231 | ipdir = path.get_ipython_dir() | |
231 | nt.assert_equal(ipdir, "someplace/.ipython") |
|
232 | nt.assert_equal(ipdir, "someplace/.ipython") | |
232 |
|
233 | |||
233 |
|
234 | |||
234 | @with_environment |
|
235 | @with_environment | |
235 | def test_get_ipython_dir_2(): |
|
236 | def test_get_ipython_dir_2(): | |
236 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
237 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
237 | path.get_home_dir = lambda : "someplace" |
|
238 | path.get_home_dir = lambda : "someplace" | |
238 | os.name = "posix" |
|
239 | os.name = "posix" | |
239 | env.pop('IPYTHON_DIR', None) |
|
240 | env.pop('IPYTHON_DIR', None) | |
240 | env.pop('IPYTHONDIR', None) |
|
241 | env.pop('IPYTHONDIR', None) | |
241 | env.pop('XDG_CONFIG_HOME', None) |
|
242 | env.pop('XDG_CONFIG_HOME', None) | |
242 | ipdir = path.get_ipython_dir() |
|
243 | ipdir = path.get_ipython_dir() | |
243 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) |
|
244 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) | |
244 |
|
245 | |||
245 | @with_environment |
|
246 | @with_environment | |
246 | def test_get_ipython_dir_3(): |
|
247 | def test_get_ipython_dir_3(): | |
247 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" |
|
248 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" | |
248 | path.get_home_dir = lambda : "someplace" |
|
249 | path.get_home_dir = lambda : "someplace" | |
249 | os.name = "posix" |
|
250 | os.name = "posix" | |
250 | env.pop('IPYTHON_DIR', None) |
|
251 | env.pop('IPYTHON_DIR', None) | |
251 | env.pop('IPYTHONDIR', None) |
|
252 | env.pop('IPYTHONDIR', None) | |
252 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
253 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | |
253 | ipdir = path.get_ipython_dir() |
|
254 | ipdir = path.get_ipython_dir() | |
254 | nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython")) |
|
255 | nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython")) | |
255 |
|
256 | |||
256 | @with_environment |
|
257 | @with_environment | |
257 | def test_get_ipython_dir_4(): |
|
258 | def test_get_ipython_dir_4(): | |
258 | """test_get_ipython_dir_4, use XDG if both exist.""" |
|
259 | """test_get_ipython_dir_4, use XDG if both exist.""" | |
259 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
260 | path.get_home_dir = lambda : HOME_TEST_DIR | |
260 | os.name = "posix" |
|
261 | os.name = "posix" | |
261 | env.pop('IPYTHON_DIR', None) |
|
262 | env.pop('IPYTHON_DIR', None) | |
262 | env.pop('IPYTHONDIR', None) |
|
263 | env.pop('IPYTHONDIR', None) | |
263 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
264 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | |
264 | xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython") |
|
265 | xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython") | |
265 | ipdir = path.get_ipython_dir() |
|
266 | ipdir = path.get_ipython_dir() | |
266 | nt.assert_equal(ipdir, xdg_ipdir) |
|
267 | nt.assert_equal(ipdir, xdg_ipdir) | |
267 |
|
268 | |||
268 | @with_environment |
|
269 | @with_environment | |
269 | def test_get_ipython_dir_5(): |
|
270 | def test_get_ipython_dir_5(): | |
270 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
271 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" | |
271 | os.name = "posix" |
|
272 | os.name = "posix" | |
272 | env.pop('IPYTHON_DIR', None) |
|
273 | env.pop('IPYTHON_DIR', None) | |
273 | env.pop('IPYTHONDIR', None) |
|
274 | env.pop('IPYTHONDIR', None) | |
274 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
275 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | |
275 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
276 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) | |
276 | ipdir = path.get_ipython_dir() |
|
277 | ipdir = path.get_ipython_dir() | |
277 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
278 | nt.assert_equal(ipdir, IP_TEST_DIR) | |
278 |
|
279 | |||
279 | @with_environment |
|
280 | @with_environment | |
280 | def test_get_ipython_dir_6(): |
|
281 | def test_get_ipython_dir_6(): | |
281 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" |
|
282 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" | |
282 | path.get_home_dir = lambda : 'somehome' |
|
283 | path.get_home_dir = lambda : 'somehome' | |
283 | path.get_xdg_dir = lambda : 'somexdg' |
|
284 | path.get_xdg_dir = lambda : 'somexdg' | |
284 | os.name = "posix" |
|
285 | os.name = "posix" | |
285 | env.pop('IPYTHON_DIR', None) |
|
286 | env.pop('IPYTHON_DIR', None) | |
286 | env.pop('IPYTHONDIR', None) |
|
287 | env.pop('IPYTHONDIR', None) | |
287 | xdg_ipdir = os.path.join("somexdg", "ipython") |
|
288 | xdg_ipdir = os.path.join("somexdg", "ipython") | |
288 | ipdir = path.get_ipython_dir() |
|
289 | ipdir = path.get_ipython_dir() | |
289 | nt.assert_equal(ipdir, xdg_ipdir) |
|
290 | nt.assert_equal(ipdir, xdg_ipdir) | |
290 |
|
291 | |||
291 | @with_environment |
|
292 | @with_environment | |
292 | def test_get_xdg_dir_1(): |
|
293 | def test_get_xdg_dir_1(): | |
293 | """test_get_xdg_dir_1, check xdg_dir""" |
|
294 | """test_get_xdg_dir_1, check xdg_dir""" | |
294 | reload(path) |
|
295 | reload(path) | |
295 | path.get_home_dir = lambda : 'somewhere' |
|
296 | path.get_home_dir = lambda : 'somewhere' | |
296 | os.name = "posix" |
|
297 | os.name = "posix" | |
297 | env.pop('IPYTHON_DIR', None) |
|
298 | env.pop('IPYTHON_DIR', None) | |
298 | env.pop('IPYTHONDIR', None) |
|
299 | env.pop('IPYTHONDIR', None) | |
299 | env.pop('XDG_CONFIG_HOME', None) |
|
300 | env.pop('XDG_CONFIG_HOME', None) | |
300 |
|
301 | |||
301 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) |
|
302 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) | |
302 |
|
303 | |||
303 |
|
304 | |||
304 | @with_environment |
|
305 | @with_environment | |
305 | def test_get_xdg_dir_1(): |
|
306 | def test_get_xdg_dir_1(): | |
306 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" |
|
307 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" | |
307 | reload(path) |
|
308 | reload(path) | |
308 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
309 | path.get_home_dir = lambda : HOME_TEST_DIR | |
309 | os.name = "posix" |
|
310 | os.name = "posix" | |
310 | env.pop('IPYTHON_DIR', None) |
|
311 | env.pop('IPYTHON_DIR', None) | |
311 | env.pop('IPYTHONDIR', None) |
|
312 | env.pop('IPYTHONDIR', None) | |
312 | env.pop('XDG_CONFIG_HOME', None) |
|
313 | env.pop('XDG_CONFIG_HOME', None) | |
313 | nt.assert_equal(path.get_xdg_dir(), None) |
|
314 | nt.assert_equal(path.get_xdg_dir(), None) | |
314 |
|
315 | |||
315 | @with_environment |
|
316 | @with_environment | |
316 | def test_get_xdg_dir_2(): |
|
317 | def test_get_xdg_dir_2(): | |
317 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" |
|
318 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" | |
318 | reload(path) |
|
319 | reload(path) | |
319 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
320 | path.get_home_dir = lambda : HOME_TEST_DIR | |
320 | os.name = "posix" |
|
321 | os.name = "posix" | |
321 | env.pop('IPYTHON_DIR', None) |
|
322 | env.pop('IPYTHON_DIR', None) | |
322 | env.pop('IPYTHONDIR', None) |
|
323 | env.pop('IPYTHONDIR', None) | |
323 | env.pop('XDG_CONFIG_HOME', None) |
|
324 | env.pop('XDG_CONFIG_HOME', None) | |
324 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
325 | cfgdir=os.path.join(path.get_home_dir(), '.config') | |
325 | os.makedirs(cfgdir) |
|
326 | os.makedirs(cfgdir) | |
326 |
|
327 | |||
327 | nt.assert_equal(path.get_xdg_dir(), cfgdir) |
|
328 | nt.assert_equal(path.get_xdg_dir(), cfgdir) | |
328 |
|
329 | |||
329 | def test_filefind(): |
|
330 | def test_filefind(): | |
330 | """Various tests for filefind""" |
|
331 | """Various tests for filefind""" | |
331 | f = tempfile.NamedTemporaryFile() |
|
332 | f = tempfile.NamedTemporaryFile() | |
332 | # print 'fname:',f.name |
|
333 | # print 'fname:',f.name | |
333 | alt_dirs = path.get_ipython_dir() |
|
334 | alt_dirs = path.get_ipython_dir() | |
334 | t = path.filefind(f.name, alt_dirs) |
|
335 | t = path.filefind(f.name, alt_dirs) | |
335 | # print 'found:',t |
|
336 | # print 'found:',t | |
336 |
|
337 | |||
337 |
|
338 | |||
338 | def test_get_ipython_package_dir(): |
|
339 | def test_get_ipython_package_dir(): | |
339 | ipdir = path.get_ipython_package_dir() |
|
340 | ipdir = path.get_ipython_package_dir() | |
340 | nt.assert_true(os.path.isdir(ipdir)) |
|
341 | nt.assert_true(os.path.isdir(ipdir)) | |
341 |
|
342 | |||
342 |
|
343 | |||
343 | def test_get_ipython_module_path(): |
|
344 | def test_get_ipython_module_path(): | |
344 | ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp') |
|
345 | ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp') | |
345 | nt.assert_true(os.path.isfile(ipapp_path)) |
|
346 | nt.assert_true(os.path.isfile(ipapp_path)) | |
346 |
|
347 | |||
347 |
|
348 | |||
348 | @dec.skip_if_not_win32 |
|
349 | @dec.skip_if_not_win32 | |
349 | def test_get_long_path_name_win32(): |
|
350 | def test_get_long_path_name_win32(): | |
350 | p = path.get_long_path_name('c:\\docume~1') |
|
351 | p = path.get_long_path_name('c:\\docume~1') | |
351 | nt.assert_equals(p,u'c:\\Documents and Settings') |
|
352 | nt.assert_equals(p,u'c:\\Documents and Settings') | |
352 |
|
353 | |||
353 |
|
354 | |||
354 | @dec.skip_win32 |
|
355 | @dec.skip_win32 | |
355 | def test_get_long_path_name(): |
|
356 | def test_get_long_path_name(): | |
356 | p = path.get_long_path_name('/usr/local') |
|
357 | p = path.get_long_path_name('/usr/local') | |
357 | nt.assert_equals(p,'/usr/local') |
|
358 | nt.assert_equals(p,'/usr/local') | |
358 |
|
359 |
General Comments 0
You need to be logged in to leave comments.
Login now