Show More
@@ -1,393 +1,401 | |||||
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 | fs_encoding = sys.getfilesystemencoding() | |||
|
29 | ||||
|
30 | def _cast_unicode(s, enc=None): | |||
|
31 | """Turn 8-bit strings into unicode.""" | |||
|
32 | if isinstance(s, bytes): | |||
|
33 | enc = enc or sys.getdefaultencoding() | |||
|
34 | return s.decode(enc) | |||
|
35 | return s | |||
|
36 | ||||
28 |
|
37 | |||
29 | def _get_long_path_name(path): |
|
38 | def _get_long_path_name(path): | |
30 | """Dummy no-op.""" |
|
39 | """Dummy no-op.""" | |
31 | return path |
|
40 | return path | |
32 |
|
41 | |||
33 |
|
||||
34 | if sys.platform == 'win32': |
|
42 | if sys.platform == 'win32': | |
35 | def _get_long_path_name(path): |
|
43 | def _get_long_path_name(path): | |
36 | """Get a long path name (expand ~) on Windows using ctypes. |
|
44 | """Get a long path name (expand ~) on Windows using ctypes. | |
37 |
|
45 | |||
38 | Examples |
|
46 | Examples | |
39 | -------- |
|
47 | -------- | |
40 |
|
48 | |||
41 | >>> get_long_path_name('c:\\docume~1') |
|
49 | >>> get_long_path_name('c:\\docume~1') | |
42 | u'c:\\\\Documents and Settings' |
|
50 | u'c:\\\\Documents and Settings' | |
43 |
|
51 | |||
44 | """ |
|
52 | """ | |
45 | try: |
|
53 | try: | |
46 | import ctypes |
|
54 | import ctypes | |
47 | except ImportError: |
|
55 | except ImportError: | |
48 | raise ImportError('you need to have ctypes installed for this to work') |
|
56 | raise ImportError('you need to have ctypes installed for this to work') | |
49 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
57 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW | |
50 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
58 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, | |
51 | ctypes.c_uint ] |
|
59 | ctypes.c_uint ] | |
52 |
|
60 | |||
53 | buf = ctypes.create_unicode_buffer(260) |
|
61 | buf = ctypes.create_unicode_buffer(260) | |
54 | rv = _GetLongPathName(path, buf, 260) |
|
62 | rv = _GetLongPathName(path, buf, 260) | |
55 | if rv == 0 or rv > 260: |
|
63 | if rv == 0 or rv > 260: | |
56 | return path |
|
64 | return path | |
57 | else: |
|
65 | else: | |
58 | return buf.value |
|
66 | return buf.value | |
59 |
|
67 | |||
60 |
|
68 | |||
61 | def get_long_path_name(path): |
|
69 | def get_long_path_name(path): | |
62 | """Expand a path into its long form. |
|
70 | """Expand a path into its long form. | |
63 |
|
71 | |||
64 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
72 | On Windows this expands any ~ in the paths. On other platforms, it is | |
65 | a null operation. |
|
73 | a null operation. | |
66 | """ |
|
74 | """ | |
67 | return _get_long_path_name(path) |
|
75 | return _get_long_path_name(path) | |
68 |
|
76 | |||
69 |
|
77 | |||
70 | def get_py_filename(name): |
|
78 | def get_py_filename(name): | |
71 | """Return a valid python filename in the current directory. |
|
79 | """Return a valid python filename in the current directory. | |
72 |
|
80 | |||
73 | If the given name is not a file, it adds '.py' and searches again. |
|
81 | 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.""" |
|
82 | Raises IOError with an informative message if the file isn't found.""" | |
75 |
|
83 | |||
76 | name = os.path.expanduser(name) |
|
84 | name = os.path.expanduser(name) | |
77 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
85 | if not os.path.isfile(name) and not name.endswith('.py'): | |
78 | name += '.py' |
|
86 | name += '.py' | |
79 | if os.path.isfile(name): |
|
87 | if os.path.isfile(name): | |
80 | return name |
|
88 | return name | |
81 | else: |
|
89 | else: | |
82 | raise IOError,'File `%s` not found.' % name |
|
90 | raise IOError,'File `%s` not found.' % name | |
83 |
|
91 | |||
84 |
|
92 | |||
85 | def filefind(filename, path_dirs=None): |
|
93 | def filefind(filename, path_dirs=None): | |
86 | """Find a file by looking through a sequence of paths. |
|
94 | """Find a file by looking through a sequence of paths. | |
87 |
|
95 | |||
88 | This iterates through a sequence of paths looking for a file and returns |
|
96 | 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 |
|
97 | 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 |
|
98 | path dirs is given, the filename is tested as is, after running through | |
91 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: |
|
99 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: | |
92 |
|
100 | |||
93 | filefind('myfile.txt') |
|
101 | filefind('myfile.txt') | |
94 |
|
102 | |||
95 | will find the file in the current working dir, but:: |
|
103 | will find the file in the current working dir, but:: | |
96 |
|
104 | |||
97 | filefind('~/myfile.txt') |
|
105 | filefind('~/myfile.txt') | |
98 |
|
106 | |||
99 | Will find the file in the users home directory. This function does not |
|
107 | 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. |
|
108 | automatically try any paths, such as the cwd or the user's home directory. | |
101 |
|
109 | |||
102 | Parameters |
|
110 | Parameters | |
103 | ---------- |
|
111 | ---------- | |
104 | filename : str |
|
112 | filename : str | |
105 | The filename to look for. |
|
113 | The filename to look for. | |
106 | path_dirs : str, None or sequence of str |
|
114 | path_dirs : str, None or sequence of str | |
107 | The sequence of paths to look for the file in. If None, the filename |
|
115 | 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 |
|
116 | 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 |
|
117 | put into a sequence and the searched. If a sequence, walk through | |
110 | each element and join with ``filename``, calling :func:`expandvars` |
|
118 | each element and join with ``filename``, calling :func:`expandvars` | |
111 | and :func:`expanduser` before testing for existence. |
|
119 | and :func:`expanduser` before testing for existence. | |
112 |
|
120 | |||
113 | Returns |
|
121 | Returns | |
114 | ------- |
|
122 | ------- | |
115 | Raises :exc:`IOError` or returns absolute path to file. |
|
123 | Raises :exc:`IOError` or returns absolute path to file. | |
116 | """ |
|
124 | """ | |
117 |
|
125 | |||
118 | # If paths are quoted, abspath gets confused, strip them... |
|
126 | # If paths are quoted, abspath gets confused, strip them... | |
119 | filename = filename.strip('"').strip("'") |
|
127 | filename = filename.strip('"').strip("'") | |
120 | # If the input is an absolute path, just check it exists |
|
128 | # If the input is an absolute path, just check it exists | |
121 | if os.path.isabs(filename) and os.path.isfile(filename): |
|
129 | if os.path.isabs(filename) and os.path.isfile(filename): | |
122 | return filename |
|
130 | return filename | |
123 |
|
131 | |||
124 | if path_dirs is None: |
|
132 | if path_dirs is None: | |
125 | path_dirs = ("",) |
|
133 | path_dirs = ("",) | |
126 | elif isinstance(path_dirs, basestring): |
|
134 | elif isinstance(path_dirs, basestring): | |
127 | path_dirs = (path_dirs,) |
|
135 | path_dirs = (path_dirs,) | |
128 |
|
136 | |||
129 | for path in path_dirs: |
|
137 | for path in path_dirs: | |
130 | if path == '.': path = os.getcwd() |
|
138 | if path == '.': path = os.getcwd() | |
131 | testname = expand_path(os.path.join(path, filename)) |
|
139 | testname = expand_path(os.path.join(path, filename)) | |
132 | if os.path.isfile(testname): |
|
140 | if os.path.isfile(testname): | |
133 | return os.path.abspath(testname) |
|
141 | return os.path.abspath(testname) | |
134 |
|
142 | |||
135 | raise IOError("File %r does not exist in any of the search paths: %r" % |
|
143 | raise IOError("File %r does not exist in any of the search paths: %r" % | |
136 | (filename, path_dirs) ) |
|
144 | (filename, path_dirs) ) | |
137 |
|
145 | |||
138 |
|
146 | |||
139 | class HomeDirError(Exception): |
|
147 | class HomeDirError(Exception): | |
140 | pass |
|
148 | pass | |
141 |
|
149 | |||
142 |
|
150 | |||
143 | def get_home_dir(): |
|
151 | def get_home_dir(): | |
144 | """Return the closest possible equivalent to a 'home' directory. |
|
152 | """Return the closest possible equivalent to a 'home' directory. | |
145 |
|
153 | |||
146 | * On POSIX, we try $HOME. |
|
154 | * On POSIX, we try $HOME. | |
147 | * On Windows we try: |
|
155 | * On Windows we try: | |
148 | - %HOMESHARE% |
|
156 | - %HOMESHARE% | |
149 | - %HOMEDRIVE\%HOMEPATH% |
|
157 | - %HOMEDRIVE\%HOMEPATH% | |
150 | - %USERPROFILE% |
|
158 | - %USERPROFILE% | |
151 | - Registry hack for My Documents |
|
159 | - Registry hack for My Documents | |
152 | - %HOME%: rare, but some people with unix-like setups may have defined it |
|
160 | - %HOME%: rare, but some people with unix-like setups may have defined it | |
153 | * On Dos C:\ |
|
161 | * On Dos C:\ | |
154 |
|
162 | |||
155 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
163 | Currently only Posix and NT are implemented, a HomeDirError exception is | |
156 | raised for all other OSes. |
|
164 | raised for all other OSes. | |
157 | """ |
|
165 | """ | |
158 |
|
166 | |||
159 | isdir = os.path.isdir |
|
167 | isdir = os.path.isdir | |
160 | env = os.environ |
|
168 | env = os.environ | |
161 |
|
169 | |||
162 | # first, check py2exe distribution root directory for _ipython. |
|
170 | # first, check py2exe distribution root directory for _ipython. | |
163 | # This overrides all. Normally does not exist. |
|
171 | # This overrides all. Normally does not exist. | |
164 |
|
172 | |||
165 | if hasattr(sys, "frozen"): #Is frozen by py2exe |
|
173 | if hasattr(sys, "frozen"): #Is frozen by py2exe | |
166 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file |
|
174 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file | |
167 | root, rest = IPython.__file__.lower().split('library.zip') |
|
175 | root, rest = IPython.__file__.lower().split('library.zip') | |
168 | else: |
|
176 | else: | |
169 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
177 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") | |
170 | root=os.path.abspath(root).rstrip('\\') |
|
178 | root=os.path.abspath(root).rstrip('\\') | |
171 | if isdir(os.path.join(root, '_ipython')): |
|
179 | if isdir(os.path.join(root, '_ipython')): | |
172 | os.environ["IPYKITROOT"] = root |
|
180 | os.environ["IPYKITROOT"] = root | |
173 |
return root |
|
181 | return _cast_unicode(root, fs_encoding) | |
174 |
|
182 | |||
175 | if os.name == 'posix': |
|
183 | if os.name == 'posix': | |
176 | # Linux, Unix, AIX, OS X |
|
184 | # Linux, Unix, AIX, OS X | |
177 | try: |
|
185 | try: | |
178 | homedir = env['HOME'] |
|
186 | homedir = env['HOME'] | |
179 | except KeyError: |
|
187 | except KeyError: | |
180 | # Last-ditch attempt at finding a suitable $HOME, on systems where |
|
188 | # Last-ditch attempt at finding a suitable $HOME, on systems where | |
181 | # it may not be defined in the environment but the system shell |
|
189 | # it may not be defined in the environment but the system shell | |
182 | # still knows it - reported once as: |
|
190 | # still knows it - reported once as: | |
183 | # https://github.com/ipython/ipython/issues/154 |
|
191 | # https://github.com/ipython/ipython/issues/154 | |
184 | from subprocess import Popen, PIPE |
|
192 | from subprocess import Popen, PIPE | |
185 | homedir = Popen('echo $HOME', shell=True, |
|
193 | homedir = Popen('echo $HOME', shell=True, | |
186 | stdout=PIPE).communicate()[0].strip() |
|
194 | stdout=PIPE).communicate()[0].strip() | |
187 | if homedir: |
|
195 | if homedir: | |
188 |
return homedir |
|
196 | return _cast_unicode(homedir, fs_encoding) | |
189 | else: |
|
197 | else: | |
190 | raise HomeDirError('Undefined $HOME, IPython cannot proceed.') |
|
198 | raise HomeDirError('Undefined $HOME, IPython cannot proceed.') | |
191 | else: |
|
199 | else: | |
192 |
return homedir |
|
200 | return _cast_unicode(homedir, fs_encoding) | |
193 | elif os.name == 'nt': |
|
201 | elif os.name == 'nt': | |
194 | # Now for win9x, XP, Vista, 7? |
|
202 | # Now for win9x, XP, Vista, 7? | |
195 | # For some strange reason all of these return 'nt' for os.name. |
|
203 | # For some strange reason all of these return 'nt' for os.name. | |
196 | # First look for a network home directory. This will return the UNC |
|
204 | # First look for a network home directory. This will return the UNC | |
197 | # path (\\server\\Users\%username%) not the mapped path (Z:\). This |
|
205 | # path (\\server\\Users\%username%) not the mapped path (Z:\). This | |
198 | # is needed when running IPython on cluster where all paths have to |
|
206 | # is needed when running IPython on cluster where all paths have to | |
199 | # be UNC. |
|
207 | # be UNC. | |
200 | try: |
|
208 | try: | |
201 | homedir = env['HOMESHARE'] |
|
209 | homedir = env['HOMESHARE'] | |
202 | except KeyError: |
|
210 | except KeyError: | |
203 | pass |
|
211 | pass | |
204 | else: |
|
212 | else: | |
205 | if isdir(homedir): |
|
213 | if isdir(homedir): | |
206 |
return homedir |
|
214 | return _cast_unicode(homedir, fs_encoding) | |
207 |
|
215 | |||
208 | # Now look for a local home directory |
|
216 | # Now look for a local home directory | |
209 | try: |
|
217 | try: | |
210 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
218 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) | |
211 | except KeyError: |
|
219 | except KeyError: | |
212 | pass |
|
220 | pass | |
213 | else: |
|
221 | else: | |
214 | if isdir(homedir): |
|
222 | if isdir(homedir): | |
215 |
return homedir |
|
223 | return _cast_unicode(homedir, fs_encoding) | |
216 |
|
224 | |||
217 | # Now the users profile directory |
|
225 | # Now the users profile directory | |
218 | try: |
|
226 | try: | |
219 | homedir = os.path.join(env['USERPROFILE']) |
|
227 | homedir = os.path.join(env['USERPROFILE']) | |
220 | except KeyError: |
|
228 | except KeyError: | |
221 | pass |
|
229 | pass | |
222 | else: |
|
230 | else: | |
223 | if isdir(homedir): |
|
231 | if isdir(homedir): | |
224 |
return homedir |
|
232 | return _cast_unicode(homedir, fs_encoding) | |
225 |
|
233 | |||
226 | # Use the registry to get the 'My Documents' folder. |
|
234 | # Use the registry to get the 'My Documents' folder. | |
227 | try: |
|
235 | try: | |
228 | import _winreg as wreg |
|
236 | import _winreg as wreg | |
229 | key = wreg.OpenKey( |
|
237 | key = wreg.OpenKey( | |
230 | wreg.HKEY_CURRENT_USER, |
|
238 | wreg.HKEY_CURRENT_USER, | |
231 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |
|
239 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" | |
232 | ) |
|
240 | ) | |
233 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
241 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
234 | key.Close() |
|
242 | key.Close() | |
235 | except: |
|
243 | except: | |
236 | pass |
|
244 | pass | |
237 | else: |
|
245 | else: | |
238 | if isdir(homedir): |
|
246 | if isdir(homedir): | |
239 |
return homedir |
|
247 | return _cast_unicode(homedir, fs_encoding) | |
240 |
|
248 | |||
241 | # A user with a lot of unix tools in win32 may have defined $HOME. |
|
249 | # A user with a lot of unix tools in win32 may have defined $HOME. | |
242 | # Try this as a last ditch option. |
|
250 | # Try this as a last ditch option. | |
243 | try: |
|
251 | try: | |
244 | homedir = env['HOME'] |
|
252 | homedir = env['HOME'] | |
245 | except KeyError: |
|
253 | except KeyError: | |
246 | pass |
|
254 | pass | |
247 | else: |
|
255 | else: | |
248 | if isdir(homedir): |
|
256 | if isdir(homedir): | |
249 |
return homedir |
|
257 | return _cast_unicode(homedir, fs_encoding) | |
250 |
|
258 | |||
251 | # If all else fails, raise HomeDirError |
|
259 | # If all else fails, raise HomeDirError | |
252 | raise HomeDirError('No valid home directory could be found') |
|
260 | raise HomeDirError('No valid home directory could be found') | |
253 | elif os.name == 'dos': |
|
261 | elif os.name == 'dos': | |
254 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
262 | # Desperate, may do absurd things in classic MacOS. May work under DOS. | |
255 | return 'C:\\'.decode(sys.getfilesystemencoding()) |
|
263 | return u'C:\\' | |
256 | else: |
|
264 | else: | |
257 | raise HomeDirError('No valid home directory could be found for your OS') |
|
265 | raise HomeDirError('No valid home directory could be found for your OS') | |
258 |
|
266 | |||
259 | def get_xdg_dir(): |
|
267 | def get_xdg_dir(): | |
260 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. |
|
268 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. | |
261 |
|
269 | |||
262 | This is only for posix (Linux,Unix,OS X, etc) systems. |
|
270 | This is only for posix (Linux,Unix,OS X, etc) systems. | |
263 | """ |
|
271 | """ | |
264 |
|
272 | |||
265 | isdir = os.path.isdir |
|
273 | isdir = os.path.isdir | |
266 | env = os.environ |
|
274 | env = os.environ | |
267 |
|
275 | |||
268 | if os.name == 'posix': |
|
276 | if os.name == 'posix': | |
269 | # Linux, Unix, AIX, OS X |
|
277 | # Linux, Unix, AIX, OS X | |
270 | # use ~/.config if not set OR empty |
|
278 | # use ~/.config if not set OR empty | |
271 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
279 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') | |
272 | if xdg and isdir(xdg): |
|
280 | if xdg and isdir(xdg): | |
273 |
return xdg |
|
281 | return _cast_unicode(xdg, fs_encoding) | |
274 |
|
282 | |||
275 | return None |
|
283 | return None | |
276 |
|
284 | |||
277 |
|
285 | |||
278 | def get_ipython_dir(): |
|
286 | def get_ipython_dir(): | |
279 | """Get the IPython directory for this platform and user. |
|
287 | """Get the IPython directory for this platform and user. | |
280 |
|
288 | |||
281 | This uses the logic in `get_home_dir` to find the home directory |
|
289 | This uses the logic in `get_home_dir` to find the home directory | |
282 | and the adds .ipython to the end of the path. |
|
290 | and the adds .ipython to the end of the path. | |
283 | """ |
|
291 | """ | |
284 |
|
292 | |||
285 | env = os.environ |
|
293 | env = os.environ | |
286 | pjoin = os.path.join |
|
294 | pjoin = os.path.join | |
287 | exists = os.path.exists |
|
295 | exists = os.path.exists | |
288 |
|
296 | |||
289 | ipdir_def = '.ipython' |
|
297 | ipdir_def = '.ipython' | |
290 | xdg_def = 'ipython' |
|
298 | xdg_def = 'ipython' | |
291 |
|
299 | |||
292 | home_dir = get_home_dir() |
|
300 | home_dir = get_home_dir() | |
293 | xdg_dir = get_xdg_dir() |
|
301 | xdg_dir = get_xdg_dir() | |
294 | # import pdb; pdb.set_trace() # dbg |
|
302 | # import pdb; pdb.set_trace() # dbg | |
295 | ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None)) |
|
303 | ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None)) | |
296 | if ipdir is None: |
|
304 | if ipdir is None: | |
297 | # not set explicitly, use XDG_CONFIG_HOME or HOME |
|
305 | # not set explicitly, use XDG_CONFIG_HOME or HOME | |
298 | home_ipdir = pjoin(home_dir, ipdir_def) |
|
306 | home_ipdir = pjoin(home_dir, ipdir_def) | |
299 | if xdg_dir: |
|
307 | if xdg_dir: | |
300 | # use XDG, as long as the user isn't already |
|
308 | # use XDG, as long as the user isn't already | |
301 | # using $HOME/.ipython and *not* XDG/ipython |
|
309 | # using $HOME/.ipython and *not* XDG/ipython | |
302 |
|
310 | |||
303 | xdg_ipdir = pjoin(xdg_dir, xdg_def) |
|
311 | xdg_ipdir = pjoin(xdg_dir, xdg_def) | |
304 |
|
312 | |||
305 | if exists(xdg_ipdir) or not exists(home_ipdir): |
|
313 | if exists(xdg_ipdir) or not exists(home_ipdir): | |
306 | ipdir = xdg_ipdir |
|
314 | ipdir = xdg_ipdir | |
307 |
|
315 | |||
308 | if ipdir is None: |
|
316 | if ipdir is None: | |
309 | # not using XDG |
|
317 | # not using XDG | |
310 | ipdir = home_ipdir |
|
318 | ipdir = home_ipdir | |
311 |
|
319 | |||
312 | return ipdir.decode(sys.getfilesystemencoding()) |
|
320 | return _cast_unicode(ipdir, fs_encoding) | |
313 |
|
321 | |||
314 |
|
322 | |||
315 | def get_ipython_package_dir(): |
|
323 | def get_ipython_package_dir(): | |
316 | """Get the base directory where IPython itself is installed.""" |
|
324 | """Get the base directory where IPython itself is installed.""" | |
317 | ipdir = os.path.dirname(IPython.__file__) |
|
325 | ipdir = os.path.dirname(IPython.__file__) | |
318 | return ipdir.decode(sys.getfilesystemencoding()) |
|
326 | return _cast_unicode(ipdir, fs_encoding) | |
319 |
|
327 | |||
320 |
|
328 | |||
321 | def get_ipython_module_path(module_str): |
|
329 | def get_ipython_module_path(module_str): | |
322 | """Find the path to an IPython module in this version of IPython. |
|
330 | """Find the path to an IPython module in this version of IPython. | |
323 |
|
331 | |||
324 | This will always find the version of the module that is in this importable |
|
332 | This will always find the version of the module that is in this importable | |
325 | IPython package. This will always return the path to the ``.py`` |
|
333 | IPython package. This will always return the path to the ``.py`` | |
326 | version of the module. |
|
334 | version of the module. | |
327 | """ |
|
335 | """ | |
328 | if module_str == 'IPython': |
|
336 | if module_str == 'IPython': | |
329 | return os.path.join(get_ipython_package_dir(), '__init__.py') |
|
337 | return os.path.join(get_ipython_package_dir(), '__init__.py') | |
330 | mod = import_item(module_str) |
|
338 | mod = import_item(module_str) | |
331 | the_path = mod.__file__.replace('.pyc', '.py') |
|
339 | the_path = mod.__file__.replace('.pyc', '.py') | |
332 | the_path = the_path.replace('.pyo', '.py') |
|
340 | the_path = the_path.replace('.pyo', '.py') | |
333 | return the_path.decode(sys.getfilesystemencoding()) |
|
341 | return _cast_unicode(the_path, fs_encoding) | |
334 |
|
342 | |||
335 |
|
343 | |||
336 | def expand_path(s): |
|
344 | def expand_path(s): | |
337 | """Expand $VARS and ~names in a string, like a shell |
|
345 | """Expand $VARS and ~names in a string, like a shell | |
338 |
|
346 | |||
339 | :Examples: |
|
347 | :Examples: | |
340 |
|
348 | |||
341 | In [2]: os.environ['FOO']='test' |
|
349 | In [2]: os.environ['FOO']='test' | |
342 |
|
350 | |||
343 | In [3]: expand_path('variable FOO is $FOO') |
|
351 | In [3]: expand_path('variable FOO is $FOO') | |
344 | Out[3]: 'variable FOO is test' |
|
352 | Out[3]: 'variable FOO is test' | |
345 | """ |
|
353 | """ | |
346 | # This is a pretty subtle hack. When expand user is given a UNC path |
|
354 | # This is a pretty subtle hack. When expand user is given a UNC path | |
347 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes |
|
355 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes | |
348 | # the $ to get (\\server\share\%username%). I think it considered $ |
|
356 | # the $ to get (\\server\share\%username%). I think it considered $ | |
349 | # alone an empty var. But, we need the $ to remains there (it indicates |
|
357 | # alone an empty var. But, we need the $ to remains there (it indicates | |
350 | # a hidden share). |
|
358 | # a hidden share). | |
351 | if os.name=='nt': |
|
359 | if os.name=='nt': | |
352 | s = s.replace('$\\', 'IPYTHON_TEMP') |
|
360 | s = s.replace('$\\', 'IPYTHON_TEMP') | |
353 | s = os.path.expandvars(os.path.expanduser(s)) |
|
361 | s = os.path.expandvars(os.path.expanduser(s)) | |
354 | if os.name=='nt': |
|
362 | if os.name=='nt': | |
355 | s = s.replace('IPYTHON_TEMP', '$\\') |
|
363 | s = s.replace('IPYTHON_TEMP', '$\\') | |
356 | return s |
|
364 | return s | |
357 |
|
365 | |||
358 |
|
366 | |||
359 | def target_outdated(target,deps): |
|
367 | def target_outdated(target,deps): | |
360 | """Determine whether a target is out of date. |
|
368 | """Determine whether a target is out of date. | |
361 |
|
369 | |||
362 | target_outdated(target,deps) -> 1/0 |
|
370 | target_outdated(target,deps) -> 1/0 | |
363 |
|
371 | |||
364 | deps: list of filenames which MUST exist. |
|
372 | deps: list of filenames which MUST exist. | |
365 | target: single filename which may or may not exist. |
|
373 | target: single filename which may or may not exist. | |
366 |
|
374 | |||
367 | If target doesn't exist or is older than any file listed in deps, return |
|
375 | If target doesn't exist or is older than any file listed in deps, return | |
368 | true, otherwise return false. |
|
376 | true, otherwise return false. | |
369 | """ |
|
377 | """ | |
370 | try: |
|
378 | try: | |
371 | target_time = os.path.getmtime(target) |
|
379 | target_time = os.path.getmtime(target) | |
372 | except os.error: |
|
380 | except os.error: | |
373 | return 1 |
|
381 | return 1 | |
374 | for dep in deps: |
|
382 | for dep in deps: | |
375 | dep_time = os.path.getmtime(dep) |
|
383 | dep_time = os.path.getmtime(dep) | |
376 | if dep_time > target_time: |
|
384 | if dep_time > target_time: | |
377 | #print "For target",target,"Dep failed:",dep # dbg |
|
385 | #print "For target",target,"Dep failed:",dep # dbg | |
378 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
386 | #print "times (dep,tar):",dep_time,target_time # dbg | |
379 | return 1 |
|
387 | return 1 | |
380 | return 0 |
|
388 | return 0 | |
381 |
|
389 | |||
382 |
|
390 | |||
383 | def target_update(target,deps,cmd): |
|
391 | def target_update(target,deps,cmd): | |
384 | """Update a target with a given command given a list of dependencies. |
|
392 | """Update a target with a given command given a list of dependencies. | |
385 |
|
393 | |||
386 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
394 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
387 |
|
395 | |||
388 | This is just a wrapper around target_outdated() which calls the given |
|
396 | This is just a wrapper around target_outdated() which calls the given | |
389 | command if target is outdated.""" |
|
397 | command if target is outdated.""" | |
390 |
|
398 | |||
391 | if target_outdated(target,deps): |
|
399 | if target_outdated(target,deps): | |
392 | system(cmd) |
|
400 | system(cmd) | |
393 |
|
401 |
General Comments 0
You need to be logged in to leave comments.
Login now