annotate mercurial/scmposix.py @ 30579:fbc3f73dc802

py3: utility functions to convert keys of kwargs to bytes/unicodes Keys of keyword arguments need to be str(unicodes) on Python 3. We have a lot of function where we pass keyword arguments. Having utility functions to help converting keys to unicodes before passing and convert back them to bytes once passed into the function will be helpful. We now have functions named pycompat.strkwargs(dic) and pycompat.byteskwargs(dic) to help us.
author Pulkit Goyal <7895pulkit@gmail.com>
date Wed, 07 Dec 2016 21:53:03 +0530
parents 5b0baa9f3362
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
30311
80708959161a scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30310
diff changeset
3 import array
30309
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
diff changeset
4 import errno
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
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 (
30276
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,
30467
5b0baa9f3362 py3: use pycompat.sysargv in scmposix.systemrcpath()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30314
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:
30467
5b0baa9f3362 py3: use pycompat.sysargv in scmposix.systemrcpath()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30314
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':
30276
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')]
30309
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
diff changeset
45
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30312
diff changeset
46 def termsize(ui):
30309
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
diff changeset
47 try:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
diff changeset
48 import termios
30311
80708959161a scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30310
diff changeset
49 TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449)
80708959161a scmutil: narrow ImportError handling in termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30310
diff changeset
50 except (AttributeError, ImportError):
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30312
diff changeset
51 return 80, 24
30312
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
52
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
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: 30311
diff changeset
54 try:
30309
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
diff changeset
55 try:
30312
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
56 fd = dev.fileno()
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
57 except AttributeError:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
58 continue
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
59 if not os.isatty(fd):
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
60 continue
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
61 arri = fcntl.ioctl(fd, TIOCGWINSZ, '\0' * 8)
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30312
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: 30312
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: 30312
diff changeset
64 return width, height
30312
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
65 except ValueError:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
66 pass
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
67 except IOError as e:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
68 if e[0] == errno.EINVAL:
30309
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30276
diff changeset
69 pass
30312
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
70 else:
1ad1c5017043 scmutil: remove superfluous indent from termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30311
diff changeset
71 raise
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30312
diff changeset
72 return 80, 24