##// END OF EJS Templates
rcutil: drop the `defaultrcpath()` method (API)...
Matt Harbison -
r44484:2d4cad94 default
parent child Browse files
Show More
@@ -1,120 +1,114 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 pycompat,
14 pycompat,
15 util,
15 util,
16 )
16 )
17
17
18 from .utils import resourceutil
18 from .utils import resourceutil
19
19
20 if pycompat.iswindows:
20 if pycompat.iswindows:
21 from . import scmwindows as scmplatform
21 from . import scmwindows as scmplatform
22 else:
22 else:
23 from . import scmposix as scmplatform
23 from . import scmposix as scmplatform
24
24
25 fallbackpager = scmplatform.fallbackpager
25 fallbackpager = scmplatform.fallbackpager
26 systemrcpath = scmplatform.systemrcpath
26 systemrcpath = scmplatform.systemrcpath
27 userrcpath = scmplatform.userrcpath
27 userrcpath = scmplatform.userrcpath
28
28
29
29
30 def _expandrcpath(path):
30 def _expandrcpath(path):
31 '''path could be a file or a directory. return a list of file paths'''
31 '''path could be a file or a directory. return a list of file paths'''
32 p = util.expandpath(path)
32 p = util.expandpath(path)
33 if os.path.isdir(p):
33 if os.path.isdir(p):
34 join = os.path.join
34 join = os.path.join
35 return sorted(
35 return sorted(
36 join(p, f) for f, k in util.listdir(p) if f.endswith(b'.rc')
36 join(p, f) for f, k in util.listdir(p) if f.endswith(b'.rc')
37 )
37 )
38 return [p]
38 return [p]
39
39
40
40
41 def envrcitems(env=None):
41 def envrcitems(env=None):
42 '''Return [(section, name, value, source)] config items.
42 '''Return [(section, name, value, source)] config items.
43
43
44 The config items are extracted from environment variables specified by env,
44 The config items are extracted from environment variables specified by env,
45 used to override systemrc, but not userrc.
45 used to override systemrc, but not userrc.
46
46
47 If env is not provided, encoding.environ will be used.
47 If env is not provided, encoding.environ will be used.
48 '''
48 '''
49 if env is None:
49 if env is None:
50 env = encoding.environ
50 env = encoding.environ
51 checklist = [
51 checklist = [
52 (b'EDITOR', b'ui', b'editor'),
52 (b'EDITOR', b'ui', b'editor'),
53 (b'VISUAL', b'ui', b'editor'),
53 (b'VISUAL', b'ui', b'editor'),
54 (b'PAGER', b'pager', b'pager'),
54 (b'PAGER', b'pager', b'pager'),
55 ]
55 ]
56 result = []
56 result = []
57 for envname, section, configname in checklist:
57 for envname, section, configname in checklist:
58 if envname not in env:
58 if envname not in env:
59 continue
59 continue
60 result.append((section, configname, env[envname], b'$%s' % envname))
60 result.append((section, configname, env[envname], b'$%s' % envname))
61 return result
61 return result
62
62
63
63
64 def defaultrcpath():
65 '''return rc paths in defaultrc'''
66 defaultpath = os.path.join(resourceutil.datapath, b'defaultrc')
67 return _expandrcpath(defaultpath)
68
69
70 def default_rc_resources():
64 def default_rc_resources():
71 """return rc resource IDs in defaultrc"""
65 """return rc resource IDs in defaultrc"""
72 rsrcs = resourceutil.contents(b'mercurial.defaultrc')
66 rsrcs = resourceutil.contents(b'mercurial.defaultrc')
73 return [
67 return [
74 (b'mercurial.defaultrc', r)
68 (b'mercurial.defaultrc', r)
75 for r in sorted(rsrcs)
69 for r in sorted(rsrcs)
76 if resourceutil.is_resource(b'mercurial.defaultrc', r)
70 if resourceutil.is_resource(b'mercurial.defaultrc', r)
77 and r.endswith(b'.rc')
71 and r.endswith(b'.rc')
78 ]
72 ]
79
73
80
74
81 def rccomponents():
75 def rccomponents():
82 '''return an ordered [(type, obj)] about where to load configs.
76 '''return an ordered [(type, obj)] about where to load configs.
83
77
84 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
78 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
85 used. if $HGRCPATH is not set, the platform default will be used.
79 used. if $HGRCPATH is not set, the platform default will be used.
86
80
87 if a directory is provided, *.rc files under it will be used.
81 if a directory is provided, *.rc files under it will be used.
88
82
89 type could be either 'path', 'items' or 'resource'. If type is 'path',
83 type could be either 'path', 'items' or 'resource'. If type is 'path',
90 obj is a string, and is the config file path. if type is 'items', obj is a
84 obj is a string, and is the config file path. if type is 'items', obj is a
91 list of (section, name, value, source) that should fill the config directly.
85 list of (section, name, value, source) that should fill the config directly.
92 If type is 'resource', obj is a tuple of (package name, resource name).
86 If type is 'resource', obj is a tuple of (package name, resource name).
93 '''
87 '''
94 envrc = (b'items', envrcitems())
88 envrc = (b'items', envrcitems())
95
89
96 if b'HGRCPATH' in encoding.environ:
90 if b'HGRCPATH' in encoding.environ:
97 # assume HGRCPATH is all about user configs so environments can be
91 # assume HGRCPATH is all about user configs so environments can be
98 # overridden.
92 # overridden.
99 _rccomponents = [envrc]
93 _rccomponents = [envrc]
100 for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep):
94 for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep):
101 if not p:
95 if not p:
102 continue
96 continue
103 _rccomponents.extend((b'path', p) for p in _expandrcpath(p))
97 _rccomponents.extend((b'path', p) for p in _expandrcpath(p))
104 else:
98 else:
105 _rccomponents = [(b'resource', r) for r in default_rc_resources()]
99 _rccomponents = [(b'resource', r) for r in default_rc_resources()]
106
100
107 normpaths = lambda paths: [
101 normpaths = lambda paths: [
108 (b'path', os.path.normpath(p)) for p in paths
102 (b'path', os.path.normpath(p)) for p in paths
109 ]
103 ]
110 _rccomponents.extend(normpaths(defaultrcpath() + systemrcpath()))
104 _rccomponents.extend(normpaths(systemrcpath()))
111 _rccomponents.append(envrc)
105 _rccomponents.append(envrc)
112 _rccomponents.extend(normpaths(userrcpath()))
106 _rccomponents.extend(normpaths(userrcpath()))
113 return _rccomponents
107 return _rccomponents
114
108
115
109
116 def defaultpagerenv():
110 def defaultpagerenv():
117 '''return a dict of default environment variables and their values,
111 '''return a dict of default environment variables and their values,
118 intended to be set before starting a pager.
112 intended to be set before starting a pager.
119 '''
113 '''
120 return {b'LESS': b'FRX', b'LV': b'-c'}
114 return {b'LESS': b'FRX', b'LV': b'-c'}
@@ -1,60 +1,59 b''
1 # Test the config layer generated by environment variables
1 # Test the config layer generated by environment variables
2
2
3 from __future__ import absolute_import, print_function
3 from __future__ import absolute_import, print_function
4
4
5 import os
5 import os
6
6
7 from mercurial import (
7 from mercurial import (
8 encoding,
8 encoding,
9 extensions,
9 extensions,
10 rcutil,
10 rcutil,
11 ui as uimod,
11 ui as uimod,
12 util,
12 util,
13 )
13 )
14
14
15 from mercurial.utils import procutil
15 from mercurial.utils import procutil
16
16
17 testtmp = encoding.environ[b'TESTTMP']
17 testtmp = encoding.environ[b'TESTTMP']
18
18
19 # prepare hgrc files
19 # prepare hgrc files
20 def join(name):
20 def join(name):
21 return os.path.join(testtmp, name)
21 return os.path.join(testtmp, name)
22
22
23
23
24 with open(join(b'sysrc'), 'wb') as f:
24 with open(join(b'sysrc'), 'wb') as f:
25 f.write(b'[ui]\neditor=e0\n[pager]\npager=p0\n')
25 f.write(b'[ui]\neditor=e0\n[pager]\npager=p0\n')
26
26
27 with open(join(b'userrc'), 'wb') as f:
27 with open(join(b'userrc'), 'wb') as f:
28 f.write(b'[ui]\neditor=e1')
28 f.write(b'[ui]\neditor=e1')
29
29
30 # replace rcpath functions so they point to the files above
30 # replace rcpath functions so they point to the files above
31 def systemrcpath():
31 def systemrcpath():
32 return [join(b'sysrc')]
32 return [join(b'sysrc')]
33
33
34
34
35 def userrcpath():
35 def userrcpath():
36 return [join(b'userrc')]
36 return [join(b'userrc')]
37
37
38
38
39 extensions.wrapfunction(rcutil, 'defaultrcpath', lambda orig: [])
40 extensions.wrapfunction(rcutil, 'default_rc_resources', lambda orig: [])
39 extensions.wrapfunction(rcutil, 'default_rc_resources', lambda orig: [])
41
40
42 rcutil.systemrcpath = systemrcpath
41 rcutil.systemrcpath = systemrcpath
43 rcutil.userrcpath = userrcpath
42 rcutil.userrcpath = userrcpath
44
43
45 # utility to print configs
44 # utility to print configs
46 def printconfigs(env):
45 def printconfigs(env):
47 encoding.environ = env
46 encoding.environ = env
48 rcutil._rccomponents = None # reset cache
47 rcutil._rccomponents = None # reset cache
49 ui = uimod.ui.load()
48 ui = uimod.ui.load()
50 for section, name, value in ui.walkconfig():
49 for section, name, value in ui.walkconfig():
51 source = ui.configsource(section, name)
50 source = ui.configsource(section, name)
52 procutil.stdout.write(
51 procutil.stdout.write(
53 b'%s.%s=%s # %s\n' % (section, name, value, util.pconvert(source))
52 b'%s.%s=%s # %s\n' % (section, name, value, util.pconvert(source))
54 )
53 )
55 procutil.stdout.write(b'\n')
54 procutil.stdout.write(b'\n')
56
55
57
56
58 # environment variable overrides
57 # environment variable overrides
59 printconfigs({})
58 printconfigs({})
60 printconfigs({b'EDITOR': b'e2', b'PAGER': b'p2'})
59 printconfigs({b'EDITOR': b'e2', b'PAGER': b'p2'})
General Comments 0
You need to be logged in to leave comments. Login now