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