Show More
@@ -1,52 +1,53 | |||
|
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 | 29 | |
|
30 | 30 | name = sys.argv[1] |
|
31 | 31 | if len(sys.argv) > 2: |
|
32 | 32 | exitcode = int(sys.argv[2]) |
|
33 | 33 | if len(sys.argv) > 3: |
|
34 | 34 | out = open(sys.argv[3], "ab") |
|
35 | 35 | |
|
36 | 36 | # variables with empty values may not exist on all platforms, filter |
|
37 | 37 | # them now for portability sake. |
|
38 | 38 | env = [(k, v) for k, v in os.environ.items() |
|
39 | 39 | if k.startswith("HG_") and v] |
|
40 | 40 | env.sort() |
|
41 | 41 | |
|
42 | out.write("%s hook: " % name) | |
|
42 | out.write(b"%s hook: " % name.encode('ascii')) | |
|
43 | 43 | if os.name == 'nt': |
|
44 | 44 | filter = lambda x: x.replace('\\', '/') |
|
45 | 45 | else: |
|
46 | 46 | filter = lambda x: x |
|
47 | vars = ["%s=%s" % (k, filter(v)) for k, v in env] | |
|
48 | out.write(" ".join(vars)) | |
|
49 |
out.write(" |
|
|
47 | vars = [b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii')) | |
|
48 | for k, v in env] | |
|
49 | out.write(b" ".join(vars)) | |
|
50 | out.write(b"\n") | |
|
50 | 51 | out.close() |
|
51 | 52 | |
|
52 | 53 | sys.exit(exitcode) |
General Comments 0
You need to be logged in to leave comments.
Login now