##// 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 # rcutil.py - utilities about config paths, special config sections etc.
1 # rcutil.py - utilities about config paths, special config sections etc.
2 #
2 #
3 # Copyright Mercurial Contributors
3 # Copyright Mercurial Contributors
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import os
10 import os
11
11
12 from . import (
12 from . import (
13 encoding,
13 encoding,
14 osutil,
14 osutil,
15 pycompat,
15 pycompat,
16 util,
16 util,
17 )
17 )
18
18
19 if pycompat.osname == 'nt':
19 if pycompat.osname == 'nt':
20 from . import scmwindows as scmplatform
20 from . import scmwindows as scmplatform
21 else:
21 else:
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():
40 '''return hgrc search path. if env var HGRCPATH is set, use it.
46 '''return hgrc search path. if env var HGRCPATH is set, use it.
41 for each item in path, if directory, use files ending in .rc,
47 for each item in path, if directory, use files ending in .rc,
42 else use item.
48 else use item.
43 make HGRCPATH empty to only look in .hg/hgrc of current repo.
49 make HGRCPATH empty to only look in .hg/hgrc of current repo.
44 if no HGRCPATH, use default os-specific path.'''
50 if no HGRCPATH, use default os-specific path.'''
45 global _rcpath
51 global _rcpath
46 if _rcpath is None:
52 if _rcpath is None:
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
General Comments 0
You need to be logged in to leave comments. Login now