Mercurial > python-hglib
annotate hglib/util.py @ 153:ef8eb78fc88d
hglib: don't try to use long under Python 3 (issue4520)
author | Brett Cannon <brett@python.org> |
---|---|
date | Fri, 20 Mar 2015 16:34:42 -0400 |
parents | 0808bb03add5 |
children | 2104fc9aa513 |
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) |
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
18 |
141
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
19 def b(s): |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
20 """Encode the string as bytes.""" |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
21 return s |
0 | 22 |
150
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
23 def strtobytes(s): |
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
24 """Return the bytes of the string representation of an object.""" |
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
25 return str(s).encode('latin-1') |
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
26 |
0 | 27 def grouper(n, iterable): |
28 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' | |
29 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
|
30 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
|
31 |
8
3ac38d500d68
move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents:
3
diff
changeset
|
32 def eatlines(s, n): |
9
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
33 """ |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
34 >>> eatlines("1\\n2", 1) |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
35 '2' |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
36 >>> eatlines("1\\n2", 2) |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
37 '' |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
38 >>> eatlines("1\\n2", 3) |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
39 '' |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
40 >>> eatlines("1\\n2\\n3", 1) |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
41 '2\\n3' |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
42 """ |
145
f3c430afa598
hglib: abstract out use of cStringIO.StringIO (issue4520)
Brett Cannon <brett@python.org>
parents:
142
diff
changeset
|
43 cs = BytesIO(s) |
8
3ac38d500d68
move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents:
3
diff
changeset
|
44 |
9
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
45 for line in cs: |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
46 n -= 1 |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
47 if n == 0: |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
48 return cs.read() |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
49 return b('') |
8
3ac38d500d68
move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents:
3
diff
changeset
|
50 |
19 | 51 def skiplines(s, prefix): |
52 """ | |
53 Skip lines starting with prefix in s | |
54 | |
55 >>> skiplines('a\\nb\\na\\n', 'a') | |
56 'b\\na\\n' | |
57 >>> skiplines('a\\na\\n', 'a') | |
58 '' | |
59 >>> skiplines('', 'a') | |
60 '' | |
61 >>> skiplines('a\\nb', 'b') | |
62 'a\\nb' | |
63 """ | |
145
f3c430afa598
hglib: abstract out use of cStringIO.StringIO (issue4520)
Brett Cannon <brett@python.org>
parents:
142
diff
changeset
|
64 cs = BytesIO(s) |
19 | 65 |
66 for line in cs: | |
67 if not line.startswith(prefix): | |
68 return line + cs.read() | |
69 | |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
70 return b('') |
19 | 71 |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
72 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
|
73 """ |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
74 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
|
75 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
76 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
|
77 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
78 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
|
79 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
|
80 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
|
81 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
82 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
|
83 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
84 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
|
85 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
86 >>> cmdbuilder('cmd', a=True, b=False, c=None) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
87 ['cmd', '-a'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
88 >>> cmdbuilder('cmd', long=True) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
89 ['cmd', '--long'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
90 >>> cmdbuilder('cmd', str='s') |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
91 ['cmd', '--str', 's'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
92 >>> cmdbuilder('cmd', d_ash=True) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
93 ['cmd', '--d-ash'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
94 >>> cmdbuilder('cmd', _=True) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
95 ['cmd', '-'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
96 >>> cmdbuilder('cmd', list=[1, 2]) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
97 ['cmd', '--list', '1', '--list', '2'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
98 >>> cmdbuilder('cmd', None) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
99 ['cmd'] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
100 """ |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
101 cmd = [name] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
102 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
|
103 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
|
104 continue |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
105 |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
106 arg = arg.replace(b('_'), b('-')) |
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
107 if arg != b('-'): |
86
f98d6e234cd9
util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents:
77
diff
changeset
|
108 if len(arg) == 1: |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
109 arg = b('-') + arg |
86
f98d6e234cd9
util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents:
77
diff
changeset
|
110 else: |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
111 arg = b('--') + arg |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
112 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
|
113 if val: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
114 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
|
115 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
|
116 for v in val: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
117 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
|
118 cmd.append(str(v)) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
119 else: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
120 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
|
121 cmd.append(str(val)) |
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 for a in args: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
124 if a is not None: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
125 cmd.append(a) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
126 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
127 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
|
128 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
129 class reterrorhandler(object): |
134 | 130 """This class is meant to be used with rawcommand() error handler |
131 argument. It remembers the return value the command returned if | |
132 it's one of allowed values, which is only 1 if none are given. | |
133 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
|
134 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
135 >>> e = reterrorhandler('') |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
136 >>> bool(e) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
137 True |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
138 >>> 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
|
139 'a' |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
140 >>> bool(e) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
141 False |
134 | 142 |
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 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 else: |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
150 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
|
151 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 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
|
156 return out |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
157 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
158 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
|
159 """ 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
|
160 return self.ret == 0 |
72 | 161 |
149
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
162 def __bool__(self): |
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
163 return self.__nonzero__() |
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
164 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
165 class propertycache(object): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
166 """ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
167 Decorator that remembers the return value of a function call. |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
168 |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
169 >>> class obj(object): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
170 ... def func(self): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
171 ... print 'func' |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
172 ... return [] |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
173 ... func = propertycache(func) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
174 >>> o = obj() |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
175 >>> o.func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
176 func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
177 [] |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
178 >>> o.func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
179 [] |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
180 """ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
181 def __init__(self, func): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
182 self.func = func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
183 self.name = func.__name__ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
184 def __get__(self, obj, type=None): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
185 result = self.func(obj) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
186 setattr(obj, self.name, result) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
187 return result |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
188 |
72 | 189 close_fds = os.name == 'posix' |
190 | |
74
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
191 startupinfo = None |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
192 if os.name == 'nt': |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
193 startupinfo = subprocess.STARTUPINFO() |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
194 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
195 |
72 | 196 def popen(args, env={}): |
197 environ = None | |
198 if env: | |
199 environ = dict(os.environ) | |
200 environ.update(env) | |
201 | |
202 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
|
203 stderr=subprocess.PIPE, close_fds=close_fds, |
74
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
204 startupinfo=startupinfo, env=environ) |