annotate mercurial/scmposix.py @ 30322:4b1af1c867fa

scmutil: move util.termwidth() I'm going to get rid of sys.stderr|out|in references from posix.termwidth(). In order to do that, termwidth() needs to take a ui, but functions in util.py shouldn't depend on a ui object. So moves termwidth() to scmutil.py.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 20 Oct 2016 21:38:44 +0900
parents c90a05124fae
children 5c379b1f56c7
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
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
3 import errno
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
4 import fcntl
27483
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
5 import os
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
6 import sys
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
7
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
8 from . import (
30290
c90a05124fae py3: make scmposix.userrcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27483
diff changeset
9 encoding,
27483
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
10 osutil,
39087ee88835 scmposix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22583
diff changeset
11 )
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
12
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
13 def _rcfiles(path):
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
14 rcs = [os.path.join(path, 'hgrc')]
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
15 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
16 try:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
17 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
18 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
19 if f.endswith(".rc")])
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
20 except OSError:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
21 pass
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
22 return rcs
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
23
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
24 def systemrcpath():
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
25 path = []
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
26 if sys.platform == 'plan9':
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
27 root = 'lib/mercurial'
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
28 else:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
29 root = 'etc/mercurial'
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
30 # 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
31 if len(getattr(sys, 'argv', [])) > 0:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
32 p = os.path.dirname(os.path.dirname(sys.argv[0]))
22583
23c995ed466b config: don't read the same config file twice
Mads Kiilerich <madski@unity3d.com>
parents: 18690
diff changeset
33 if p != '/':
23c995ed466b config: don't read the same config file twice
Mads Kiilerich <madski@unity3d.com>
parents: 18690
diff changeset
34 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
35 path.extend(_rcfiles('/' + root))
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
36 return path
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
37
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
38 def userrcpath():
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
39 if sys.platform == 'plan9':
30290
c90a05124fae py3: make scmposix.userrcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27483
diff changeset
40 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
41 else:
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
diff changeset
42 return [os.path.expanduser('~/.hgrc')]
30322
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
43
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
44 def termwidth():
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
45 try:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
46 import array
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
47 import termios
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
48 for dev in (sys.stderr, sys.stdout, sys.stdin):
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
49 try:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
50 try:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
51 fd = dev.fileno()
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
52 except AttributeError:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
53 continue
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
54 if not os.isatty(fd):
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
55 continue
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
56 try:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
57 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
58 width = array.array('h', arri)[1]
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
59 if width > 0:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
60 return width
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
61 except AttributeError:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
62 pass
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
63 except ValueError:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
64 pass
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
65 except IOError as e:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
66 if e[0] == errno.EINVAL:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
67 pass
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
68 else:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
69 raise
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
70 except ImportError:
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
71 pass
4b1af1c867fa scmutil: move util.termwidth()
Yuya Nishihara <yuya@tcha.org>
parents: 30290
diff changeset
72 return 80