annotate mercurial/scmposix.py @ 30476:5b0baa9f3362

py3: use pycompat.sysargv in scmposix.systemrcpath() sys.argv returns unicodes on Python 3. We have pycompat.sysargv which returns bytes encoded using os.fsencode(). After this patch scmposix.systemrcpath() returns bytes in Python 3 world. This change is also a part of making `hg version` run in Python 3.
author Pulkit Goyal <7895pulkit@gmail.com>
date Mon, 21 Nov 2016 15:26:47 +0530
parents 365812902904
children 16b5df5792a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27483
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
1 from __future__ import absolute_import
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
2
30324
80708959161a scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30323
diff changeset
3 import array
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
4 import errno
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
5 import fcntl
27483
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
6 import os
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
7 import sys
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
8
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
9 from . import (
30290
c90a05124fae py3: make scmposix.userrcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27483
diff changeset
10 encoding,
27483
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
11 osutil,
30476
5b0baa9f3362 py3: use pycompat.sysargv in scmposix.systemrcpath()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30327
diff changeset
12 pycompat,
27483
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
13 )
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
14
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
15 def _rcfiles(path):
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
16 rcs = [os.path.join(path, 'hgrc')]
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
17 rcdir = os.path.join(path, 'hgrc.d')
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
18 try:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
19 rcs.extend([os.path.join(rcdir, f)
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
20 for f, kind in osutil.listdir(rcdir)
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
21 if f.endswith(".rc")])
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
22 except OSError:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
23 pass
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
24 return rcs
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
25
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
26 def systemrcpath():
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
27 path = []
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
28 if sys.platform == 'plan9':
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
29 root = 'lib/mercurial'
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
30 else:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
31 root = 'etc/mercurial'
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
32 # 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
33 if len(getattr(sys, 'argv', [])) > 0:
30476
5b0baa9f3362 py3: use pycompat.sysargv in scmposix.systemrcpath()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30327
diff changeset
34 p = os.path.dirname(os.path.dirname(pycompat.sysargv[0]))
22583
23c995ed466b config: don't read the same config file twice
Mads Kiilerich <madski@unity3d.com>
parents: 18690
diff changeset
35 if p != '/':
23c995ed466b config: don't read the same config file twice
Mads Kiilerich <madski@unity3d.com>
parents: 18690
diff changeset
36 path.extend(_rcfiles(os.path.join(p, root)))
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
37 path.extend(_rcfiles('/' + root))
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
38 return path
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
39
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
40 def userrcpath():
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
41 if sys.platform == 'plan9':
30290
c90a05124fae py3: make scmposix.userrcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27483
diff changeset
42 return [encoding.environ['home'] + '/lib/hgrc']
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
43 else:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
44 return [os.path.expanduser('~/.hgrc')]
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
45
30327
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30325
diff changeset
46 def termsize(ui):
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
47 try:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
48 import termios
30324
80708959161a scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30323
diff changeset
49 TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449)
80708959161a scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30323
diff changeset
50 except (AttributeError, ImportError):
30327
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30325
diff changeset
51 return 80, 24
30325
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
52
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
53 for dev in (ui.ferr, ui.fout, ui.fin):
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
54 try:
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
55 try:
30325
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
56 fd = dev.fileno()
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
57 except AttributeError:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
58 continue
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
59 if not os.isatty(fd):
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
60 continue
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
61 arri = fcntl.ioctl(fd, TIOCGWINSZ, '\0' * 8)
30327
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30325
diff changeset
62 height, width = array.array('h', arri)[:2]
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30325
diff changeset
63 if width > 0 and height > 0:
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30325
diff changeset
64 return width, height
30325
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
65 except ValueError:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
66 pass
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
67 except IOError as e:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
68 if e[0] == errno.EINVAL:
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
69 pass
30325
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
70 else:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
71 raise
30327
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30325
diff changeset
72 return 80, 24