##// END OF EJS Templates
sigpipe-remote: check for python version earlier in the script...
marmoute -
r48436:b2ed9480 default
parent child Browse files
Show More
@@ -1,152 +1,152 b''
1 #!/usr/bin/env python3
1 #!/usr/bin/env python3
2 from __future__ import print_function
2 from __future__ import print_function
3
3
4 import io
4 import io
5 import os
5 import os
6 import subprocess
6 import subprocess
7 import sys
7 import sys
8 import time
8 import time
9
9
10 # we cannot use mercurial.testing as long as python2 is not dropped as the test will only install the mercurial module for python2 in python2 run
10 # we cannot use mercurial.testing as long as python2 is not dropped as the test
11 # will only install the mercurial module for python2 in python2 run
12 if sys.version_info[0] < 3:
13 print('SIGPIPE-HELPER: script should run with Python 3', file=sys.stderr)
14 sys.exit(255)
11
15
12 if isinstance(sys.stdout.buffer, io.BufferedWriter):
16 if isinstance(sys.stdout.buffer, io.BufferedWriter):
13 print('SIGPIPE-HELPER: script need unbuffered output', file=sys.stderr)
17 print('SIGPIPE-HELPER: script need unbuffered output', file=sys.stderr)
14 sys.exit(255)
18 sys.exit(255)
15
19
16 DEBUG_FILE = os.environ.get('SIGPIPE_REMOTE_DEBUG_FILE')
20 DEBUG_FILE = os.environ.get('SIGPIPE_REMOTE_DEBUG_FILE')
17 if DEBUG_FILE is None:
21 if DEBUG_FILE is None:
18 debug_stream = sys.stderr.buffer
22 debug_stream = sys.stderr.buffer
19 else:
23 else:
20 debug_stream = open(DEBUG_FILE, 'bw', buffering=0)
24 debug_stream = open(DEBUG_FILE, 'bw', buffering=0)
21
25
22 SYNCFILE1 = os.environ.get('SYNCFILE1')
26 SYNCFILE1 = os.environ.get('SYNCFILE1')
23 SYNCFILE2 = os.environ.get('SYNCFILE2')
27 SYNCFILE2 = os.environ.get('SYNCFILE2')
24 if SYNCFILE1 is None:
28 if SYNCFILE1 is None:
25 print('SIGPIPE-HELPER: missing variable $SYNCFILE1', file=sys.stderr)
29 print('SIGPIPE-HELPER: missing variable $SYNCFILE1', file=sys.stderr)
26 sys.exit(255)
30 sys.exit(255)
27 if SYNCFILE2 is None:
31 if SYNCFILE2 is None:
28 print('SIGPIPE-HELPER: missing variable $SYNCFILE2', file=sys.stderr)
32 print('SIGPIPE-HELPER: missing variable $SYNCFILE2', file=sys.stderr)
29 sys.exit(255)
33 sys.exit(255)
30
34
31
35
32 def _timeout_factor():
36 def _timeout_factor():
33 """return the current modification to timeout"""
37 """return the current modification to timeout"""
34 default = int(os.environ.get('HGTEST_TIMEOUT_DEFAULT', 360))
38 default = int(os.environ.get('HGTEST_TIMEOUT_DEFAULT', 360))
35 current = int(os.environ.get('HGTEST_TIMEOUT', default))
39 current = int(os.environ.get('HGTEST_TIMEOUT', default))
36 if current == 0:
40 if current == 0:
37 return 1
41 return 1
38 return current / float(default)
42 return current / float(default)
39
43
40
44
41 def wait_file(path, timeout=10):
45 def wait_file(path, timeout=10):
42 timeout *= _timeout_factor()
46 timeout *= _timeout_factor()
43 start = time.time()
47 start = time.time()
44 while not os.path.exists(path):
48 while not os.path.exists(path):
45 if (time.time() - start) > timeout:
49 if (time.time() - start) > timeout:
46 raise RuntimeError(b"timed out waiting for file: %s" % path)
50 raise RuntimeError(b"timed out waiting for file: %s" % path)
47 time.sleep(0.01)
51 time.sleep(0.01)
48
52
49
53
50 def write_file(path, content=b''):
54 def write_file(path, content=b''):
51 with open(path, 'wb') as f:
55 with open(path, 'wb') as f:
52 f.write(content)
56 f.write(content)
53
57
54
58
55 # end of mercurial.testing content
59 # end of mercurial.testing content
56
60
57 if sys.version_info[0] < 3:
58 print('SIGPIPE-HELPER: script should run with Python 3', file=sys.stderr)
59 sys.exit(255)
60
61
61
62 def sysbytes(s):
62 def sysbytes(s):
63 return s.encode('utf-8')
63 return s.encode('utf-8')
64
64
65
65
66 def sysstr(s):
66 def sysstr(s):
67 return s.decode('latin-1')
67 return s.decode('latin-1')
68
68
69
69
70 debug_stream.write(b'SIGPIPE-HELPER: Starting\n')
70 debug_stream.write(b'SIGPIPE-HELPER: Starting\n')
71
71
72 TESTLIB_DIR = os.path.dirname(sys.argv[0])
72 TESTLIB_DIR = os.path.dirname(sys.argv[0])
73 WAIT_SCRIPT = os.path.join(TESTLIB_DIR, 'wait-on-file')
73 WAIT_SCRIPT = os.path.join(TESTLIB_DIR, 'wait-on-file')
74
74
75 hooks_cmd = '%s 10 %s %s'
75 hooks_cmd = '%s 10 %s %s'
76 hooks_cmd %= (
76 hooks_cmd %= (
77 WAIT_SCRIPT,
77 WAIT_SCRIPT,
78 SYNCFILE2,
78 SYNCFILE2,
79 SYNCFILE1,
79 SYNCFILE1,
80 )
80 )
81
81
82 try:
82 try:
83 cmd = ['hg']
83 cmd = ['hg']
84 cmd += sys.argv[1:]
84 cmd += sys.argv[1:]
85 sub = subprocess.Popen(
85 sub = subprocess.Popen(
86 cmd,
86 cmd,
87 bufsize=0,
87 bufsize=0,
88 close_fds=True,
88 close_fds=True,
89 stdin=sys.stdin,
89 stdin=sys.stdin,
90 stdout=subprocess.PIPE,
90 stdout=subprocess.PIPE,
91 stderr=subprocess.PIPE,
91 stderr=subprocess.PIPE,
92 )
92 )
93
93
94 basedir = os.path.dirname(sys.argv[0])
94 basedir = os.path.dirname(sys.argv[0])
95 worker = os.path.join(basedir, 'sigpipe-worker.py')
95 worker = os.path.join(basedir, 'sigpipe-worker.py')
96
96
97 cmd = [sys.executable, worker]
97 cmd = [sys.executable, worker]
98
98
99 stdout_worker = subprocess.Popen(
99 stdout_worker = subprocess.Popen(
100 cmd,
100 cmd,
101 bufsize=0,
101 bufsize=0,
102 close_fds=True,
102 close_fds=True,
103 stdin=sub.stdout,
103 stdin=sub.stdout,
104 stdout=sys.stdout,
104 stdout=sys.stdout,
105 stderr=sys.stderr,
105 stderr=sys.stderr,
106 )
106 )
107
107
108 stderr_worker = subprocess.Popen(
108 stderr_worker = subprocess.Popen(
109 cmd,
109 cmd,
110 bufsize=0,
110 bufsize=0,
111 close_fds=True,
111 close_fds=True,
112 stdin=sub.stderr,
112 stdin=sub.stderr,
113 stdout=sys.stderr,
113 stdout=sys.stderr,
114 stderr=sys.stderr,
114 stderr=sys.stderr,
115 )
115 )
116 debug_stream.write(b'SIGPIPE-HELPER: Redirection in place\n')
116 debug_stream.write(b'SIGPIPE-HELPER: Redirection in place\n')
117 os.close(sub.stdout.fileno())
117 os.close(sub.stdout.fileno())
118 os.close(sub.stderr.fileno())
118 os.close(sub.stderr.fileno())
119 debug_stream.write(b'SIGPIPE-HELPER: pipes closed in main\n')
119 debug_stream.write(b'SIGPIPE-HELPER: pipes closed in main\n')
120
120
121 try:
121 try:
122 wait_file(sysbytes(SYNCFILE1))
122 wait_file(sysbytes(SYNCFILE1))
123 except RuntimeError as exc:
123 except RuntimeError as exc:
124 msg = sysbytes(str(exc))
124 msg = sysbytes(str(exc))
125 debug_stream.write(b'SIGPIPE-HELPER: wait failed: %s\n' % msg)
125 debug_stream.write(b'SIGPIPE-HELPER: wait failed: %s\n' % msg)
126 else:
126 else:
127 debug_stream.write(b'SIGPIPE-HELPER: SYNCFILE1 detected\n')
127 debug_stream.write(b'SIGPIPE-HELPER: SYNCFILE1 detected\n')
128 stdout_worker.kill()
128 stdout_worker.kill()
129 stderr_worker.kill()
129 stderr_worker.kill()
130 stdout_worker.wait(10)
130 stdout_worker.wait(10)
131 stderr_worker.wait(10)
131 stderr_worker.wait(10)
132 debug_stream.write(b'SIGPIPE-HELPER: worker killed\n')
132 debug_stream.write(b'SIGPIPE-HELPER: worker killed\n')
133
133
134 debug_stream.write(b'SIGPIPE-HELPER: creating SYNCFILE2\n')
134 debug_stream.write(b'SIGPIPE-HELPER: creating SYNCFILE2\n')
135 write_file(sysbytes(SYNCFILE2))
135 write_file(sysbytes(SYNCFILE2))
136 finally:
136 finally:
137 debug_stream.write(b'SIGPIPE-HELPER: Shutting down\n')
137 debug_stream.write(b'SIGPIPE-HELPER: Shutting down\n')
138 if not sys.stdin.closed:
138 if not sys.stdin.closed:
139 sys.stdin.close()
139 sys.stdin.close()
140 try:
140 try:
141 sub.wait(timeout=30)
141 sub.wait(timeout=30)
142 except subprocess.TimeoutExpired:
142 except subprocess.TimeoutExpired:
143 msg = b'SIGPIPE-HELPER: Server process failed to terminate\n'
143 msg = b'SIGPIPE-HELPER: Server process failed to terminate\n'
144 debug_stream.write(msg)
144 debug_stream.write(msg)
145 sub.kill()
145 sub.kill()
146 sub.wait()
146 sub.wait()
147 msg = b'SIGPIPE-HELPER: Server process killed\n'
147 msg = b'SIGPIPE-HELPER: Server process killed\n'
148 else:
148 else:
149 msg = b'SIGPIPE-HELPER: Server process terminated with status %d\n'
149 msg = b'SIGPIPE-HELPER: Server process terminated with status %d\n'
150 msg %= sub.returncode
150 msg %= sub.returncode
151 debug_stream.write(msg)
151 debug_stream.write(msg)
152 debug_stream.write(b'SIGPIPE-HELPER: Shut down\n')
152 debug_stream.write(b'SIGPIPE-HELPER: Shut down\n')
General Comments 0
You need to be logged in to leave comments. Login now