##// END OF EJS Templates
rcutil: extract rc directory listing logic...
Jun Wu -
r31681:294728f2 default
parent child Browse files
Show More
@@ -1,62 +1,62 b''
1 1 # rcutil.py - utilities about config paths, special config sections etc.
2 2 #
3 3 # Copyright Mercurial Contributors
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import os
11 11
12 12 from . import (
13 13 encoding,
14 14 osutil,
15 15 pycompat,
16 16 util,
17 17 )
18 18
19 19 if pycompat.osname == 'nt':
20 20 from . import scmwindows as scmplatform
21 21 else:
22 22 from . import scmposix as scmplatform
23 23
24 24 systemrcpath = scmplatform.systemrcpath
25 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 35 def defaultrcpath():
28 36 '''return rc paths in default.d'''
29 37 path = []
30 38 defaultpath = os.path.join(util.datapath, 'default.d')
31 39 if os.path.isdir(defaultpath):
32 for f, kind in osutil.listdir(defaultpath):
33 if f.endswith('.rc'):
34 path.append(os.path.join(defaultpath, f))
40 path = _expandrcpath(defaultpath)
35 41 return path
36 42
37 43 _rcpath = None
38 44
39 45 def rcpath():
40 46 '''return hgrc search path. if env var HGRCPATH is set, use it.
41 47 for each item in path, if directory, use files ending in .rc,
42 48 else use item.
43 49 make HGRCPATH empty to only look in .hg/hgrc of current repo.
44 50 if no HGRCPATH, use default os-specific path.'''
45 51 global _rcpath
46 52 if _rcpath is None:
47 53 if 'HGRCPATH' in encoding.environ:
48 54 _rcpath = []
49 55 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
50 56 if not p:
51 57 continue
52 p = util.expandpath(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)
58 _rcpath.extend(_expandrcpath(p))
59 59 else:
60 60 paths = defaultrcpath() + systemrcpath() + userrcpath()
61 61 _rcpath = pycompat.maplist(os.path.normpath, paths)
62 62 return _rcpath
General Comments 0
You need to be logged in to leave comments. Login now