scmposix.py
103 lines
| 2.6 KiB
| text/x-python
|
PythonLexer
/ mercurial / scmposix.py
Matt Harbison
|
r52756 | from __future__ import annotations | ||
Yuya Nishihara
|
r30311 | import array | ||
Yuya Nishihara
|
r30309 | import errno | ||
import fcntl | ||||
Gregory Szorc
|
r27483 | import os | ||
import sys | ||||
r52178 | import typing | |||
Gregory Szorc
|
r27483 | |||
Matt Harbison
|
r50703 | from typing import ( | ||
List, | ||||
Tuple, | ||||
) | ||||
Gregory Szorc
|
r27483 | from . import ( | ||
Pulkit Goyal
|
r30276 | encoding, | ||
Pulkit Goyal
|
r30467 | pycompat, | ||
Yuya Nishihara
|
r32203 | util, | ||
Gregory Szorc
|
r27483 | ) | ||
Kevin Bullock
|
r18690 | |||
r52178 | if typing.TYPE_CHECKING: | |||
Matt Harbison
|
r50703 | from . import ui as uimod | ||
Yuya Nishihara
|
r32078 | # BSD 'more' escapes ANSI color sequences by default. This can be disabled by | ||
# $MORE variable, but there's no compatible option with Linux 'more'. Given | ||||
# OS X is widely used and most modern Unix systems would have 'less', setting | ||||
# 'less' as the default seems reasonable. | ||||
Augie Fackler
|
r43347 | fallbackpager = b'less' | ||
Yuya Nishihara
|
r32078 | |||
Augie Fackler
|
r43345 | |||
Matt Harbison
|
r50703 | def _rcfiles(path: bytes) -> List[bytes]: | ||
Augie Fackler
|
r43347 | rcs = [os.path.join(path, b'hgrc')] | ||
rcdir = os.path.join(path, b'hgrc.d') | ||||
Kevin Bullock
|
r18690 | try: | ||
Augie Fackler
|
r43345 | rcs.extend( | ||
[ | ||||
os.path.join(rcdir, f) | ||||
Martin von Zweigbergk
|
r46428 | for f, kind in sorted(util.listdir(rcdir)) | ||
Augie Fackler
|
r43347 | if f.endswith(b".rc") | ||
Augie Fackler
|
r43345 | ] | ||
) | ||||
Kevin Bullock
|
r18690 | except OSError: | ||
pass | ||||
return rcs | ||||
Augie Fackler
|
r43345 | |||
Matt Harbison
|
r50703 | def systemrcpath() -> List[bytes]: | ||
Kevin Bullock
|
r18690 | path = [] | ||
Augie Fackler
|
r43347 | if pycompat.sysplatform == b'plan9': | ||
root = b'lib/mercurial' | ||||
Kevin Bullock
|
r18690 | else: | ||
Augie Fackler
|
r43347 | root = b'etc/mercurial' | ||
Kevin Bullock
|
r18690 | # old mod_python does not set sys.argv | ||
if len(getattr(sys, 'argv', [])) > 0: | ||||
Pulkit Goyal
|
r30467 | p = os.path.dirname(os.path.dirname(pycompat.sysargv[0])) | ||
Augie Fackler
|
r43347 | if p != b'/': | ||
Mads Kiilerich
|
r22583 | path.extend(_rcfiles(os.path.join(p, root))) | ||
Augie Fackler
|
r43347 | path.extend(_rcfiles(b'/' + root)) | ||
Kevin Bullock
|
r18690 | return path | ||
Augie Fackler
|
r43345 | |||
Matt Harbison
|
r50703 | def userrcpath() -> List[bytes]: | ||
Augie Fackler
|
r43347 | if pycompat.sysplatform == b'plan9': | ||
return [encoding.environ[b'home'] + b'/lib/hgrc'] | ||||
Kevin Bullock
|
r18690 | else: | ||
Augie Fackler
|
r43347 | confighome = encoding.environ.get(b'XDG_CONFIG_HOME') | ||
David Demelier
|
r30941 | if confighome is None or not os.path.isabs(confighome): | ||
Augie Fackler
|
r43347 | confighome = os.path.expanduser(b'~/.config') | ||
David Demelier
|
r30941 | |||
Augie Fackler
|
r43345 | return [ | ||
Augie Fackler
|
r43347 | os.path.expanduser(b'~/.hgrc'), | ||
os.path.join(confighome, b'hg', b'hgrc'), | ||||
Augie Fackler
|
r43345 | ] | ||
Yuya Nishihara
|
r30309 | |||
Matt Harbison
|
r50703 | def termsize(ui: "uimod.ui") -> Tuple[int, int]: | ||
Yuya Nishihara
|
r30309 | try: | ||
import termios | ||||
Augie Fackler
|
r43345 | |||
Yuya Nishihara
|
r30311 | TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449) | ||
except (AttributeError, ImportError): | ||||
Yuya Nishihara
|
r30314 | return 80, 24 | ||
Yuya Nishihara
|
r30312 | |||
for dev in (ui.ferr, ui.fout, ui.fin): | ||||
try: | ||||
Yuya Nishihara
|
r30309 | try: | ||
Yuya Nishihara
|
r30312 | fd = dev.fileno() | ||
except AttributeError: | ||||
continue | ||||
if not os.isatty(fd): | ||||
continue | ||||
Augie Fackler
|
r43347 | arri = fcntl.ioctl(fd, TIOCGWINSZ, b'\0' * 8) | ||
Augie Fackler
|
r43906 | height, width = array.array('h', arri)[:2] | ||
Yuya Nishihara
|
r30314 | if width > 0 and height > 0: | ||
return width, height | ||||
Yuya Nishihara
|
r30312 | except ValueError: | ||
pass | ||||
except IOError as e: | ||||
Matt Harbison
|
r50699 | if e.errno == errno.EINVAL: | ||
Yuya Nishihara
|
r30309 | pass | ||
Yuya Nishihara
|
r30312 | else: | ||
raise | ||||
Yuya Nishihara
|
r30314 | return 80, 24 | ||