Show More
@@ -1,109 +1,106 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(): |
|
64 | def defaultrcpath(): | |
65 | '''return rc paths in defaultrc''' |
|
65 | '''return rc paths in defaultrc''' | |
66 | path = [] |
|
|||
67 | defaultpath = os.path.join(resourceutil.datapath, b'defaultrc') |
|
66 | defaultpath = os.path.join(resourceutil.datapath, b'defaultrc') | |
68 |
|
|
67 | return _expandrcpath(defaultpath) | |
69 | path = _expandrcpath(defaultpath) |
|
|||
70 | return path |
|
|||
71 |
|
68 | |||
72 |
|
69 | |||
73 | def rccomponents(): |
|
70 | def rccomponents(): | |
74 | '''return an ordered [(type, obj)] about where to load configs. |
|
71 | '''return an ordered [(type, obj)] about where to load configs. | |
75 |
|
72 | |||
76 | respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is |
|
73 | respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is | |
77 | used. if $HGRCPATH is not set, the platform default will be used. |
|
74 | used. if $HGRCPATH is not set, the platform default will be used. | |
78 |
|
75 | |||
79 | if a directory is provided, *.rc files under it will be used. |
|
76 | if a directory is provided, *.rc files under it will be used. | |
80 |
|
77 | |||
81 | type could be either 'path' or 'items', if type is 'path', obj is a string, |
|
78 | type could be either 'path' or 'items', if type is 'path', obj is a string, | |
82 | and is the config file path. if type is 'items', obj is a list of (section, |
|
79 | and is the config file path. if type is 'items', obj is a list of (section, | |
83 | name, value, source) that should fill the config directly. |
|
80 | name, value, source) that should fill the config directly. | |
84 | ''' |
|
81 | ''' | |
85 | envrc = (b'items', envrcitems()) |
|
82 | envrc = (b'items', envrcitems()) | |
86 |
|
83 | |||
87 | if b'HGRCPATH' in encoding.environ: |
|
84 | if b'HGRCPATH' in encoding.environ: | |
88 | # assume HGRCPATH is all about user configs so environments can be |
|
85 | # assume HGRCPATH is all about user configs so environments can be | |
89 | # overridden. |
|
86 | # overridden. | |
90 | _rccomponents = [envrc] |
|
87 | _rccomponents = [envrc] | |
91 | for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep): |
|
88 | for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep): | |
92 | if not p: |
|
89 | if not p: | |
93 | continue |
|
90 | continue | |
94 | _rccomponents.extend((b'path', p) for p in _expandrcpath(p)) |
|
91 | _rccomponents.extend((b'path', p) for p in _expandrcpath(p)) | |
95 | else: |
|
92 | else: | |
96 | normpaths = lambda paths: [ |
|
93 | normpaths = lambda paths: [ | |
97 | (b'path', os.path.normpath(p)) for p in paths |
|
94 | (b'path', os.path.normpath(p)) for p in paths | |
98 | ] |
|
95 | ] | |
99 | _rccomponents = normpaths(defaultrcpath() + systemrcpath()) |
|
96 | _rccomponents = normpaths(defaultrcpath() + systemrcpath()) | |
100 | _rccomponents.append(envrc) |
|
97 | _rccomponents.append(envrc) | |
101 | _rccomponents.extend(normpaths(userrcpath())) |
|
98 | _rccomponents.extend(normpaths(userrcpath())) | |
102 | return _rccomponents |
|
99 | return _rccomponents | |
103 |
|
100 | |||
104 |
|
101 | |||
105 | def defaultpagerenv(): |
|
102 | def defaultpagerenv(): | |
106 | '''return a dict of default environment variables and their values, |
|
103 | '''return a dict of default environment variables and their values, | |
107 | intended to be set before starting a pager. |
|
104 | intended to be set before starting a pager. | |
108 | ''' |
|
105 | ''' | |
109 | return {b'LESS': b'FRX', b'LV': b'-c'} |
|
106 | return {b'LESS': b'FRX', b'LV': b'-c'} |
General Comments 0
You need to be logged in to leave comments.
Login now