contrib/hgclient.py
changeset 43076 2372284d9457
parent 40984 6a372f943e49
child 43506 9f70512ae2cf
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
    14 
    14 
    15 if sys.version_info[0] >= 3:
    15 if sys.version_info[0] >= 3:
    16     stdout = sys.stdout.buffer
    16     stdout = sys.stdout.buffer
    17     stderr = sys.stderr.buffer
    17     stderr = sys.stderr.buffer
    18     stringio = io.BytesIO
    18     stringio = io.BytesIO
       
    19 
    19     def bprint(*args):
    20     def bprint(*args):
    20         # remove b'' as well for ease of test migration
    21         # remove b'' as well for ease of test migration
    21         pargs = [re.sub(br'''\bb(['"])''', br'\1', b'%s' % a) for a in args]
    22         pargs = [re.sub(br'''\bb(['"])''', br'\1', b'%s' % a) for a in args]
    22         stdout.write(b' '.join(pargs) + b'\n')
    23         stdout.write(b' '.join(pargs) + b'\n')
       
    24 
       
    25 
    23 else:
    26 else:
    24     import cStringIO
    27     import cStringIO
       
    28 
    25     stdout = sys.stdout
    29     stdout = sys.stdout
    26     stderr = sys.stderr
    30     stderr = sys.stderr
    27     stringio = cStringIO.StringIO
    31     stringio = cStringIO.StringIO
    28     bprint = print
    32     bprint = print
       
    33 
    29 
    34 
    30 def connectpipe(path=None, extraargs=()):
    35 def connectpipe(path=None, extraargs=()):
    31     cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe']
    36     cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe']
    32     if path:
    37     if path:
    33         cmdline += [b'-R', path]
    38         cmdline += [b'-R', path]
    36     def tonative(cmdline):
    41     def tonative(cmdline):
    37         if os.name != r'nt':
    42         if os.name != r'nt':
    38             return cmdline
    43             return cmdline
    39         return [arg.decode("utf-8") for arg in cmdline]
    44         return [arg.decode("utf-8") for arg in cmdline]
    40 
    45 
    41     server = subprocess.Popen(tonative(cmdline), stdin=subprocess.PIPE,
    46     server = subprocess.Popen(
    42                               stdout=subprocess.PIPE)
    47         tonative(cmdline), stdin=subprocess.PIPE, stdout=subprocess.PIPE
       
    48     )
    43 
    49 
    44     return server
    50     return server
       
    51 
    45 
    52 
    46 class unixconnection(object):
    53 class unixconnection(object):
    47     def __init__(self, sockpath):
    54     def __init__(self, sockpath):
    48         self.sock = sock = socket.socket(socket.AF_UNIX)
    55         self.sock = sock = socket.socket(socket.AF_UNIX)
    49         sock.connect(sockpath)
    56         sock.connect(sockpath)
    52 
    59 
    53     def wait(self):
    60     def wait(self):
    54         self.stdin.close()
    61         self.stdin.close()
    55         self.stdout.close()
    62         self.stdout.close()
    56         self.sock.close()
    63         self.sock.close()
       
    64 
    57 
    65 
    58 class unixserver(object):
    66 class unixserver(object):
    59     def __init__(self, sockpath, logpath=None, repopath=None):
    67     def __init__(self, sockpath, logpath=None, repopath=None):
    60         self.sockpath = sockpath
    68         self.sockpath = sockpath
    61         cmdline = [b'hg', b'serve', b'--cmdserver', b'unix', b'-a', sockpath]
    69         cmdline = [b'hg', b'serve', b'--cmdserver', b'unix', b'-a', sockpath]
    78 
    86 
    79     def shutdown(self):
    87     def shutdown(self):
    80         os.kill(self.server.pid, signal.SIGTERM)
    88         os.kill(self.server.pid, signal.SIGTERM)
    81         self.server.wait()
    89         self.server.wait()
    82 
    90 
       
    91 
    83 def writeblock(server, data):
    92 def writeblock(server, data):
    84     server.stdin.write(struct.pack(b'>I', len(data)))
    93     server.stdin.write(struct.pack(b'>I', len(data)))
    85     server.stdin.write(data)
    94     server.stdin.write(data)
    86     server.stdin.flush()
    95     server.stdin.flush()
       
    96 
    87 
    97 
    88 def readchannel(server):
    98 def readchannel(server):
    89     data = server.stdout.read(5)
    99     data = server.stdout.read(5)
    90     if not data:
   100     if not data:
    91         raise EOFError
   101         raise EOFError
    93     if channel in b'IL':
   103     if channel in b'IL':
    94         return channel, length
   104         return channel, length
    95     else:
   105     else:
    96         return channel, server.stdout.read(length)
   106         return channel, server.stdout.read(length)
    97 
   107 
       
   108 
    98 def sep(text):
   109 def sep(text):
    99     return text.replace(b'\\', b'/')
   110     return text.replace(b'\\', b'/')
   100 
   111 
   101 def runcommand(server, args, output=stdout, error=stderr, input=None,
   112 
   102                outfilter=lambda x: x):
   113 def runcommand(
       
   114     server, args, output=stdout, error=stderr, input=None, outfilter=lambda x: x
       
   115 ):
   103     bprint(b'*** runcommand', b' '.join(args))
   116     bprint(b'*** runcommand', b' '.join(args))
   104     stdout.flush()
   117     stdout.flush()
   105     server.stdin.write(b'runcommand\n')
   118     server.stdin.write(b'runcommand\n')
   106     writeblock(server, b'\0'.join(args))
   119     writeblock(server, b'\0'.join(args))
   107 
   120 
   121         elif ch == b'L':
   134         elif ch == b'L':
   122             writeblock(server, input.readline(data))
   135             writeblock(server, input.readline(data))
   123         elif ch == b'm':
   136         elif ch == b'm':
   124             bprint(b"message: %r" % data)
   137             bprint(b"message: %r" % data)
   125         elif ch == b'r':
   138         elif ch == b'r':
   126             ret, = struct.unpack('>i', data)
   139             (ret,) = struct.unpack('>i', data)
   127             if ret != 0:
   140             if ret != 0:
   128                 bprint(b' [%d]' % ret)
   141                 bprint(b' [%d]' % ret)
   129             return ret
   142             return ret
   130         else:
   143         else:
   131             bprint(b"unexpected channel %c: %r" % (ch, data))
   144             bprint(b"unexpected channel %c: %r" % (ch, data))
   132             if ch.isupper():
   145             if ch.isupper():
   133                 return
   146                 return
       
   147 
   134 
   148 
   135 def check(func, connect=connectpipe):
   149 def check(func, connect=connectpipe):
   136     stdout.flush()
   150     stdout.flush()
   137     server = connect()
   151     server = connect()
   138     try:
   152     try:
   139         return func(server)
   153         return func(server)
   140     finally:
   154     finally:
   141         server.stdin.close()
   155         server.stdin.close()
   142         server.wait()
   156         server.wait()
   143 
   157 
       
   158 
   144 def checkwith(connect=connectpipe, **kwargs):
   159 def checkwith(connect=connectpipe, **kwargs):
   145     def wrap(func):
   160     def wrap(func):
   146         return check(func, lambda: connect(**kwargs))
   161         return check(func, lambda: connect(**kwargs))
       
   162 
   147     return wrap
   163     return wrap