##// END OF EJS Templates
fix symlinked /home issue for FreeBSD...
Paul Ivanov -
Show More
@@ -1,461 +1,464 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-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 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 import tempfile
19 import tempfile
20 import warnings
20 import warnings
21 from hashlib import md5
21 from hashlib import md5
22
22
23 import IPython
23 import IPython
24 from IPython.utils.process import system
24 from IPython.utils.process import system
25 from IPython.utils.importstring import import_item
25 from IPython.utils.importstring import import_item
26 from IPython.utils import py3compat
26 from IPython.utils import py3compat
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Code
29 # Code
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 fs_encoding = sys.getfilesystemencoding()
32 fs_encoding = sys.getfilesystemencoding()
33
33
34 def _get_long_path_name(path):
34 def _get_long_path_name(path):
35 """Dummy no-op."""
35 """Dummy no-op."""
36 return path
36 return path
37
37
38 def _writable_dir(path):
38 def _writable_dir(path):
39 """Whether `path` is a directory, to which the user has write access."""
39 """Whether `path` is a directory, to which the user has write access."""
40 return os.path.isdir(path) and os.access(path, os.W_OK)
40 return os.path.isdir(path) and os.access(path, os.W_OK)
41
41
42 if sys.platform == 'win32':
42 if sys.platform == 'win32':
43 def _get_long_path_name(path):
43 def _get_long_path_name(path):
44 """Get a long path name (expand ~) on Windows using ctypes.
44 """Get a long path name (expand ~) on Windows using ctypes.
45
45
46 Examples
46 Examples
47 --------
47 --------
48
48
49 >>> get_long_path_name('c:\\docume~1')
49 >>> get_long_path_name('c:\\docume~1')
50 u'c:\\\\Documents and Settings'
50 u'c:\\\\Documents and Settings'
51
51
52 """
52 """
53 try:
53 try:
54 import ctypes
54 import ctypes
55 except ImportError:
55 except ImportError:
56 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')
57 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
57 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
58 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
58 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
59 ctypes.c_uint ]
59 ctypes.c_uint ]
60
60
61 buf = ctypes.create_unicode_buffer(260)
61 buf = ctypes.create_unicode_buffer(260)
62 rv = _GetLongPathName(path, buf, 260)
62 rv = _GetLongPathName(path, buf, 260)
63 if rv == 0 or rv > 260:
63 if rv == 0 or rv > 260:
64 return path
64 return path
65 else:
65 else:
66 return buf.value
66 return buf.value
67
67
68
68
69 def get_long_path_name(path):
69 def get_long_path_name(path):
70 """Expand a path into its long form.
70 """Expand a path into its long form.
71
71
72 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
73 a null operation.
73 a null operation.
74 """
74 """
75 return _get_long_path_name(path)
75 return _get_long_path_name(path)
76
76
77
77
78 def unquote_filename(name, win32=(sys.platform=='win32')):
78 def unquote_filename(name, win32=(sys.platform=='win32')):
79 """ On Windows, remove leading and trailing quotes from filenames.
79 """ On Windows, remove leading and trailing quotes from filenames.
80 """
80 """
81 if win32:
81 if win32:
82 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
82 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
83 name = name[1:-1]
83 name = name[1:-1]
84 return name
84 return name
85
85
86
86
87 def get_py_filename(name, force_win32=None):
87 def get_py_filename(name, force_win32=None):
88 """Return a valid python filename in the current directory.
88 """Return a valid python filename in the current directory.
89
89
90 If the given name is not a file, it adds '.py' and searches again.
90 If the given name is not a file, it adds '.py' and searches again.
91 Raises IOError with an informative message if the file isn't found.
91 Raises IOError with an informative message if the file isn't found.
92
92
93 On Windows, apply Windows semantics to the filename. In particular, remove
93 On Windows, apply Windows semantics to the filename. In particular, remove
94 any quoting that has been applied to it. This option can be forced for
94 any quoting that has been applied to it. This option can be forced for
95 testing purposes.
95 testing purposes.
96 """
96 """
97
97
98 name = os.path.expanduser(name)
98 name = os.path.expanduser(name)
99 if force_win32 is None:
99 if force_win32 is None:
100 win32 = (sys.platform == 'win32')
100 win32 = (sys.platform == 'win32')
101 else:
101 else:
102 win32 = force_win32
102 win32 = force_win32
103 name = unquote_filename(name, win32=win32)
103 name = unquote_filename(name, win32=win32)
104 if not os.path.isfile(name) and not name.endswith('.py'):
104 if not os.path.isfile(name) and not name.endswith('.py'):
105 name += '.py'
105 name += '.py'
106 if os.path.isfile(name):
106 if os.path.isfile(name):
107 return name
107 return name
108 else:
108 else:
109 raise IOError,'File `%s` not found.' % name
109 raise IOError,'File `%s` not found.' % name
110
110
111
111
112 def filefind(filename, path_dirs=None):
112 def filefind(filename, path_dirs=None):
113 """Find a file by looking through a sequence of paths.
113 """Find a file by looking through a sequence of paths.
114
114
115 This iterates through a sequence of paths looking for a file and returns
115 This iterates through a sequence of paths looking for a file and returns
116 the full, absolute path of the first occurence of the file. If no set of
116 the full, absolute path of the first occurence of the file. If no set of
117 path dirs is given, the filename is tested as is, after running through
117 path dirs is given, the filename is tested as is, after running through
118 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
118 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
119
119
120 filefind('myfile.txt')
120 filefind('myfile.txt')
121
121
122 will find the file in the current working dir, but::
122 will find the file in the current working dir, but::
123
123
124 filefind('~/myfile.txt')
124 filefind('~/myfile.txt')
125
125
126 Will find the file in the users home directory. This function does not
126 Will find the file in the users home directory. This function does not
127 automatically try any paths, such as the cwd or the user's home directory.
127 automatically try any paths, such as the cwd or the user's home directory.
128
128
129 Parameters
129 Parameters
130 ----------
130 ----------
131 filename : str
131 filename : str
132 The filename to look for.
132 The filename to look for.
133 path_dirs : str, None or sequence of str
133 path_dirs : str, None or sequence of str
134 The sequence of paths to look for the file in. If None, the filename
134 The sequence of paths to look for the file in. If None, the filename
135 need to be absolute or be in the cwd. If a string, the string is
135 need to be absolute or be in the cwd. If a string, the string is
136 put into a sequence and the searched. If a sequence, walk through
136 put into a sequence and the searched. If a sequence, walk through
137 each element and join with ``filename``, calling :func:`expandvars`
137 each element and join with ``filename``, calling :func:`expandvars`
138 and :func:`expanduser` before testing for existence.
138 and :func:`expanduser` before testing for existence.
139
139
140 Returns
140 Returns
141 -------
141 -------
142 Raises :exc:`IOError` or returns absolute path to file.
142 Raises :exc:`IOError` or returns absolute path to file.
143 """
143 """
144
144
145 # If paths are quoted, abspath gets confused, strip them...
145 # If paths are quoted, abspath gets confused, strip them...
146 filename = filename.strip('"').strip("'")
146 filename = filename.strip('"').strip("'")
147 # If the input is an absolute path, just check it exists
147 # If the input is an absolute path, just check it exists
148 if os.path.isabs(filename) and os.path.isfile(filename):
148 if os.path.isabs(filename) and os.path.isfile(filename):
149 return filename
149 return filename
150
150
151 if path_dirs is None:
151 if path_dirs is None:
152 path_dirs = ("",)
152 path_dirs = ("",)
153 elif isinstance(path_dirs, basestring):
153 elif isinstance(path_dirs, basestring):
154 path_dirs = (path_dirs,)
154 path_dirs = (path_dirs,)
155
155
156 for path in path_dirs:
156 for path in path_dirs:
157 if path == '.': path = os.getcwdu()
157 if path == '.': path = os.getcwdu()
158 testname = expand_path(os.path.join(path, filename))
158 testname = expand_path(os.path.join(path, filename))
159 if os.path.isfile(testname):
159 if os.path.isfile(testname):
160 return os.path.abspath(testname)
160 return os.path.abspath(testname)
161
161
162 raise IOError("File %r does not exist in any of the search paths: %r" %
162 raise IOError("File %r does not exist in any of the search paths: %r" %
163 (filename, path_dirs) )
163 (filename, path_dirs) )
164
164
165
165
166 class HomeDirError(Exception):
166 class HomeDirError(Exception):
167 pass
167 pass
168
168
169
169
170 def get_home_dir(require_writable=False):
170 def get_home_dir(require_writable=False):
171 """Return the 'home' directory, as a unicode string.
171 """Return the 'home' directory, as a unicode string.
172
172
173 * First, check for frozen env in case of py2exe
173 * First, check for frozen env in case of py2exe
174 * Otherwise, defer to os.path.expanduser('~')
174 * Otherwise, defer to os.path.expanduser('~')
175
175
176 See stdlib docs for how this is determined.
176 See stdlib docs for how this is determined.
177 $HOME is first priority on *ALL* platforms.
177 $HOME is first priority on *ALL* platforms.
178
178
179 Parameters
179 Parameters
180 ----------
180 ----------
181
181
182 require_writable : bool [default: False]
182 require_writable : bool [default: False]
183 if True:
183 if True:
184 guarantees the return value is a writable directory, otherwise
184 guarantees the return value is a writable directory, otherwise
185 raises HomeDirError
185 raises HomeDirError
186 if False:
186 if False:
187 The path is resolved, but it is not guaranteed to exist or be writable.
187 The path is resolved, but it is not guaranteed to exist or be writable.
188 """
188 """
189
189
190 # first, check py2exe distribution root directory for _ipython.
190 # first, check py2exe distribution root directory for _ipython.
191 # This overrides all. Normally does not exist.
191 # This overrides all. Normally does not exist.
192
192
193 if hasattr(sys, "frozen"): #Is frozen by py2exe
193 if hasattr(sys, "frozen"): #Is frozen by py2exe
194 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
194 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
195 root, rest = IPython.__file__.lower().split('library.zip')
195 root, rest = IPython.__file__.lower().split('library.zip')
196 else:
196 else:
197 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
197 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
198 root=os.path.abspath(root).rstrip('\\')
198 root=os.path.abspath(root).rstrip('\\')
199 if _writable_dir(os.path.join(root, '_ipython')):
199 if _writable_dir(os.path.join(root, '_ipython')):
200 os.environ["IPYKITROOT"] = root
200 os.environ["IPYKITROOT"] = root
201 return py3compat.cast_unicode(root, fs_encoding)
201 return py3compat.cast_unicode(root, fs_encoding)
202
202
203 homedir = os.path.expanduser('~')
203 homedir = os.path.expanduser('~')
204 # Next line will make things work even when /home/ is a symlink to
205 # /usr/home as it is on FreeBSD, for example
206 homedir = os.path.realpath(homedir)
204
207
205 if not _writable_dir(homedir) and os.name == 'nt':
208 if not _writable_dir(homedir) and os.name == 'nt':
206 # expanduser failed, use the registry to get the 'My Documents' folder.
209 # expanduser failed, use the registry to get the 'My Documents' folder.
207 try:
210 try:
208 import _winreg as wreg
211 import _winreg as wreg
209 key = wreg.OpenKey(
212 key = wreg.OpenKey(
210 wreg.HKEY_CURRENT_USER,
213 wreg.HKEY_CURRENT_USER,
211 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
214 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
212 )
215 )
213 homedir = wreg.QueryValueEx(key,'Personal')[0]
216 homedir = wreg.QueryValueEx(key,'Personal')[0]
214 key.Close()
217 key.Close()
215 except:
218 except:
216 pass
219 pass
217
220
218 if (not require_writable) or _writable_dir(homedir):
221 if (not require_writable) or _writable_dir(homedir):
219 return py3compat.cast_unicode(homedir, fs_encoding)
222 return py3compat.cast_unicode(homedir, fs_encoding)
220 else:
223 else:
221 raise HomeDirError('%s is not a writable dir, '
224 raise HomeDirError('%s is not a writable dir, '
222 'set $HOME environment variable to override' % homedir)
225 'set $HOME environment variable to override' % homedir)
223
226
224 def get_xdg_dir():
227 def get_xdg_dir():
225 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
228 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
226
229
227 This is only for posix (Linux,Unix,OS X, etc) systems.
230 This is only for posix (Linux,Unix,OS X, etc) systems.
228 """
231 """
229
232
230 env = os.environ
233 env = os.environ
231
234
232 if os.name == 'posix':
235 if os.name == 'posix':
233 # Linux, Unix, AIX, OS X
236 # Linux, Unix, AIX, OS X
234 # use ~/.config if empty OR not set
237 # use ~/.config if empty OR not set
235 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
238 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
236 if xdg and _writable_dir(xdg):
239 if xdg and _writable_dir(xdg):
237 return py3compat.cast_unicode(xdg, fs_encoding)
240 return py3compat.cast_unicode(xdg, fs_encoding)
238
241
239 return None
242 return None
240
243
241
244
242 def get_ipython_dir():
245 def get_ipython_dir():
243 """Get the IPython directory for this platform and user.
246 """Get the IPython directory for this platform and user.
244
247
245 This uses the logic in `get_home_dir` to find the home directory
248 This uses the logic in `get_home_dir` to find the home directory
246 and then adds .ipython to the end of the path.
249 and then adds .ipython to the end of the path.
247 """
250 """
248
251
249 env = os.environ
252 env = os.environ
250 pjoin = os.path.join
253 pjoin = os.path.join
251
254
252
255
253 ipdir_def = '.ipython'
256 ipdir_def = '.ipython'
254 xdg_def = 'ipython'
257 xdg_def = 'ipython'
255
258
256 home_dir = get_home_dir()
259 home_dir = get_home_dir()
257 xdg_dir = get_xdg_dir()
260 xdg_dir = get_xdg_dir()
258
261
259 # import pdb; pdb.set_trace() # dbg
262 # import pdb; pdb.set_trace() # dbg
260 ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None))
263 ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None))
261 if ipdir is None:
264 if ipdir is None:
262 # not set explicitly, use XDG_CONFIG_HOME or HOME
265 # not set explicitly, use XDG_CONFIG_HOME or HOME
263 home_ipdir = pjoin(home_dir, ipdir_def)
266 home_ipdir = pjoin(home_dir, ipdir_def)
264 if xdg_dir:
267 if xdg_dir:
265 # use XDG, as long as the user isn't already
268 # use XDG, as long as the user isn't already
266 # using $HOME/.ipython and *not* XDG/ipython
269 # using $HOME/.ipython and *not* XDG/ipython
267
270
268 xdg_ipdir = pjoin(xdg_dir, xdg_def)
271 xdg_ipdir = pjoin(xdg_dir, xdg_def)
269
272
270 if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir):
273 if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir):
271 ipdir = xdg_ipdir
274 ipdir = xdg_ipdir
272
275
273 if ipdir is None:
276 if ipdir is None:
274 # not using XDG
277 # not using XDG
275 ipdir = home_ipdir
278 ipdir = home_ipdir
276
279
277 ipdir = os.path.normpath(os.path.expanduser(ipdir))
280 ipdir = os.path.normpath(os.path.expanduser(ipdir))
278
281
279 if os.path.exists(ipdir) and not _writable_dir(ipdir):
282 if os.path.exists(ipdir) and not _writable_dir(ipdir):
280 # ipdir exists, but is not writable
283 # ipdir exists, but is not writable
281 warnings.warn("IPython dir '%s' is not a writable location,"
284 warnings.warn("IPython dir '%s' is not a writable location,"
282 " using a temp directory."%ipdir)
285 " using a temp directory."%ipdir)
283 ipdir = tempfile.mkdtemp()
286 ipdir = tempfile.mkdtemp()
284 elif not os.path.exists(ipdir):
287 elif not os.path.exists(ipdir):
285 parent = ipdir.rsplit(os.path.sep, 1)[0]
288 parent = ipdir.rsplit(os.path.sep, 1)[0]
286 if not _writable_dir(parent):
289 if not _writable_dir(parent):
287 # ipdir does not exist and parent isn't writable
290 # ipdir does not exist and parent isn't writable
288 warnings.warn("IPython parent '%s' is not a writable location,"
291 warnings.warn("IPython parent '%s' is not a writable location,"
289 " using a temp directory."%parent)
292 " using a temp directory."%parent)
290 ipdir = tempfile.mkdtemp()
293 ipdir = tempfile.mkdtemp()
291
294
292 return py3compat.cast_unicode(ipdir, fs_encoding)
295 return py3compat.cast_unicode(ipdir, fs_encoding)
293
296
294
297
295 def get_ipython_package_dir():
298 def get_ipython_package_dir():
296 """Get the base directory where IPython itself is installed."""
299 """Get the base directory where IPython itself is installed."""
297 ipdir = os.path.dirname(IPython.__file__)
300 ipdir = os.path.dirname(IPython.__file__)
298 return py3compat.cast_unicode(ipdir, fs_encoding)
301 return py3compat.cast_unicode(ipdir, fs_encoding)
299
302
300
303
301 def get_ipython_module_path(module_str):
304 def get_ipython_module_path(module_str):
302 """Find the path to an IPython module in this version of IPython.
305 """Find the path to an IPython module in this version of IPython.
303
306
304 This will always find the version of the module that is in this importable
307 This will always find the version of the module that is in this importable
305 IPython package. This will always return the path to the ``.py``
308 IPython package. This will always return the path to the ``.py``
306 version of the module.
309 version of the module.
307 """
310 """
308 if module_str == 'IPython':
311 if module_str == 'IPython':
309 return os.path.join(get_ipython_package_dir(), '__init__.py')
312 return os.path.join(get_ipython_package_dir(), '__init__.py')
310 mod = import_item(module_str)
313 mod = import_item(module_str)
311 the_path = mod.__file__.replace('.pyc', '.py')
314 the_path = mod.__file__.replace('.pyc', '.py')
312 the_path = the_path.replace('.pyo', '.py')
315 the_path = the_path.replace('.pyo', '.py')
313 return py3compat.cast_unicode(the_path, fs_encoding)
316 return py3compat.cast_unicode(the_path, fs_encoding)
314
317
315 def locate_profile(profile='default'):
318 def locate_profile(profile='default'):
316 """Find the path to the folder associated with a given profile.
319 """Find the path to the folder associated with a given profile.
317
320
318 I.e. find $IPYTHON_DIR/profile_whatever.
321 I.e. find $IPYTHON_DIR/profile_whatever.
319 """
322 """
320 from IPython.core.profiledir import ProfileDir, ProfileDirError
323 from IPython.core.profiledir import ProfileDir, ProfileDirError
321 try:
324 try:
322 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
325 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
323 except ProfileDirError:
326 except ProfileDirError:
324 # IOError makes more sense when people are expecting a path
327 # IOError makes more sense when people are expecting a path
325 raise IOError("Couldn't find profile %r" % profile)
328 raise IOError("Couldn't find profile %r" % profile)
326 return pd.location
329 return pd.location
327
330
328 def expand_path(s):
331 def expand_path(s):
329 """Expand $VARS and ~names in a string, like a shell
332 """Expand $VARS and ~names in a string, like a shell
330
333
331 :Examples:
334 :Examples:
332
335
333 In [2]: os.environ['FOO']='test'
336 In [2]: os.environ['FOO']='test'
334
337
335 In [3]: expand_path('variable FOO is $FOO')
338 In [3]: expand_path('variable FOO is $FOO')
336 Out[3]: 'variable FOO is test'
339 Out[3]: 'variable FOO is test'
337 """
340 """
338 # This is a pretty subtle hack. When expand user is given a UNC path
341 # This is a pretty subtle hack. When expand user is given a UNC path
339 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
342 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
340 # the $ to get (\\server\share\%username%). I think it considered $
343 # the $ to get (\\server\share\%username%). I think it considered $
341 # alone an empty var. But, we need the $ to remains there (it indicates
344 # alone an empty var. But, we need the $ to remains there (it indicates
342 # a hidden share).
345 # a hidden share).
343 if os.name=='nt':
346 if os.name=='nt':
344 s = s.replace('$\\', 'IPYTHON_TEMP')
347 s = s.replace('$\\', 'IPYTHON_TEMP')
345 s = os.path.expandvars(os.path.expanduser(s))
348 s = os.path.expandvars(os.path.expanduser(s))
346 if os.name=='nt':
349 if os.name=='nt':
347 s = s.replace('IPYTHON_TEMP', '$\\')
350 s = s.replace('IPYTHON_TEMP', '$\\')
348 return s
351 return s
349
352
350
353
351 def target_outdated(target,deps):
354 def target_outdated(target,deps):
352 """Determine whether a target is out of date.
355 """Determine whether a target is out of date.
353
356
354 target_outdated(target,deps) -> 1/0
357 target_outdated(target,deps) -> 1/0
355
358
356 deps: list of filenames which MUST exist.
359 deps: list of filenames which MUST exist.
357 target: single filename which may or may not exist.
360 target: single filename which may or may not exist.
358
361
359 If target doesn't exist or is older than any file listed in deps, return
362 If target doesn't exist or is older than any file listed in deps, return
360 true, otherwise return false.
363 true, otherwise return false.
361 """
364 """
362 try:
365 try:
363 target_time = os.path.getmtime(target)
366 target_time = os.path.getmtime(target)
364 except os.error:
367 except os.error:
365 return 1
368 return 1
366 for dep in deps:
369 for dep in deps:
367 dep_time = os.path.getmtime(dep)
370 dep_time = os.path.getmtime(dep)
368 if dep_time > target_time:
371 if dep_time > target_time:
369 #print "For target",target,"Dep failed:",dep # dbg
372 #print "For target",target,"Dep failed:",dep # dbg
370 #print "times (dep,tar):",dep_time,target_time # dbg
373 #print "times (dep,tar):",dep_time,target_time # dbg
371 return 1
374 return 1
372 return 0
375 return 0
373
376
374
377
375 def target_update(target,deps,cmd):
378 def target_update(target,deps,cmd):
376 """Update a target with a given command given a list of dependencies.
379 """Update a target with a given command given a list of dependencies.
377
380
378 target_update(target,deps,cmd) -> runs cmd if target is outdated.
381 target_update(target,deps,cmd) -> runs cmd if target is outdated.
379
382
380 This is just a wrapper around target_outdated() which calls the given
383 This is just a wrapper around target_outdated() which calls the given
381 command if target is outdated."""
384 command if target is outdated."""
382
385
383 if target_outdated(target,deps):
386 if target_outdated(target,deps):
384 system(cmd)
387 system(cmd)
385
388
386 def filehash(path):
389 def filehash(path):
387 """Make an MD5 hash of a file, ignoring any differences in line
390 """Make an MD5 hash of a file, ignoring any differences in line
388 ending characters."""
391 ending characters."""
389 with open(path, "rU") as f:
392 with open(path, "rU") as f:
390 return md5(py3compat.str_to_bytes(f.read())).hexdigest()
393 return md5(py3compat.str_to_bytes(f.read())).hexdigest()
391
394
392 # If the config is unmodified from the default, we'll just delete it.
395 # If the config is unmodified from the default, we'll just delete it.
393 # These are consistent for 0.10.x, thankfully. We're not going to worry about
396 # These are consistent for 0.10.x, thankfully. We're not going to worry about
394 # older versions.
397 # older versions.
395 old_config_md5 = {'ipy_user_conf.py': 'fc108bedff4b9a00f91fa0a5999140d3',
398 old_config_md5 = {'ipy_user_conf.py': 'fc108bedff4b9a00f91fa0a5999140d3',
396 'ipythonrc': '12a68954f3403eea2eec09dc8fe5a9b5'}
399 'ipythonrc': '12a68954f3403eea2eec09dc8fe5a9b5'}
397
400
398 def check_for_old_config(ipython_dir=None):
401 def check_for_old_config(ipython_dir=None):
399 """Check for old config files, and present a warning if they exist.
402 """Check for old config files, and present a warning if they exist.
400
403
401 A link to the docs of the new config is included in the message.
404 A link to the docs of the new config is included in the message.
402
405
403 This should mitigate confusion with the transition to the new
406 This should mitigate confusion with the transition to the new
404 config system in 0.11.
407 config system in 0.11.
405 """
408 """
406 if ipython_dir is None:
409 if ipython_dir is None:
407 ipython_dir = get_ipython_dir()
410 ipython_dir = get_ipython_dir()
408
411
409 old_configs = ['ipy_user_conf.py', 'ipythonrc', 'ipython_config.py']
412 old_configs = ['ipy_user_conf.py', 'ipythonrc', 'ipython_config.py']
410 warned = False
413 warned = False
411 for cfg in old_configs:
414 for cfg in old_configs:
412 f = os.path.join(ipython_dir, cfg)
415 f = os.path.join(ipython_dir, cfg)
413 if os.path.exists(f):
416 if os.path.exists(f):
414 if filehash(f) == old_config_md5.get(cfg, ''):
417 if filehash(f) == old_config_md5.get(cfg, ''):
415 os.unlink(f)
418 os.unlink(f)
416 else:
419 else:
417 warnings.warn("Found old IPython config file %r (modified by user)"%f)
420 warnings.warn("Found old IPython config file %r (modified by user)"%f)
418 warned = True
421 warned = True
419
422
420 if warned:
423 if warned:
421 warnings.warn("""
424 warnings.warn("""
422 The IPython configuration system has changed as of 0.11, and these files will
425 The IPython configuration system has changed as of 0.11, and these files will
423 be ignored. See http://ipython.github.com/ipython-doc/dev/config for details
426 be ignored. See http://ipython.github.com/ipython-doc/dev/config for details
424 of the new config system.
427 of the new config system.
425 To start configuring IPython, do `ipython profile create`, and edit
428 To start configuring IPython, do `ipython profile create`, and edit
426 `ipython_config.py` in <ipython_dir>/profile_default.
429 `ipython_config.py` in <ipython_dir>/profile_default.
427 If you need to leave the old config files in place for an older version of
430 If you need to leave the old config files in place for an older version of
428 IPython and want to suppress this warning message, set
431 IPython and want to suppress this warning message, set
429 `c.InteractiveShellApp.ignore_old_config=True` in the new config.""")
432 `c.InteractiveShellApp.ignore_old_config=True` in the new config.""")
430
433
431 def get_security_file(filename, profile='default'):
434 def get_security_file(filename, profile='default'):
432 """Return the absolute path of a security file given by filename and profile
435 """Return the absolute path of a security file given by filename and profile
433
436
434 This allows users and developers to find security files without
437 This allows users and developers to find security files without
435 knowledge of the IPython directory structure. The search path
438 knowledge of the IPython directory structure. The search path
436 will be ['.', profile.security_dir]
439 will be ['.', profile.security_dir]
437
440
438 Parameters
441 Parameters
439 ----------
442 ----------
440
443
441 filename : str
444 filename : str
442 The file to be found. If it is passed as an absolute path, it will
445 The file to be found. If it is passed as an absolute path, it will
443 simply be returned.
446 simply be returned.
444 profile : str [default: 'default']
447 profile : str [default: 'default']
445 The name of the profile to search. Leaving this unspecified
448 The name of the profile to search. Leaving this unspecified
446 The file to be found. If it is passed as an absolute path, fname will
449 The file to be found. If it is passed as an absolute path, fname will
447 simply be returned.
450 simply be returned.
448
451
449 Returns
452 Returns
450 -------
453 -------
451 Raises :exc:`IOError` if file not found or returns absolute path to file.
454 Raises :exc:`IOError` if file not found or returns absolute path to file.
452 """
455 """
453 # import here, because profiledir also imports from utils.path
456 # import here, because profiledir also imports from utils.path
454 from IPython.core.profiledir import ProfileDir
457 from IPython.core.profiledir import ProfileDir
455 try:
458 try:
456 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
459 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
457 except Exception:
460 except Exception:
458 # will raise ProfileDirError if no such profile
461 # will raise ProfileDirError if no such profile
459 raise IOError("Profile %r not found")
462 raise IOError("Profile %r not found")
460 return filefind(filename, ['.', pd.security_dir])
463 return filefind(filename, ['.', pd.security_dir])
461
464
General Comments 0
You need to be logged in to leave comments. Login now