comparison tests/printenv.py @ 41039:73da729ccfef

test: introduce a new flag to display env variable line per line It's easier to conditionalize some of the environment variables per Mercurial version once there is only one value per line. Differential Revision: https://phab.mercurial-scm.org/D5453
author Boris Feld <boris.feld@octobus.net>
date Wed, 19 Dec 2018 15:45:29 +0100
parents 3c5aaea9638f
children 2372284d9457
comparison
equal deleted inserted replaced
41038:15f78383d3c8 41039:73da729ccfef
35 help="the exit code for the hook", 35 help="the exit code for the hook",
36 ) 36 )
37 parser.add_argument( 37 parser.add_argument(
38 "out", nargs="?", default=None, help="where to write the output" 38 "out", nargs="?", default=None, help="where to write the output"
39 ) 39 )
40 parser.add_argument(
41 "--line",
42 action="store_true",
43 help="print environment variables one per line instead of on a single line",
44 )
40 args = parser.parse_args() 45 args = parser.parse_args()
41 46
42 if args.out is None: 47 if args.out is None:
43 out = sys.stdout 48 out = sys.stdout
44 out = getattr(out, "buffer", out) 49 out = getattr(out, "buffer", out)
54 out.write(b"%s hook: " % args.name.encode('ascii')) 59 out.write(b"%s hook: " % args.name.encode('ascii'))
55 if os.name == 'nt': 60 if os.name == 'nt':
56 filter = lambda x: x.replace('\\', '/') 61 filter = lambda x: x.replace('\\', '/')
57 else: 62 else:
58 filter = lambda x: x 63 filter = lambda x: x
64
59 vars = [b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii')) 65 vars = [b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii'))
60 for k, v in env] 66 for k, v in env]
61 out.write(b" ".join(vars)) 67
68 # Print variables on out
69 if not args.line:
70 out.write(b" ".join(vars))
71 else:
72 for var in vars:
73 out.write(var)
74 out.write(b"\n")
75
62 out.write(b"\n") 76 out.write(b"\n")
63 out.close() 77 out.close()
64 78
65 sys.exit(args.exitcode) 79 sys.exit(args.exitcode)