author | Martin von Zweigbergk <martinvonz@google.com> |
Tue, 18 Nov 2014 20:29:25 -0800 | |
changeset 23652 | 6fcc3669e483 |
parent 23602 | a4679a74df14 |
child 25011 | 7d6a507a4c53 |
permissions | -rw-r--r-- |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
1 |
import os, errno, stat |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
2 |
|
23598
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
3 |
import encoding |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
4 |
import util |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
5 |
from i18n import _ |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
6 |
|
23598
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
7 |
def _lowerclean(s): |
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
8 |
return encoding.hfsignoreclean(s.lower()) |
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
9 |
|
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
10 |
class pathauditor(object): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
11 |
'''ensure that a filesystem path contains no banned components. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
12 |
the following properties of a path are checked: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
13 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
14 |
- ends with a directory separator |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
15 |
- under top-level .hg |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
16 |
- starts at the root of a windows drive |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
17 |
- contains ".." |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
18 |
- traverses a symlink (e.g. a/symlink_here/b) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
19 |
- inside a nested repository (a callback can be used to approve |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
20 |
some nested repositories, e.g., subrepositories) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
21 |
''' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
22 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
23 |
def __init__(self, root, callback=None): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
24 |
self.audited = set() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
25 |
self.auditeddir = set() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
26 |
self.root = root |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
27 |
self.callback = callback |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
28 |
if os.path.lexists(root) and not util.checkcase(root): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
29 |
self.normcase = util.normcase |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
30 |
else: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
31 |
self.normcase = lambda x: x |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
32 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
33 |
def __call__(self, path): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
34 |
'''Check the relative path. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
35 |
path may contain a pattern (e.g. foodir/**.txt)''' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
36 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
37 |
path = util.localpath(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
38 |
normpath = self.normcase(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
39 |
if normpath in self.audited: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
40 |
return |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
41 |
# AIX ignores "/" at end of path, others raise EISDIR. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
42 |
if util.endswithsep(path): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
43 |
raise util.Abort(_("path ends in directory separator: %s") % path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
44 |
parts = util.splitpath(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
45 |
if (os.path.splitdrive(path)[0] |
23598
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
46 |
or _lowerclean(parts[0]) in ('.hg', '.hg.', '') |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
47 |
or os.pardir in parts): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
48 |
raise util.Abort(_("path contains illegal component: %s") % path) |
23599
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
49 |
# Windows shortname aliases |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
50 |
for p in parts: |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
51 |
if "~" in p: |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
52 |
first, last = p.split("~", 1) |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
53 |
if last.isdigit() and first.upper() in ["HG", "HG8B6C"]: |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
54 |
raise util.Abort(_("path contains illegal component: %s") |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
55 |
% path) |
23598
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
56 |
if '.hg' in _lowerclean(path): |
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
57 |
lparts = [_lowerclean(p.lower()) for p in parts] |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
58 |
for p in '.hg', '.hg.': |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
59 |
if p in lparts[1:]: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
60 |
pos = lparts.index(p) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
61 |
base = os.path.join(*parts[:pos]) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
62 |
raise util.Abort(_("path '%s' is inside nested repo %r") |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
63 |
% (path, base)) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
64 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
65 |
normparts = util.splitpath(normpath) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
66 |
assert len(parts) == len(normparts) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
67 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
68 |
parts.pop() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
69 |
normparts.pop() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
70 |
prefixes = [] |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
71 |
while parts: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
72 |
prefix = os.sep.join(parts) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
73 |
normprefix = os.sep.join(normparts) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
74 |
if normprefix in self.auditeddir: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
75 |
break |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
76 |
curpath = os.path.join(self.root, prefix) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
77 |
try: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
78 |
st = os.lstat(curpath) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
79 |
except OSError, err: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
80 |
# EINVAL can be raised as invalid path syntax under win32. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
81 |
# They must be ignored for patterns can be checked too. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
82 |
if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
83 |
raise |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
84 |
else: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
85 |
if stat.S_ISLNK(st.st_mode): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
86 |
raise util.Abort( |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
87 |
_('path %r traverses symbolic link %r') |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
88 |
% (path, prefix)) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
89 |
elif (stat.S_ISDIR(st.st_mode) and |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
90 |
os.path.isdir(os.path.join(curpath, '.hg'))): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
91 |
if not self.callback or not self.callback(curpath): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
92 |
raise util.Abort(_("path '%s' is inside nested " |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
93 |
"repo %r") |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
94 |
% (path, prefix)) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
95 |
prefixes.append(normprefix) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
96 |
parts.pop() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
97 |
normparts.pop() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
98 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
99 |
self.audited.add(normpath) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
100 |
# only add prefixes to the cache after checking everything: we don't |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
101 |
# want to add "foo/bar/baz" before checking if there's a "foo/.hg" |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
102 |
self.auditeddir.update(prefixes) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
103 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
104 |
def check(self, path): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
105 |
try: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
106 |
self(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
107 |
return True |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
108 |
except (OSError, util.Abort): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
109 |
return False |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
110 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
111 |
def canonpath(root, cwd, myname, auditor=None): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
112 |
'''return the canonical path of myname, given cwd and root''' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
113 |
if util.endswithsep(root): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
114 |
rootsep = root |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
115 |
else: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
116 |
rootsep = root + os.sep |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
117 |
name = myname |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
118 |
if not os.path.isabs(name): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
119 |
name = os.path.join(root, cwd, name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
120 |
name = os.path.normpath(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
121 |
if auditor is None: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
122 |
auditor = pathauditor(root) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
123 |
if name != rootsep and name.startswith(rootsep): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
124 |
name = name[len(rootsep):] |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
125 |
auditor(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
126 |
return util.pconvert(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
127 |
elif name == root: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
128 |
return '' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
129 |
else: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
130 |
# Determine whether `name' is in the hierarchy at or beneath `root', |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
131 |
# by iterating name=dirname(name) until that causes no change (can't |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
132 |
# check name == '/', because that doesn't work on windows). The list |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
133 |
# `rel' holds the reversed list of components making up the relative |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
134 |
# file name we want. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
135 |
rel = [] |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
136 |
while True: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
137 |
try: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
138 |
s = util.samefile(name, root) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
139 |
except OSError: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
140 |
s = False |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
141 |
if s: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
142 |
if not rel: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
143 |
# name was actually the same as root (maybe a symlink) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
144 |
return '' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
145 |
rel.reverse() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
146 |
name = os.path.join(*rel) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
147 |
auditor(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
148 |
return util.pconvert(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
149 |
dirname, basename = util.split(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
150 |
rel.append(basename) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
151 |
if dirname == name: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
152 |
break |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
153 |
name = dirname |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
154 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
155 |
raise util.Abort(_("%s not under root '%s'") % (myname, root)) |
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
156 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
157 |
def normasprefix(path): |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
158 |
'''normalize the specified path as path prefix |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
159 |
|
23139
e53f6b72a0e4
spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents:
21568
diff
changeset
|
160 |
Returned value can be used safely for "p.startswith(prefix)", |
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
161 |
"p[len(prefix):]", and so on. |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
162 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
163 |
For efficiency, this expects "path" argument to be already |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
164 |
normalized by "os.path.normpath", "os.path.realpath", and so on. |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
165 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
166 |
See also issue3033 for detail about need of this function. |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
167 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
168 |
>>> normasprefix('/foo/bar').replace(os.sep, '/') |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
169 |
'/foo/bar/' |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
170 |
>>> normasprefix('/').replace(os.sep, '/') |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
171 |
'/' |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
172 |
''' |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
173 |
d, p = os.path.splitdrive(path) |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
174 |
if len(p) != len(os.sep): |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
175 |
return path + os.sep |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
176 |
else: |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
177 |
return path |