Mercurial > python-hglib
annotate hglib/util.py @ 214:f422a5fdda79
Added tag 2.6.2 for changeset 388820908580
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Tue, 17 Nov 2020 13:35:10 -0500 |
parents | 2d0ec6097d78 |
children |
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 | 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 | 28 def grouper(n, iterable): |
29 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' | |
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 | 52 def skiplines(s, prefix): |
53 """ | |
54 Skip lines starting with prefix in s | |
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 | 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 | 66 |
67 for line in cs: | |
68 if not line.startswith(prefix): | |
69 return line + cs.read() | |
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 | 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 |
205
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
114 >>> cmdbuilder(b('cmd'), b('')) == [b('cmd'), b('--'), b('')] |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
115 True |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
116 >>> cmdbuilder(b('cmd'), s=b('')) == [b('cmd'), b('-s'), b('')] |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
117 True |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
118 >>> cmdbuilder(b('cmd'), s=[b('')]) == [b('cmd'), b('-s'), b('')] |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
119 True |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
120 >>> cmdbuilder(b('cmd'), long=b('')) == [b('cmd'), b('--long=')] |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
121 True |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
122 """ |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
123 cmd = [name] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
124 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
|
125 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
|
126 continue |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
127 |
193
32e8d51ec16c
util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents:
182
diff
changeset
|
128 arg = pfx = arg.encode('latin-1').replace(b('_'), b('-')) |
205
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
129 short = (len(arg) == 1) |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
130 if arg != b('-'): |
205
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
131 if short: |
193
32e8d51ec16c
util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents:
182
diff
changeset
|
132 arg = pfx = b('-') + arg |
86
f98d6e234cd9
util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents:
77
diff
changeset
|
133 else: |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
134 arg = b('--') + arg |
193
32e8d51ec16c
util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents:
182
diff
changeset
|
135 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
|
136 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
|
137 if val: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
138 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
|
139 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
|
140 for v in val: |
205
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
141 s = _cmdval(v) |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
142 if s or not short: |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
143 cmd.append(pfx + s) |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
144 else: |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
145 cmd.extend([arg, s]) |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
146 else: |
205
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
147 s = _cmdval(val) |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
148 if s or not short: |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
149 cmd.append(pfx + s) |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
150 else: |
2d0ec6097d78
util: fix handling of empty short option
Yuya Nishihara <yuya@tcha.org>
parents:
193
diff
changeset
|
151 cmd.extend([arg, s]) |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
152 |
193
32e8d51ec16c
util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents:
182
diff
changeset
|
153 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
|
154 if args: |
32e8d51ec16c
util: make cmdbuilder() robust for faulty parsing of early options
Yuya Nishihara <yuya@tcha.org>
parents:
182
diff
changeset
|
155 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
|
156 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
|
157 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
|
158 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
159 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
|
160 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
161 class reterrorhandler(object): |
134 | 162 """This class is meant to be used with rawcommand() error handler |
163 argument. It remembers the return value the command returned if | |
164 it's one of allowed values, which is only 1 if none are given. | |
165 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
|
166 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
167 >>> e = reterrorhandler('') |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
168 >>> bool(e) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
169 True |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
170 >>> 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
|
171 'a' |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
172 >>> bool(e) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
173 False |
134 | 174 |
49
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
175 """ |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 else: |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
182 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
|
183 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
184 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
|
185 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
|
186 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
|
187 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
|
188 return out |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
189 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
190 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
|
191 """ 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
|
192 return self.ret == 0 |
72 | 193 |
149
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
194 def __bool__(self): |
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
195 return self.__nonzero__() |
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
196 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
197 class propertycache(object): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
198 """ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
199 Decorator that remembers the return value of a function call. |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
200 |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
201 >>> execcount = 0 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
202 >>> class obj(object): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
203 ... def func(self): |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
204 ... global execcount |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
205 ... execcount += 1 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
206 ... return [] |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
207 ... func = propertycache(func) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
208 >>> o = obj() |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
209 >>> o.func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
210 [] |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
211 >>> execcount |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
212 1 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
213 >>> o.func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
214 [] |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
215 >>> execcount |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
216 1 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
217 """ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
218 def __init__(self, func): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
219 self.func = func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
220 self.name = func.__name__ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
221 def __get__(self, obj, type=None): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
222 result = self.func(obj) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
223 setattr(obj, self.name, result) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
224 return result |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
225 |
72 | 226 close_fds = os.name == 'posix' |
227 | |
74
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
228 startupinfo = None |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
229 if os.name == 'nt': |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
230 startupinfo = subprocess.STARTUPINFO() |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
231 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
232 |
182
0f81ed8e147b
util: drop mutable default from popen()
Yuya Nishihara <yuya@tcha.org>
parents:
159
diff
changeset
|
233 def popen(args, env=None): |
72 | 234 environ = None |
235 if env: | |
236 environ = dict(os.environ) | |
237 environ.update(env) | |
238 | |
239 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
|
240 stderr=subprocess.PIPE, close_fds=close_fds, |
74
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
241 startupinfo=startupinfo, env=environ) |