##// END OF EJS Templates
tests: fix printenv script on Python 3...
Augie Fackler -
r38315:42f3a277 default
parent child Browse files
Show More
@@ -1,53 +1,54 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 #
2 #
3 # simple script to be used in hooks
3 # simple script to be used in hooks
4 #
4 #
5 # put something like this in the repo .hg/hgrc:
5 # put something like this in the repo .hg/hgrc:
6 #
6 #
7 # [hooks]
7 # [hooks]
8 # changegroup = python "$TESTDIR/printenv.py" <hookname> [exit] [output]
8 # changegroup = python "$TESTDIR/printenv.py" <hookname> [exit] [output]
9 #
9 #
10 # - <hookname> is a mandatory argument (e.g. "changegroup")
10 # - <hookname> is a mandatory argument (e.g. "changegroup")
11 # - [exit] is the exit code of the hook (default: 0)
11 # - [exit] is the exit code of the hook (default: 0)
12 # - [output] is the name of the output file (default: use sys.stdout)
12 # - [output] is the name of the output file (default: use sys.stdout)
13 # the file will be opened in append mode.
13 # the file will be opened in append mode.
14 #
14 #
15 from __future__ import absolute_import
15 from __future__ import absolute_import
16 import os
16 import os
17 import sys
17 import sys
18
18
19 try:
19 try:
20 import msvcrt
20 import msvcrt
21 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
21 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
22 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
22 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
23 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
23 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
24 except ImportError:
24 except ImportError:
25 pass
25 pass
26
26
27 exitcode = 0
27 exitcode = 0
28 out = sys.stdout
28 out = sys.stdout
29 out = getattr(out, 'buffer', out)
29
30
30 name = sys.argv[1]
31 name = sys.argv[1]
31 if len(sys.argv) > 2:
32 if len(sys.argv) > 2:
32 exitcode = int(sys.argv[2])
33 exitcode = int(sys.argv[2])
33 if len(sys.argv) > 3:
34 if len(sys.argv) > 3:
34 out = open(sys.argv[3], "ab")
35 out = open(sys.argv[3], "ab")
35
36
36 # variables with empty values may not exist on all platforms, filter
37 # variables with empty values may not exist on all platforms, filter
37 # them now for portability sake.
38 # them now for portability sake.
38 env = [(k, v) for k, v in os.environ.items()
39 env = [(k, v) for k, v in os.environ.items()
39 if k.startswith("HG_") and v]
40 if k.startswith("HG_") and v]
40 env.sort()
41 env.sort()
41
42
42 out.write(b"%s hook: " % name.encode('ascii'))
43 out.write(b"%s hook: " % name.encode('ascii'))
43 if os.name == 'nt':
44 if os.name == 'nt':
44 filter = lambda x: x.replace('\\', '/')
45 filter = lambda x: x.replace('\\', '/')
45 else:
46 else:
46 filter = lambda x: x
47 filter = lambda x: x
47 vars = [b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii'))
48 vars = [b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii'))
48 for k, v in env]
49 for k, v in env]
49 out.write(b" ".join(vars))
50 out.write(b" ".join(vars))
50 out.write(b"\n")
51 out.write(b"\n")
51 out.close()
52 out.close()
52
53
53 sys.exit(exitcode)
54 sys.exit(exitcode)
General Comments 0
You need to be logged in to leave comments. Login now