annotate hglib/util.py @ 141:ea80bd2775f6

hglib: introduce util.b() (issue4520) The util.b() function will be used to mark all string literals in the code base which should be treated as bytes instead of text. This is to help with supporting Python 3.
author Brett Cannon <brett@python.org>
date Sat, 07 Mar 2015 10:08:52 -0500
parents 1b47146a4a2c
children fe74d5599539
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
1 import itertools, cStringIO, error, os, subprocess, sys
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
2
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
3 if sys.version_info[0] > 2:
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
4 def b(s):
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
5 """Encode the string as bytes."""
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
6 return s.encode('latin-1')
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
7 else:
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
8 def b(s):
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
9 """Encode the string as bytes."""
ea80bd2775f6 hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents: 134
diff changeset
10 return s
0
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
11
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
12 def grouper(n, iterable):
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
13 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
14 args = [iter(iterable)] * n
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
15 return itertools.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
16
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
17 def eatlines(s, n):
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
18 """
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
19 >>> eatlines("1\\n2", 1)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
20 '2'
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
21 >>> eatlines("1\\n2", 2)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
22 ''
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
23 >>> eatlines("1\\n2", 3)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
24 ''
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
25 >>> eatlines("1\\n2\\n3", 1)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
26 '2\\n3'
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
27 """
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
28 cs = cStringIO.StringIO(s)
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
29
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
30 for line in cs:
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
31 n -= 1
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
32 if n == 0:
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
33 return cs.read()
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
34 return ''
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
35
19
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
36 def skiplines(s, prefix):
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
37 """
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
38 Skip lines starting with prefix in s
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
39
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
40 >>> skiplines('a\\nb\\na\\n', 'a')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
41 'b\\na\\n'
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
42 >>> skiplines('a\\na\\n', 'a')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
43 ''
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
44 >>> skiplines('', 'a')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
45 ''
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
46 >>> skiplines('a\\nb', 'b')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
47 'a\\nb'
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
48 """
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
49 cs = cStringIO.StringIO(s)
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
50
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
51 for line in cs:
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
52 if not line.startswith(prefix):
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
53 return line + cs.read()
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
54
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
55 return ''
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
56
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
57 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
58 """
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
59 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
60
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
61 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
62
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
63 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
64 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
65 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
66
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
67 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
68
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
69 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
70
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
71 >>> 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
72 ['cmd', '-a']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
73 >>> 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
74 ['cmd', '--long']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
75 >>> 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
76 ['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
77 >>> 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
78 ['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
79 >>> 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
80 ['cmd', '-']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
81 >>> 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
82 ['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
83 >>> 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
84 ['cmd']
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 cmd = [name]
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
87 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
88 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
89 continue
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 arg = arg.replace('_', '-')
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
92 if arg != '-':
86
f98d6e234cd9 util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents: 77
diff changeset
93 if len(arg) == 1:
f98d6e234cd9 util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents: 77
diff changeset
94 arg = '-' + arg
f98d6e234cd9 util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents: 77
diff changeset
95 else:
f98d6e234cd9 util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents: 77
diff changeset
96 arg = '--' + arg
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
97 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
98 if val:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
99 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
100 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
101 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
102 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
103 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
104 else:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
105 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
106 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
107
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
108 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
109 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
110 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
111
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
112 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
113
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
114 class reterrorhandler(object):
134
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
115 """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
116 argument. It remembers the return value the command returned if
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
117 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
118 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
119
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
120 >>> e = reterrorhandler('')
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
121 >>> bool(e)
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
122 True
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
123 >>> 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
124 'a'
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
125 >>> bool(e)
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
126 False
134
1b47146a4a2c style: fix long lines
Matt Mackall <mpm@selenic.com>
parents: 86
diff changeset
127
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 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
130 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
131 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
132 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
133 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
134 else:
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
135 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
136
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
137 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
138 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
139 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
140 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
141 return out
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
142
3d7e0325ba1c util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents: 19
diff changeset
143 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
144 """ 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
145 return self.ret == 0
72
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
146
77
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
147 class propertycache(object):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
148 """
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
149 Decorator that remembers the return value of a function call.
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
150
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
151 >>> class obj(object):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
152 ... def func(self):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
153 ... print 'func'
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
154 ... return []
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
155 ... func = propertycache(func)
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
156 >>> o = obj()
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
157 >>> o.func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
158 func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
159 []
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
160 >>> o.func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
161 []
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
162 """
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
163 def __init__(self, func):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
164 self.func = func
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
165 self.name = func.__name__
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
166 def __get__(self, obj, type=None):
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
167 result = self.func(obj)
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
168 setattr(obj, self.name, result)
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
169 return result
4282391dd693 util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents: 74
diff changeset
170
72
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
171 close_fds = os.name == 'posix'
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
172
74
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
173 startupinfo = None
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
174 if os.name == 'nt':
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
175 startupinfo = subprocess.STARTUPINFO()
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
176 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
177
72
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
178 def popen(args, env={}):
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
179 environ = None
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
180 if env:
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
181 environ = dict(os.environ)
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
182 environ.update(env)
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
183
15485fa4b35e util: introduce popen
Idan Kamara <idankk86@gmail.com>
parents: 62
diff changeset
184 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
185 stderr=subprocess.PIPE, close_fds=close_fds,
74
a5dd7b5d0be1 util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents: 73
diff changeset
186 startupinfo=startupinfo, env=environ)