annotate hglib/util.py @ 196:c586d02f7cda

_readchannel: if a read failure is due to a broken server, report that We can end up in this codepath if the specified hg binary fails to start, and we're better off reporting that than the fact that we got no response.
author Augie Fackler <raf@durin42.com>
date Sun, 10 Dec 2017 12:50:57 -0500
parents 32e8d51ec16c
children 2d0ec6097d78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
152
0808bb03add5 util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 150
diff changeset
1 import os, subprocess, sys
148
c1b966866ed7 hglib: make all imports absolute (issue4520)
Brett Cannon <brett@python.org>
parents: 146
diff changeset
2 from hglib import error
146
8d7bf729a4db hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents: 145
diff changeset
3 try:
8d7bf729a4db hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents: 145
diff changeset
4 from io import BytesIO
8d7bf729a4db hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents: 145
diff changeset
5 except ImportError:
8d7bf729a4db hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents: 145
diff changeset
6 from cStringIO import StringIO as BytesIO
141
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
7
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
8 if sys.version_info[0] > 2:
152
0808bb03add5 util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 150
diff changeset
9 izip = zip
153
ef8eb78fc88d hglib: don't try to use long under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 152
diff changeset
10 integertypes = (int,)
152
0808bb03add5 util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 150
diff changeset
11
141
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
12 def b(s):
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
13 """Encode the string as bytes."""
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
14 return s.encode('latin-1')
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
15 else:
152
0808bb03add5 util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 150
diff changeset
16 from itertools import izip
153
ef8eb78fc88d hglib: don't try to use long under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 152
diff changeset
17 integertypes = (long, int)
158
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
18 bytes = str # Defined in Python 2.6/2.7, but to the same value.
152
0808bb03add5 util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 150
diff changeset
19
141
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
20 def b(s):
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
21 """Encode the string as bytes."""
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
22 return s
0
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
23
150
b94e1263836c util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents: 149
diff changeset
24 def strtobytes(s):
b94e1263836c util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents: 149
diff changeset
25 """Return the bytes of the string representation of an object."""
b94e1263836c util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents: 149
diff changeset
26 return str(s).encode('latin-1')
b94e1263836c util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents: 149
diff changeset
27
0
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
28 def grouper(n, iterable):
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
29 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
30 args = [iter(iterable)] * n
152
0808bb03add5 util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents: 150
diff changeset
31 return izip(*args)
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
32
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
33 def eatlines(s, n):
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
34 """
159
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
35 >>> eatlines(b("1\\n2"), 1) == b('2')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
36 True
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
37 >>> eatlines(b("1\\n2"), 2) == b('')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
38 True
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
39 >>> eatlines(b("1\\n2"), 3) == b('')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
40 True
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
41 >>> eatlines(b("1\\n2\\n3"), 1) == b('2\\n3')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
42 True
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
43 """
145
f3c430afa598 hglib: abstract out use of cStringIO.StringIO (issue4520)
Brett Cannon <brett@python.org>
parents: 142
diff changeset
44 cs = BytesIO(s)
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
45
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
46 for line in cs:
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
47 n -= 1
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
48 if n == 0:
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
49 return cs.read()
142
fe74d5599539 hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 141
diff changeset
50 return b('')
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
51
19
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
52 def skiplines(s, prefix):
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
53 """
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
54 Skip lines starting with prefix in s
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
55
159
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
56 >>> skiplines(b('a\\nb\\na\\n'), b('a')) == b('b\\na\\n')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
57 True
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
58 >>> skiplines(b('a\\na\\n'), b('a')) == b('')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
59 True
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
60 >>> skiplines(b(''), b('a')) == b('')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
61 True
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
62 >>> skiplines(b('a\\nb'), b('b')) == b('a\\nb')
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
63 True
19
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
64 """
145
f3c430afa598 hglib: abstract out use of cStringIO.StringIO (issue4520)
Brett Cannon <brett@python.org>
parents: 142
diff changeset
65 cs = BytesIO(s)
19
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
66
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
67 for line in cs:
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
68 if not line.startswith(prefix):
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
69 return line + cs.read()
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
70
142
fe74d5599539 hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 141
diff changeset
71 return b('')
19
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
72
158
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
73 def _cmdval(val):
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
74 if isinstance(val, bytes):
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
75 return val
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
76 else:
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
77 return strtobytes(val)
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
78
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
79 def cmdbuilder(name, *args, **kwargs):
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
80 """
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
81 A helper for building the command arguments
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
82
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
83 args are the positional arguments
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
84
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
85 kwargs are the options
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
86 keys that are single lettered are prepended with '-', others with '--',
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
87 underscores are replaced with dashes
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
88
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
89 keys with False boolean values are ignored, lists add the key multiple times
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
90
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
91 None arguments are skipped
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
92
158
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
93 >>> cmdbuilder(b('cmd'), a=True, b=False, c=None) == [b('cmd'), b('-a')]
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
94 True
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
95 >>> cmdbuilder(b('cmd'), long=True) == [b('cmd'), b('--long')]
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
96 True
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
97 >>> cmdbuilder(b('cmd'), s=b('hort')) == [b('cmd'), b('-short')]
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
98 True
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
99 >>> cmdbuilder(b('cmd'), str=b('s')) == [b('cmd'), b('--str=s')]
158
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
100 True
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
101 >>> cmdbuilder(b('cmd'), d_ash=True) == [b('cmd'), b('--d-ash')]
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
102 True
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
103 >>> cmdbuilder(b('cmd'), _=True) == [b('cmd'), b('-')]
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
104 True
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
105 >>> cmdbuilder(b('cmd'), l=[1, 2]) == [b('cmd'), b('-l1'), b('-l2')]
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
106 True
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
107 >>> expect = [b('cmd'), b('--list=1'), b('--list=2')]
158
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
108 >>> cmdbuilder(b('cmd'), list=[1, 2]) == expect
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
109 True
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
110 >>> cmdbuilder(b('cmd'), None) == [b('cmd')]
2104fc9aa513 util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 153
diff changeset
111 True
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
112 >>> cmdbuilder(b('cmd'), b('-a')) == [b('cmd'), b('--'), b('-a')]
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
113 True
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
114 """
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
115 cmd = [name]
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
116 for arg, val in kwargs.items():
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
117 if val is None:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
118 continue
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
119
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
120 arg = pfx = arg.encode('latin-1').replace(b('_'), b('-'))
142
fe74d5599539 hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 141
diff changeset
121 if arg != b('-'):
86
f98d6e234cd9 util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents: 77
diff changeset
122 if len(arg) == 1:
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
123 arg = pfx = b('-') + arg
86
f98d6e234cd9 util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents: 77
diff changeset
124 else:
142
fe74d5599539 hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 141
diff changeset
125 arg = b('--') + arg
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
126 pfx = arg + b('=')
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
127 if isinstance(val, bool):
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
128 if val:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
129 cmd.append(arg)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
130 elif isinstance(val, list):
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
131 for v in val:
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
132 cmd.append(pfx + _cmdval(v))
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
133 else:
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
134 cmd.append(pfx + _cmdval(val))
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
135
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
136 args = [a for a in args if a is not None]
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
137 if args:
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
138 cmd.append(b('--'))
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
139 for a in args:
193
32e8d51ec16c util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents: 182
diff changeset
140 cmd.append(a)
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
141
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
142 return cmd
49
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
143
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
144 class reterrorhandler(object):
134
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
145 """This class is meant to be used with rawcommand() error handler
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
146 argument. It remembers the return value the command returned if
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
147 it's one of allowed values, which is only 1 if none are given.
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
148 Otherwise it raises a CommandError.
49
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
149
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
150 >>> e = reterrorhandler('')
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
151 >>> bool(e)
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
152 True
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
153 >>> e(1, 'a', '')
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
154 'a'
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
155 >>> bool(e)
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
156 False
134
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
157
49
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
158 """
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
159 def __init__(self, args, allowed=None):
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
160 self.args = args
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
161 self.ret = 0
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
162 if allowed is None:
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
163 self.allowed = [1]
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
164 else:
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
165 self.allowed = allowed
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
166
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
167 def __call__(self, ret, out, err):
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
168 self.ret = ret
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
169 if ret not in self.allowed:
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
170 raise error.CommandError(self.args, ret, out, err)
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
171 return out
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
172
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
173 def __nonzero__(self):
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
174 """ Returns True if the return code was 0, False otherwise """
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
175 return self.ret == 0
72
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
176
149
958307b30af3 hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents: 148
diff changeset
177 def __bool__(self):
958307b30af3 hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents: 148
diff changeset
178 return self.__nonzero__()
958307b30af3 hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents: 148
diff changeset
179
77
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
180 class propertycache(object):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
181 """
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
182 Decorator that remembers the return value of a function call.
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
183
159
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
184 >>> execcount = 0
77
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
185 >>> class obj(object):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
186 ... def func(self):
159
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
187 ... global execcount
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
188 ... execcount += 1
77
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
189 ... return []
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
190 ... func = propertycache(func)
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
191 >>> o = obj()
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
192 >>> o.func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
193 []
159
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
194 >>> execcount
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
195 1
77
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
196 >>> o.func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
197 []
159
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
198 >>> execcount
16496e0f3c09 util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents: 158
diff changeset
199 1
77
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
200 """
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
201 def __init__(self, func):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
202 self.func = func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
203 self.name = func.__name__
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
204 def __get__(self, obj, type=None):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
205 result = self.func(obj)
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
206 setattr(obj, self.name, result)
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
207 return result
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
208
72
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
209 close_fds = os.name == 'posix'
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
210
74
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
211 startupinfo = None
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
212 if os.name == 'nt':
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
213 startupinfo = subprocess.STARTUPINFO()
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
214 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
215
182
0f81ed8e147b util: drop mutable default from popen()
Yuya Nishihara <yuya@tcha.org>
parents: 159
diff changeset
216 def popen(args, env=None):
72
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
217 environ = None
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
218 if env:
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
219 environ = dict(os.environ)
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
220 environ.update(env)
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
221
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
222 return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
73
77ae99e032f6 util, popen: redirect stderr as well (for hglib.init in case of error)
Idan Kamara <idankk86@gmail.com>
parents: 72
diff changeset
223 stderr=subprocess.PIPE, close_fds=close_fds,
74
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
224 startupinfo=startupinfo, env=environ)