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