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