| 1 |
import os, subprocess, sys |
| 2 |
from hglib import error |
| 3 |
try: |
| 4 |
from io import BytesIO |
| 5 |
except ImportError: |
| 6 |
from cStringIO import StringIO as BytesIO |
| 7 |
|
| 8 |
if sys.version_info[0] > 2: |
| 9 |
izip = zip |
| 10 |
integertypes = (int,) |
| 11 |
|
| 12 |
def b(s): |
| 13 |
"""Encode the string as bytes.""" |
| 14 |
return s.encode('latin-1') |
| 15 |
else: |
| 16 |
from itertools import izip |
| 17 |
integertypes = (long, int) |
| 18 |
bytes = str # Defined in Python 2.6/2.7, but to the same value. |
| 19 |
|
| 20 |
def b(s): |
| 21 |
"""Encode the string as bytes.""" |
| 22 |
return s |
| 23 |
|
| 24 |
def strtobytes(s): |
| 25 |
"""Return the bytes of the string representation of an object.""" |
| 26 |
return str(s).encode('latin-1') |
| 27 |
|
| 28 |
def grouper(n, iterable): |
| 29 |
''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' |
| 30 |
args = [iter(iterable)] * n |
| 31 |
return izip(*args) |
| 32 |
|
| 33 |
def eatlines(s, n): |
| 34 |
""" |
| 35 |
>>> eatlines(b("1\\n2"), 1) == b('2') |
| 36 |
True |
| 37 |
>>> eatlines(b("1\\n2"), 2) == b('') |
| 38 |
True |
| 39 |
>>> eatlines(b("1\\n2"), 3) == b('') |
| 40 |
True |
| 41 |
>>> eatlines(b("1\\n2\\n3"), 1) == b('2\\n3') |
| 42 |
True |
| 43 |
""" |
| 44 |
cs = BytesIO(s) |
| 45 |
|
| 46 |
for line in cs: |
| 47 |
n -= 1 |
| 48 |
if n == 0: |
| 49 |
return cs.read() |
| 50 |
return b('') |
| 51 |
|
| 52 |
def skiplines(s, prefix): |
| 53 |
""" |
| 54 |
Skip lines starting with prefix in s |
| 55 |
|
| 56 |
>>> skiplines(b('a\\nb\\na\\n'), b('a')) == b('b\\na\\n') |
| 57 |
True |
| 58 |
>>> skiplines(b('a\\na\\n'), b('a')) == b('') |
| 59 |
True |
| 60 |
>>> skiplines(b(''), b('a')) == b('') |
| 61 |
True |
| 62 |
>>> skiplines(b('a\\nb'), b('b')) == b('a\\nb') |
| 63 |
True |
| 64 |
""" |
| 65 |
cs = BytesIO(s) |
| 66 |
|
| 67 |
for line in cs: |
| 68 |
if not line.startswith(prefix): |
| 69 |
return line + cs.read() |
| 70 |
|
| 71 |
return b('') |
| 72 |
|
| 73 |
def _cmdval(val): |
| 74 |
if isinstance(val, bytes): |
| 75 |
return val |
| 76 |
else: |
| 77 |
return strtobytes(val) |
| 78 |
|
| 79 |
def cmdbuilder(name, *args, **kwargs): |
| 80 |
""" |
| 81 |
A helper for building the command arguments |
| 82 |
|
| 83 |
args are the positional arguments |
| 84 |
|
| 85 |
kwargs are the options |
| 86 |
keys that are single lettered are prepended with '-', others with '--', |
| 87 |
underscores are replaced with dashes |
| 88 |
|
| 89 |
keys with False boolean values are ignored, lists add the key multiple times |
| 90 |
|
| 91 |
None arguments are skipped |
| 92 |
|
| 93 |
>>> cmdbuilder(b('cmd'), a=True, b=False, c=None) == [b('cmd'), b('-a')] |
| 94 |
True |
| 95 |
>>> cmdbuilder(b('cmd'), long=True) == [b('cmd'), b('--long')] |
| 96 |
True |
| 97 |
>>> cmdbuilder(b('cmd'), str=b('s')) == [b('cmd'), b('--str'), b('s')] |
| 98 |
True |
| 99 |
>>> cmdbuilder(b('cmd'), d_ash=True) == [b('cmd'), b('--d-ash')] |
| 100 |
True |
| 101 |
>>> cmdbuilder(b('cmd'), _=True) == [b('cmd'), b('-')] |
| 102 |
True |
| 103 |
>>> expect = [b('cmd'), b('--list'), b('1'), b('--list'), b('2')] |
| 104 |
>>> cmdbuilder(b('cmd'), list=[1, 2]) == expect |
| 105 |
True |
| 106 |
>>> cmdbuilder(b('cmd'), None) == [b('cmd')] |
| 107 |
True |
| 108 |
""" |
| 109 |
cmd = [name] |
| 110 |
for arg, val in kwargs.items(): |
| 111 |
if val is None: |
| 112 |
continue |
| 113 |
|
| 114 |
arg = arg.encode('latin-1').replace(b('_'), b('-')) |
| 115 |
if arg != b('-'): |
| 116 |
if len(arg) == 1: |
| 117 |
arg = b('-') + arg |
| 118 |
else: |
| 119 |
arg = b('--') + arg |
| 120 |
if isinstance(val, bool): |
| 121 |
if val: |
| 122 |
cmd.append(arg) |
| 123 |
elif isinstance(val, list): |
| 124 |
for v in val: |
| 125 |
cmd.append(arg) |
| 126 |
cmd.append(_cmdval(v)) |
| 127 |
else: |
| 128 |
cmd.append(arg) |
| 129 |
cmd.append(_cmdval(val)) |
| 130 |
|
| 131 |
for a in args: |
| 132 |
if a is not None: |
| 133 |
cmd.append(a) |
| 134 |
|
| 135 |
return cmd |
| 136 |
|
| 137 |
class reterrorhandler(object): |
| 138 |
"""This class is meant to be used with rawcommand() error handler |
| 139 |
argument. It remembers the return value the command returned if |
| 140 |
it's one of allowed values, which is only 1 if none are given. |
| 141 |
Otherwise it raises a CommandError. |
| 142 |
|
| 143 |
>>> e = reterrorhandler('') |
| 144 |
>>> bool(e) |
| 145 |
True |
| 146 |
>>> e(1, 'a', '') |
| 147 |
'a' |
| 148 |
>>> bool(e) |
| 149 |
False |
| 150 |
|
| 151 |
""" |
| 152 |
def __init__(self, args, allowed=None): |
| 153 |
self.args = args |
| 154 |
self.ret = 0 |
| 155 |
if allowed is None: |
| 156 |
self.allowed = [1] |
| 157 |
else: |
| 158 |
self.allowed = allowed |
| 159 |
|
| 160 |
def __call__(self, ret, out, err): |
| 161 |
self.ret = ret |
| 162 |
if ret not in self.allowed: |
| 163 |
raise error.CommandError(self.args, ret, out, err) |
| 164 |
return out |
| 165 |
|
| 166 |
def __nonzero__(self): |
| 167 |
""" Returns True if the return code was 0, False otherwise """ |
| 168 |
return self.ret == 0 |
| 169 |
|
| 170 |
def __bool__(self): |
| 171 |
return self.__nonzero__() |
| 172 |
|
| 173 |
class propertycache(object): |
| 174 |
""" |
| 175 |
Decorator that remembers the return value of a function call. |
| 176 |
|
| 177 |
>>> execcount = 0 |
| 178 |
>>> class obj(object): |
| 179 |
... def func(self): |
| 180 |
... global execcount |
| 181 |
... execcount += 1 |
| 182 |
... return [] |
| 183 |
... func = propertycache(func) |
| 184 |
>>> o = obj() |
| 185 |
>>> o.func |
| 186 |
[] |
| 187 |
>>> execcount |
| 188 |
1 |
| 189 |
>>> o.func |
| 190 |
[] |
| 191 |
>>> execcount |
| 192 |
1 |
| 193 |
""" |
| 194 |
def __init__(self, func): |
| 195 |
self.func = func |
| 196 |
self.name = func.__name__ |
| 197 |
def __get__(self, obj, type=None): |
| 198 |
result = self.func(obj) |
| 199 |
setattr(obj, self.name, result) |
| 200 |
return result |
| 201 |
|
| 202 |
close_fds = os.name == 'posix' |
| 203 |
|
| 204 |
startupinfo = None |
| 205 |
if os.name == 'nt': |
| 206 |
startupinfo = subprocess.STARTUPINFO() |
| 207 |
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
| 208 |
|
| 209 |
def popen(args, env=None): |
| 210 |
environ = None |
| 211 |
if env: |
| 212 |
environ = dict(os.environ) |
| 213 |
environ.update(env) |
| 214 |
|
| 215 |
return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 216 |
stderr=subprocess.PIPE, close_fds=close_fds, |
| 217 |
startupinfo=startupinfo, env=environ) |