mercurial/rcutil.py
author Jun Wu <quark@fb.com>
Sun, 26 Mar 2017 21:33:37 -0700
changeset 31690 d83e51654c8a
parent 31689 0be96ac9199a
child 31697 cf052cc5c2c2
permissions -rw-r--r--
rcutil: let environ override system configs (BC) This is BC because system configs won't be able to override $EDITOR, $PAGER. The new behavior is arguably more rational.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     1
# rcutil.py - utilities about config paths, special config sections etc.
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     2
#
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     3
#  Copyright Mercurial Contributors
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     4
#
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     7
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     8
from __future__ import absolute_import
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     9
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    10
import os
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    11
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    12
from . import (
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    13
    encoding,
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    14
    osutil,
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    15
    pycompat,
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    16
    util,
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    17
)
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    18
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    19
if pycompat.osname == 'nt':
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    20
    from . import scmwindows as scmplatform
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    21
else:
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    22
    from . import scmposix as scmplatform
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    23
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    24
systemrcpath = scmplatform.systemrcpath
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    25
userrcpath = scmplatform.userrcpath
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    26
31686
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    27
def _expandrcpath(path):
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    28
    '''path could be a file or a directory. return a list of file paths'''
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    29
    p = util.expandpath(path)
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    30
    if os.path.isdir(p):
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    31
        join = os.path.join
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    32
        return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    33
    return [p]
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    34
31689
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    35
def envrcitems(env=None):
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    36
    '''Return [(section, name, value, source)] config items.
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    37
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    38
    The config items are extracted from environment variables specified by env,
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    39
    used to override systemrc, but not userrc.
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    40
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    41
    If env is not provided, encoding.environ will be used.
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    42
    '''
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    43
    if env is None:
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    44
        env = encoding.environ
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    45
    checklist = [
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    46
        ('EDITOR', 'ui', 'editor'),
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    47
        ('VISUAL', 'ui', 'editor'),
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    48
        ('PAGER', 'pager', 'pager'),
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    49
    ]
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    50
    result = []
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    51
    for envname, section, configname in checklist:
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    52
        if envname not in env:
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    53
            continue
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    54
        result.append((section, configname, env[envname], '$%s' % envname))
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    55
    return result
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31688
diff changeset
    56
31685
448889f9a36c rcutil: split osrcpath to return default.d paths (API)
Jun Wu <quark@fb.com>
parents: 31684
diff changeset
    57
def defaultrcpath():
448889f9a36c rcutil: split osrcpath to return default.d paths (API)
Jun Wu <quark@fb.com>
parents: 31684
diff changeset
    58
    '''return rc paths in default.d'''
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    59
    path = []
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    60
    defaultpath = os.path.join(util.datapath, 'default.d')
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    61
    if os.path.isdir(defaultpath):
31686
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31685
diff changeset
    62
        path = _expandrcpath(defaultpath)
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    63
    return path
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    64
31687
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31686
diff changeset
    65
_rccomponents = None
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    66
31687
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31686
diff changeset
    67
def rccomponents():
31688
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    68
    '''return an ordered [(type, obj)] about where to load configs.
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    69
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    70
    respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    71
    used. if $HGRCPATH is not set, the platform default will be used.
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    72
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    73
    if a directory is provided, *.rc files under it will be used.
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    74
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    75
    type could be either 'path' or 'items', if type is 'path', obj is a string,
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    76
    and is the config file path. if type is 'items', obj is a list of (section,
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    77
    name, value, source) that should fill the config directly.
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    78
    '''
31690
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    79
    envrc = ('items', envrcitems())
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    80
31687
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31686
diff changeset
    81
    global _rccomponents
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31686
diff changeset
    82
    if _rccomponents is None:
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    83
        if 'HGRCPATH' in encoding.environ:
31690
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    84
            # assume HGRCPATH is all about user configs so environments can be
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    85
            # overridden.
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    86
            _rccomponents = [envrc]
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    87
            for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    88
                if not p:
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    89
                    continue
31688
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    90
                _rccomponents.extend(('path', p) for p in _expandrcpath(p))
31684
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    91
        else:
31690
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    92
            paths = defaultrcpath() + systemrcpath()
31688
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31687
diff changeset
    93
            _rccomponents = [('path', os.path.normpath(p)) for p in paths]
31690
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    94
            _rccomponents.append(envrc)
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    95
            paths = userrcpath()
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31689
diff changeset
    96
            _rccomponents.extend(('path', os.path.normpath(p)) for p in paths)
31687
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31686
diff changeset
    97
    return _rccomponents