Debian Codegraph @

test/util/check-hostaliases

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import argparse
4 import tempfile
5 import os
6 import socket
7 import subprocess
8 import sys
9
10 parser = argparse.ArgumentParser(
11 description="Check that $HOSTALIASES is working"
12 )
13 parser.add_argument(
14 "--child",
15 action="store_true",
16 help="run in child mode, for internal use only",
17 )
18 parser.add_argument(
19 "-v",
20 action="store_true",
21 help="verbose mode",
22 )
23 args = parser.parse_args()
24
25
26 def dprint(*a):
27 if args.v:
28 print(*a)
29
30
31 if args.child:
32 dprint("child mode, getting hosts")
33 lo = socket.gethostbyname("localhost")
34 ts = socket.gethostbyname("testserver")
35 dprint(ts, lo, ts == lo)
36 if ts != lo:
37 sys.exit(1)
38 else:
39 dprint("## parent mode")
40 # Create the hostaliases file.
41 fd = tempfile.NamedTemporaryFile(mode="w+")
42 fd.write("testserver localhost\n")
43 fd.flush()
44
45 # Re-execute ourselves with --child, to do the check.
46 env = dict(os.environ)
47 env["HOSTALIASES"] = fd.name
48 cargs = [sys.argv[0], "--child"]
49 if args.v:
50 cargs.append("-v")
51 cmd = subprocess.run(
52 cargs,
53 env=env,
54 text=True,
55 encoding="ascii",
56 stdout=subprocess.PIPE,
57 stderr=subprocess.STDOUT,
58 )
59 dprint("## child output:")
60 dprint(cmd.stdout)
61 dprint("## child returned", cmd.returncode)
62 sys.exit(cmd.returncode)