Debian Codegraph @

contrib/hgclient.py

raw
1 # A minimal client for Mercurial's command server
2
3
4 import io
5 import os
6 import re
7 import signal
8 import socket
9 import struct
10 import subprocess
11 import sys
12 import time
13
14 if sys.version_info[0] >= 3:
15 stdout = sys.stdout.buffer
16 stderr = sys.stderr.buffer
17 stringio = io.BytesIO
18
19 def bprint(*args):
20 # 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 stdout.write(b' '.join(pargs) + b'\n')
23
24 else:
25 import cStringIO
26
27 stdout = sys.stdout
28 stderr = sys.stderr
29 stringio = cStringIO.StringIO
30 bprint = print
31
32
33 def connectpipe(path=None, extraargs=()):
34 cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe']
35 if path:
36 cmdline += [b'-R', path]
37 cmdline.extend(extraargs)
38
39 def tonative(cmdline):
40 if os.name != 'nt':
41 return cmdline
42 return [arg.decode("utf-8") for arg in cmdline]
43
44 server = subprocess.Popen(
45 tonative(cmdline), stdin=subprocess.PIPE, stdout=subprocess.PIPE
46 )
47
48 return server
49
50
51 class unixconnection:
52 def __init__(self, sockpath):
53 self.sock = sock = socket.socket(socket.AF_UNIX)
54 sock.connect(sockpath)
55 self.stdin = sock.makefile('wb')
56 self.stdout = sock.makefile('rb')
57
58 def wait(self):
59 self.stdin.close()
60 self.stdout.close()
61 self.sock.close()
62
63
64 class unixserver:
65 def __init__(self, sockpath, logpath=None, repopath=None):
66 self.sockpath = sockpath
67 cmdline = [b'hg', b'serve', b'--cmdserver', b'unix', b'-a', sockpath]
68 if repopath:
69 cmdline += [b'-R', repopath]
70 if logpath:
71 stdout = open(logpath, 'a')
72 stderr = subprocess.STDOUT
73 else:
74 stdout = stderr = None
75 self.server = subprocess.Popen(cmdline, stdout=stdout, stderr=stderr)
76 # wait for listen()
77 while self.server.poll() is None:
78 if os.path.exists(sockpath):
79 break
80 time.sleep(0.1)
81
82 def connect(self):
83 return unixconnection(self.sockpath)
84
85 def shutdown(self):
86 os.kill(self.server.pid, signal.SIGTERM)
87 self.server.wait()
88
89
90 def writeblock(server, data):
91 server.stdin.write(struct.pack(b'>I', len(data)))
92 server.stdin.write(data)
93 server.stdin.flush()
94
95
96 def readchannel(server):
97 data = server.stdout.read(5)
98 if not data:
99 raise EOFError
100 channel, length = struct.unpack('>cI', data)
101 if channel in b'IL':
102 return channel, length
103 else:
104 return channel, server.stdout.read(length)
105
106
107 def sep(text):
108 return text.replace(b'\\', b'/')
109
110
111 def runcommand(
112 server, args, output=stdout, error=stderr, input=None, outfilter=lambda x: x
113 ):
114 bprint(b'*** runcommand', b' '.join(args))
115 stdout.flush()
116 server.stdin.write(b'runcommand\n')
117 writeblock(server, b'\0'.join(args))
118
119 if not input:
120 input = stringio()
121
122 while True:
123 ch, data = readchannel(server)
124 if ch == b'o':
125 output.write(outfilter(data))
126 output.flush()
127 elif ch == b'e':
128 error.write(data)
129 error.flush()
130 elif ch == b'I':
131 writeblock(server, input.read(data))
132 elif ch == b'L':
133 writeblock(server, input.readline(data))
134 elif ch == b'm':
135 bprint(b"message: %r" % data)
136 elif ch == b'r':
137 (ret,) = struct.unpack('>i', data)
138 if ret != 0:
139 bprint(b' [%d]' % ret)
140 return ret
141 else:
142 bprint(b"unexpected channel %c: %r" % (ch, data))
143 if ch.isupper():
144 return
145
146
147 def check(func, connect=connectpipe):
148 stdout.flush()
149 server = connect()
150 try:
151 return func(server)
152 finally:
153 server.stdin.close()
154 server.wait()
155
156
157 def checkwith(connect=connectpipe, **kwargs):
158 def wrap(func):
159 return check(func, lambda: connect(**kwargs))
160
161 return wrap