Show More
@@ -1,81 +1,113 b'' | |||
|
1 | 1 | from __future__ import absolute_import |
|
2 | 2 | |
|
3 | 3 | import os |
|
4 | 4 | |
|
5 | 5 | from . import ( |
|
6 | 6 | encoding, |
|
7 | 7 | pycompat, |
|
8 | 8 | util, |
|
9 | 9 | win32, |
|
10 | 10 | ) |
|
11 | 11 | |
|
12 | 12 | try: |
|
13 | 13 | import _winreg as winreg # pytype: disable=import-error |
|
14 | 14 | |
|
15 | 15 | winreg.CloseKey |
|
16 | 16 | except ImportError: |
|
17 | 17 | # py2 only |
|
18 | 18 | import winreg # pytype: disable=import-error |
|
19 | 19 | |
|
20 | 20 | # MS-DOS 'more' is the only pager available by default on Windows. |
|
21 | 21 | fallbackpager = b'more' |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | def systemrcpath(): |
|
25 | 25 | '''return default os-specific hgrc search path''' |
|
26 | 26 | rcpath = [] |
|
27 | 27 | filename = win32.executablepath() |
|
28 | 28 | # Use mercurial.ini found in directory with hg.exe |
|
29 | 29 | progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini') |
|
30 | 30 | rcpath.append(progrc) |
|
31 | 31 | |
|
32 | 32 | def _processdir(progrcd): |
|
33 | 33 | if os.path.isdir(progrcd): |
|
34 | 34 | for f, kind in sorted(util.listdir(progrcd)): |
|
35 | 35 | if f.endswith(b'.rc'): |
|
36 | 36 | rcpath.append(os.path.join(progrcd, f)) |
|
37 | 37 | |
|
38 | 38 | # Use hgrc.d found in directory with hg.exe |
|
39 | 39 | _processdir(os.path.join(os.path.dirname(filename), b'hgrc.d')) |
|
40 | 40 | |
|
41 | 41 | # treat a PROGRAMDATA directory as equivalent to /etc/mercurial |
|
42 | 42 | programdata = encoding.environ.get(b'PROGRAMDATA') |
|
43 | 43 | if programdata: |
|
44 | 44 | programdata = os.path.join(programdata, b'Mercurial') |
|
45 | 45 | _processdir(os.path.join(programdata, b'hgrc.d')) |
|
46 | 46 | |
|
47 | 47 | ini = os.path.join(programdata, b'mercurial.ini') |
|
48 | 48 | if os.path.isfile(ini): |
|
49 | 49 | rcpath.append(ini) |
|
50 | 50 | |
|
51 | 51 | ini = os.path.join(programdata, b'hgrc') |
|
52 | 52 | if os.path.isfile(ini): |
|
53 | 53 | rcpath.append(ini) |
|
54 | 54 | |
|
55 | 55 | # next look for a system rcpath in the registry |
|
56 | 56 | value = util.lookupreg( |
|
57 | 57 | b'SOFTWARE\\Mercurial', None, winreg.HKEY_LOCAL_MACHINE |
|
58 | 58 | ) |
|
59 | 59 | if value and isinstance(value, bytes): |
|
60 | 60 | value = util.localpath(value) |
|
61 | 61 | for p in value.split(pycompat.ospathsep): |
|
62 | 62 | if p.lower().endswith(b'mercurial.ini'): |
|
63 | 63 | rcpath.append(p) |
|
64 | 64 | else: |
|
65 | 65 | _processdir(p) |
|
66 | 66 | return rcpath |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def userrcpath(): |
|
70 | 70 | '''return os-specific hgrc search path to the user dir''' |
|
71 |
home = |
|
|
71 | home = _legacy_expanduser(b'~') | |
|
72 | 72 | path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')] |
|
73 | 73 | userprofile = encoding.environ.get(b'USERPROFILE') |
|
74 | 74 | if userprofile and userprofile != home: |
|
75 | 75 | path.append(os.path.join(userprofile, b'mercurial.ini')) |
|
76 | 76 | path.append(os.path.join(userprofile, b'.hgrc')) |
|
77 | 77 | return path |
|
78 | 78 | |
|
79 | 79 | |
|
80 | def _legacy_expanduser(path): | |
|
81 | """Expand ~ and ~user constructs in the pre 3.8 style""" | |
|
82 | ||
|
83 | # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE. See | |
|
84 | # https://bugs.python.org/issue36264. It also seems to capitalize the drive | |
|
85 | # letter, as though it was processed through os.path.realpath(). | |
|
86 | if not path.startswith(b'~'): | |
|
87 | return path | |
|
88 | ||
|
89 | i, n = 1, len(path) | |
|
90 | while i < n and path[i] not in b'\\/': | |
|
91 | i += 1 | |
|
92 | ||
|
93 | if b'HOME' in encoding.environ: | |
|
94 | userhome = encoding.environ[b'HOME'] | |
|
95 | elif b'USERPROFILE' in encoding.environ: | |
|
96 | userhome = encoding.environ[b'USERPROFILE'] | |
|
97 | elif b'HOMEPATH' not in encoding.environ: | |
|
98 | return path | |
|
99 | else: | |
|
100 | try: | |
|
101 | drive = encoding.environ[b'HOMEDRIVE'] | |
|
102 | except KeyError: | |
|
103 | drive = b'' | |
|
104 | userhome = os.path.join(drive, encoding.environ[b'HOMEPATH']) | |
|
105 | ||
|
106 | if i != 1: # ~user | |
|
107 | userhome = os.path.join(os.path.dirname(userhome), path[1:i]) | |
|
108 | ||
|
109 | return userhome + path[i:] | |
|
110 | ||
|
111 | ||
|
80 | 112 | def termsize(ui): |
|
81 | 113 | return win32.termsize() |
General Comments 0
You need to be logged in to leave comments.
Login now