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