15 except ImportError: |
15 except ImportError: |
16 import io |
16 import io |
17 stringio = io.StringIO |
17 stringio = io.StringIO |
18 |
18 |
19 def connectpipe(path=None): |
19 def connectpipe(path=None): |
20 cmdline = ['hg', 'serve', '--cmdserver', 'pipe'] |
20 cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe'] |
21 if path: |
21 if path: |
22 cmdline += ['-R', path] |
22 cmdline += [b'-R', path] |
23 |
23 |
24 server = subprocess.Popen(cmdline, stdin=subprocess.PIPE, |
24 server = subprocess.Popen(cmdline, stdin=subprocess.PIPE, |
25 stdout=subprocess.PIPE) |
25 stdout=subprocess.PIPE) |
26 |
26 |
27 return server |
27 return server |
39 self.sock.close() |
39 self.sock.close() |
40 |
40 |
41 class unixserver(object): |
41 class unixserver(object): |
42 def __init__(self, sockpath, logpath=None, repopath=None): |
42 def __init__(self, sockpath, logpath=None, repopath=None): |
43 self.sockpath = sockpath |
43 self.sockpath = sockpath |
44 cmdline = ['hg', 'serve', '--cmdserver', 'unix', '-a', sockpath] |
44 cmdline = [b'hg', b'serve', b'--cmdserver', b'unix', b'-a', sockpath] |
45 if repopath: |
45 if repopath: |
46 cmdline += ['-R', repopath] |
46 cmdline += [b'-R', repopath] |
47 if logpath: |
47 if logpath: |
48 stdout = open(logpath, 'a') |
48 stdout = open(logpath, 'a') |
49 stderr = subprocess.STDOUT |
49 stderr = subprocess.STDOUT |
50 else: |
50 else: |
51 stdout = stderr = None |
51 stdout = stderr = None |
62 def shutdown(self): |
62 def shutdown(self): |
63 os.kill(self.server.pid, signal.SIGTERM) |
63 os.kill(self.server.pid, signal.SIGTERM) |
64 self.server.wait() |
64 self.server.wait() |
65 |
65 |
66 def writeblock(server, data): |
66 def writeblock(server, data): |
67 server.stdin.write(struct.pack('>I', len(data))) |
67 server.stdin.write(struct.pack(b'>I', len(data))) |
68 server.stdin.write(data) |
68 server.stdin.write(data) |
69 server.stdin.flush() |
69 server.stdin.flush() |
70 |
70 |
71 def readchannel(server): |
71 def readchannel(server): |
72 data = server.stdout.read(5) |
72 data = server.stdout.read(5) |
73 if not data: |
73 if not data: |
74 raise EOFError |
74 raise EOFError |
75 channel, length = struct.unpack('>cI', data) |
75 channel, length = struct.unpack('>cI', data) |
76 if channel in 'IL': |
76 if channel in b'IL': |
77 return channel, length |
77 return channel, length |
78 else: |
78 else: |
79 return channel, server.stdout.read(length) |
79 return channel, server.stdout.read(length) |
80 |
80 |
81 def sep(text): |
81 def sep(text): |
82 return text.replace('\\', '/') |
82 return text.replace(b'\\', b'/') |
83 |
83 |
84 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, |
84 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, |
85 outfilter=lambda x: x): |
85 outfilter=lambda x: x): |
86 print('*** runcommand', ' '.join(args)) |
86 print(b'*** runcommand', b' '.join(args)) |
87 sys.stdout.flush() |
87 sys.stdout.flush() |
88 server.stdin.write('runcommand\n') |
88 server.stdin.write(b'runcommand\n') |
89 writeblock(server, '\0'.join(args)) |
89 writeblock(server, b'\0'.join(args)) |
90 |
90 |
91 if not input: |
91 if not input: |
92 input = stringio() |
92 input = stringio() |
93 |
93 |
94 while True: |
94 while True: |
95 ch, data = readchannel(server) |
95 ch, data = readchannel(server) |
96 if ch == 'o': |
96 if ch == b'o': |
97 output.write(outfilter(data)) |
97 output.write(outfilter(data)) |
98 output.flush() |
98 output.flush() |
99 elif ch == 'e': |
99 elif ch == b'e': |
100 error.write(data) |
100 error.write(data) |
101 error.flush() |
101 error.flush() |
102 elif ch == 'I': |
102 elif ch == b'I': |
103 writeblock(server, input.read(data)) |
103 writeblock(server, input.read(data)) |
104 elif ch == 'L': |
104 elif ch == b'L': |
105 writeblock(server, input.readline(data)) |
105 writeblock(server, input.readline(data)) |
106 elif ch == 'r': |
106 elif ch == b'r': |
107 ret, = struct.unpack('>i', data) |
107 ret, = struct.unpack('>i', data) |
108 if ret != 0: |
108 if ret != 0: |
109 print(' [%d]' % ret) |
109 print(b' [%d]' % ret) |
110 return ret |
110 return ret |
111 else: |
111 else: |
112 print("unexpected channel %c: %r" % (ch, data)) |
112 print(b"unexpected channel %c: %r" % (ch, data)) |
113 if ch.isupper(): |
113 if ch.isupper(): |
114 return |
114 return |
115 |
115 |
116 def check(func, connect=connectpipe): |
116 def check(func, connect=connectpipe): |
117 sys.stdout.flush() |
117 sys.stdout.flush() |