Mercurial > hg
annotate mercurial/scmposix.py @ 49808:7a4143428db7
typing: add type hints to the platform specific scm modules
Surprisingly, pytype struggled to figure out the return types in the posix
functions.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Thu, 15 Dec 2022 15:41:59 -0500 |
parents | a87338fe8cfa |
children | 18c8c18993f0 |
rev | line source |
---|---|
30311
80708959161a
scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30310
diff
changeset
|
1 import array |
30309
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
2 import errno |
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
3 import fcntl |
27483
39087ee88835
scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
22583
diff
changeset
|
4 import os |
39087ee88835
scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
22583
diff
changeset
|
5 import sys |
39087ee88835
scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
22583
diff
changeset
|
6 |
49808
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
7 from typing import ( |
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
8 List, |
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
9 Tuple, |
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
10 ) |
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
11 |
43089
c59eb1560c44
py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43077
diff
changeset
|
12 from .pycompat import getattr |
27483
39087ee88835
scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
22583
diff
changeset
|
13 from . import ( |
30276
c90a05124fae
py3: make scmposix.userrcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27483
diff
changeset
|
14 encoding, |
30467
5b0baa9f3362
py3: use pycompat.sysargv in scmposix.systemrcpath()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30314
diff
changeset
|
15 pycompat, |
32208
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32078
diff
changeset
|
16 util, |
27483
39087ee88835
scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
22583
diff
changeset
|
17 ) |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
18 |
49808
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
19 if pycompat.TYPE_CHECKING: |
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
20 from . import ui as uimod |
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
21 |
32078
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31339
diff
changeset
|
22 # BSD 'more' escapes ANSI color sequences by default. This can be disabled by |
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31339
diff
changeset
|
23 # $MORE variable, but there's no compatible option with Linux 'more'. Given |
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31339
diff
changeset
|
24 # OS X is widely used and most modern Unix systems would have 'less', setting |
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31339
diff
changeset
|
25 # 'less' as the default seems reasonable. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
26 fallbackpager = b'less' |
32078
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31339
diff
changeset
|
27 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
28 |
49808
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
29 def _rcfiles(path: bytes) -> List[bytes]: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
30 rcs = [os.path.join(path, b'hgrc')] |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
31 rcdir = os.path.join(path, b'hgrc.d') |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
32 try: |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
33 rcs.extend( |
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
34 [ |
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
35 os.path.join(rcdir, f) |
45824
9ac96b9fa76e
config: read system hgrc in lexicographical order
Martin von Zweigbergk <martinvonz@google.com>
parents:
43506
diff
changeset
|
36 for f, kind in sorted(util.listdir(rcdir)) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
37 if f.endswith(b".rc") |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
38 ] |
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
39 ) |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
40 except OSError: |
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
41 pass |
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
42 return rcs |
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
43 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
44 |
49808
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
45 def systemrcpath() -> List[bytes]: |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
46 path = [] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
47 if pycompat.sysplatform == b'plan9': |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
48 root = b'lib/mercurial' |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
49 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
50 root = b'etc/mercurial' |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
51 # old mod_python does not set sys.argv |
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
52 if len(getattr(sys, 'argv', [])) > 0: |
30467
5b0baa9f3362
py3: use pycompat.sysargv in scmposix.systemrcpath()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30314
diff
changeset
|
53 p = os.path.dirname(os.path.dirname(pycompat.sysargv[0])) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
54 if p != b'/': |
22583
23c995ed466b
config: don't read the same config file twice
Mads Kiilerich <madski@unity3d.com>
parents:
18690
diff
changeset
|
55 path.extend(_rcfiles(os.path.join(p, root))) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
56 path.extend(_rcfiles(b'/' + root)) |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
57 return path |
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
58 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
59 |
49808
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
60 def userrcpath() -> List[bytes]: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
61 if pycompat.sysplatform == b'plan9': |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
62 return [encoding.environ[b'home'] + b'/lib/hgrc'] |
34647 | 63 elif pycompat.isdarwin: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
64 return [os.path.expanduser(b'~/.hgrc')] |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff
changeset
|
65 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
66 confighome = encoding.environ.get(b'XDG_CONFIG_HOME') |
30941
354020079723
hg: allow usage of XDG_CONFIG_HOME/hg/hgrc
David Demelier <demelier.david@gmail.com>
parents:
30641
diff
changeset
|
67 if confighome is None or not os.path.isabs(confighome): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
68 confighome = os.path.expanduser(b'~/.config') |
30941
354020079723
hg: allow usage of XDG_CONFIG_HOME/hg/hgrc
David Demelier <demelier.david@gmail.com>
parents:
30641
diff
changeset
|
69 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
70 return [ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
71 os.path.expanduser(b'~/.hgrc'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
72 os.path.join(confighome, b'hg', b'hgrc'), |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
73 ] |
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
74 |
30309
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
75 |
49808
7a4143428db7
typing: add type hints to the platform specific scm modules
Matt Harbison <matt_harbison@yahoo.com>
parents:
49804
diff
changeset
|
76 def termsize(ui: "uimod.ui") -> Tuple[int, int]: |
30309
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
77 try: |
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
78 import termios |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
34647
diff
changeset
|
79 |
30311
80708959161a
scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30310
diff
changeset
|
80 TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449) |
80708959161a
scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30310
diff
changeset
|
81 except (AttributeError, ImportError): |
30314
365812902904
scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents:
30312
diff
changeset
|
82 return 80, 24 |
30312
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
83 |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
84 for dev in (ui.ferr, ui.fout, ui.fin): |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
85 try: |
30309
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
86 try: |
30312
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
87 fd = dev.fileno() |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
88 except AttributeError: |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
89 continue |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
90 if not os.isatty(fd): |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
91 continue |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
92 arri = fcntl.ioctl(fd, TIOCGWINSZ, b'\0' * 8) |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43480
diff
changeset
|
93 height, width = array.array('h', arri)[:2] |
30314
365812902904
scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents:
30312
diff
changeset
|
94 if width > 0 and height > 0: |
365812902904
scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents:
30312
diff
changeset
|
95 return width, height |
30312
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
96 except ValueError: |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
97 pass |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
98 except IOError as e: |
49804
a87338fe8cfa
scmposix: don't subscript IOError
Matt Harbison <matt_harbison@yahoo.com>
parents:
48875
diff
changeset
|
99 if e.errno == errno.EINVAL: |
30309
4b1af1c867fa
scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30276
diff
changeset
|
100 pass |
30312
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
101 else: |
1ad1c5017043
scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents:
30311
diff
changeset
|
102 raise |
30314
365812902904
scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents:
30312
diff
changeset
|
103 return 80, 24 |