-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindup.py
More file actions
executable file
·85 lines (61 loc) · 2.22 KB
/
findup.py
File metadata and controls
executable file
·85 lines (61 loc) · 2.22 KB
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
'''
'''
__version__ = '0.3.0'
import os
import glob as globmodule
def glob(pattern, dirname=None):
'''
Find the first path matching a given pattern within dirname or the nearest
ancestor of dirname. Walk up the directory tree, starting from
`dirname` and return the first path found that matches the glob
pattern, using glob.glob(). If no path is found, return None.
:param pattern: a glob pattern that will be appended to (a normalized)
dirname.
:param dirname: String. A directory path. Defaults to the current working
directory. This path will be "normalized" using os.path.expanduser and
os.path.abspath.
For example, findup.glob('.git') would find the path to the nearest '.git'
directory in the current working dir or above it.
To find the root dir from anywhere within a git repository, do:
os.path.dirname(findup.glob('.git'))
Or more robustly:
path = findup.glob('.git')
git_root = None if path is None or os.path.dirname(path)
Raises a ValueError if dirname is not the name of a directory.
'''
if dirname is None:
normdn = normalize_path(os.getcwd())
else:
normdn = normalize_path(dirname)
if not os.path.isdir(normdn):
raise ValueError('Parameter dirname must be a directory path.', dirname)
for d in walk_up(normdn):
matches = globmodule.glob(os.path.join(d, pattern))
if matches:
return matches[0]
return None
#####################
# AUXILIARY FUNCTIONS
def normalize_path(path, realpath=False):
'''
Convert path that possibly contains a user tilde and/or is a relative
path into an absolute path.
'''
if realpath:
return os.path.realpath(os.path.expanduser(path))
else:
return os.path.abspath(os.path.expanduser(path))
def walk_up(path):
'''
Yield path and every directory above path.
Consider normalizing path, using `normalize_path(path)` first.
Example:
list(walk_up('/Users/td23/tmp')) ->
['/Users/td23/tmp', '/Users/td23', '/Users', '/']
'''
curr_path = path
while 1:
yield curr_path
curr_path, tail = os.path.split(curr_path)
if not tail:
break