##// END OF EJS Templates
rcutil: add a method to convert environment variables to config items...
Jun Wu -
r31684:0be96ac9 default
parent child Browse files
Show More
@@ -1,68 +1,90
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):
27 def _expandrcpath(path):
28 '''path could be a file or a directory. return a list of file paths'''
28 '''path could be a file or a directory. return a list of file paths'''
29 p = util.expandpath(path)
29 p = util.expandpath(path)
30 if os.path.isdir(p):
30 if os.path.isdir(p):
31 join = os.path.join
31 join = os.path.join
32 return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
32 return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
33 return [p]
33 return [p]
34
34
35 def envrcitems(env=None):
36 '''Return [(section, name, value, source)] config items.
37
38 The config items are extracted from environment variables specified by env,
39 used to override systemrc, but not userrc.
40
41 If env is not provided, encoding.environ will be used.
42 '''
43 if env is None:
44 env = encoding.environ
45 checklist = [
46 ('EDITOR', 'ui', 'editor'),
47 ('VISUAL', 'ui', 'editor'),
48 ('PAGER', 'pager', 'pager'),
49 ]
50 result = []
51 for envname, section, configname in checklist:
52 if envname not in env:
53 continue
54 result.append((section, configname, env[envname], '$%s' % envname))
55 return result
56
35 def defaultrcpath():
57 def defaultrcpath():
36 '''return rc paths in default.d'''
58 '''return rc paths in default.d'''
37 path = []
59 path = []
38 defaultpath = os.path.join(util.datapath, 'default.d')
60 defaultpath = os.path.join(util.datapath, 'default.d')
39 if os.path.isdir(defaultpath):
61 if os.path.isdir(defaultpath):
40 path = _expandrcpath(defaultpath)
62 path = _expandrcpath(defaultpath)
41 return path
63 return path
42
64
43 _rccomponents = None
65 _rccomponents = None
44
66
45 def rccomponents():
67 def rccomponents():
46 '''return an ordered [(type, obj)] about where to load configs.
68 '''return an ordered [(type, obj)] about where to load configs.
47
69
48 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
70 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
49 used. if $HGRCPATH is not set, the platform default will be used.
71 used. if $HGRCPATH is not set, the platform default will be used.
50
72
51 if a directory is provided, *.rc files under it will be used.
73 if a directory is provided, *.rc files under it will be used.
52
74
53 type could be either 'path' or 'items', if type is 'path', obj is a string,
75 type could be either 'path' or 'items', if type is 'path', obj is a string,
54 and is the config file path. if type is 'items', obj is a list of (section,
76 and is the config file path. if type is 'items', obj is a list of (section,
55 name, value, source) that should fill the config directly.
77 name, value, source) that should fill the config directly.
56 '''
78 '''
57 global _rccomponents
79 global _rccomponents
58 if _rccomponents is None:
80 if _rccomponents is None:
59 if 'HGRCPATH' in encoding.environ:
81 if 'HGRCPATH' in encoding.environ:
60 _rccomponents = []
82 _rccomponents = []
61 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
83 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
62 if not p:
84 if not p:
63 continue
85 continue
64 _rccomponents.extend(('path', p) for p in _expandrcpath(p))
86 _rccomponents.extend(('path', p) for p in _expandrcpath(p))
65 else:
87 else:
66 paths = defaultrcpath() + systemrcpath() + userrcpath()
88 paths = defaultrcpath() + systemrcpath() + userrcpath()
67 _rccomponents = [('path', os.path.normpath(p)) for p in paths]
89 _rccomponents = [('path', os.path.normpath(p)) for p in paths]
68 return _rccomponents
90 return _rccomponents
General Comments 0
You need to be logged in to leave comments. Login now