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