Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/rcutil.py @ 31686:294728f2a908
rcutil: extract rc directory listing logic
The logic of listing a ".rc" directory is duplicated in two functions,
extract it to a single function to make the code cleaner.
author | Jun Wu <quark@fb.com> |
---|---|
date | Sun, 26 Mar 2017 20:46:05 -0700 |
parents | 448889f9a36c |
children | 07d62fa518a4 |
comparison
equal
deleted
inserted
replaced
31685:448889f9a36c | 31686:294728f2a908 |
---|---|
22 from . import scmposix as scmplatform | 22 from . import scmposix as scmplatform |
23 | 23 |
24 systemrcpath = scmplatform.systemrcpath | 24 systemrcpath = scmplatform.systemrcpath |
25 userrcpath = scmplatform.userrcpath | 25 userrcpath = scmplatform.userrcpath |
26 | 26 |
27 def _expandrcpath(path): | |
28 '''path could be a file or a directory. return a list of file paths''' | |
29 p = util.expandpath(path) | |
30 if os.path.isdir(p): | |
31 join = os.path.join | |
32 return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')] | |
33 return [p] | |
34 | |
27 def defaultrcpath(): | 35 def defaultrcpath(): |
28 '''return rc paths in default.d''' | 36 '''return rc paths in default.d''' |
29 path = [] | 37 path = [] |
30 defaultpath = os.path.join(util.datapath, 'default.d') | 38 defaultpath = os.path.join(util.datapath, 'default.d') |
31 if os.path.isdir(defaultpath): | 39 if os.path.isdir(defaultpath): |
32 for f, kind in osutil.listdir(defaultpath): | 40 path = _expandrcpath(defaultpath) |
33 if f.endswith('.rc'): | |
34 path.append(os.path.join(defaultpath, f)) | |
35 return path | 41 return path |
36 | 42 |
37 _rcpath = None | 43 _rcpath = None |
38 | 44 |
39 def rcpath(): | 45 def rcpath(): |
47 if 'HGRCPATH' in encoding.environ: | 53 if 'HGRCPATH' in encoding.environ: |
48 _rcpath = [] | 54 _rcpath = [] |
49 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep): | 55 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep): |
50 if not p: | 56 if not p: |
51 continue | 57 continue |
52 p = util.expandpath(p) | 58 _rcpath.extend(_expandrcpath(p)) |
53 if os.path.isdir(p): | |
54 for f, kind in osutil.listdir(p): | |
55 if f.endswith('.rc'): | |
56 _rcpath.append(os.path.join(p, f)) | |
57 else: | |
58 _rcpath.append(p) | |
59 else: | 59 else: |
60 paths = defaultrcpath() + systemrcpath() + userrcpath() | 60 paths = defaultrcpath() + systemrcpath() + userrcpath() |
61 _rcpath = pycompat.maplist(os.path.normpath, paths) | 61 _rcpath = pycompat.maplist(os.path.normpath, paths) |
62 return _rcpath | 62 return _rcpath |