-
Notifications
You must be signed in to change notification settings - Fork 13
/
build_from_source.py
executable file
·94 lines (85 loc) · 3.08 KB
/
build_from_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
'''
Build software on VM that is part of a testbed for DNS resolvers
See https://github.com/icann/resolver-testbed for more information
Must be run in the same directory as the config files
'''
import os
import subprocess
import sys
import time
import tempfile
# Some program-wide constants
PROG_DIR = os.path.abspath(os.getcwd())
SOURCE_DIR = "/root/Source"
TARGET_DIR = "/root/Target"
this_log_filename = tempfile.mktemp()
this_log_f = open(this_log_filename, mode="wt")
def log(in_str):
''' Prints a message , but only if the message is non-null; returns nothing '''
if not in_str:
return
out = "{}: {}\n".format(time.strftime("%H-%M-%S"), in_str)
this_log_f.write(out)
def die(in_str):
''' log then exit '''
err_str = in_str + " Exiting.\n"
log(err_str)
this_log_f.close()
die_out = open(this_log_filename, mode="rt").read()
print(die_out, file=sys.stderr)
sys.exit(1)
log("## Starting run on date {}".format(time.strftime("%Y-%m-%d")))
# Parse the input
if len(sys.argv) < 4:
die("There were not enough arguments on the command line.")
# Be sure that the needed directories are there
for this_dir in (SOURCE_DIR, TARGET_DIR):
if not os.path.exists(this_dir):
try:
os.mkdir(this_dir)
except Exception:
die("Could not create {}.".format(this_dir))
# Get the name to build
package_name = sys.argv[1]
package_url = sys.argv[2]
package_make_str = sys.argv[3]
try:
os.chdir(SOURCE_DIR)
except Exception:
die("Could not chdir to {}.".format(SOURCE_DIR))
# Get the compressed file into SOURCE_DIR
log("Getting {}".format(package_url))
p = subprocess.Popen("wget {}".format(package_url), stderr=subprocess.PIPE, shell=True)
p_ret = p.wait()
if p_ret > 0:
die("wget failed with '{}'.".format((p.stderr.read()).decode("latin-1")))
# Uncompress into package_name
log("Uncompressing {}".format(os.path.basename(package_url)))
p = subprocess.Popen("tar -xf {}".format(os.path.basename(package_url)), shell=True)
p_ret = p.wait()
if p_ret > 0:
die("tar -xf failed with '{}'.".format((p.stderr.read()).decode("latin-1")))
# Verify that the expected directory exists, then chdir there
package_source_dir = (os.path.splitext(os.path.basename(package_url))[0]).replace(".tar", "")
if not os.path.exists(package_source_dir):
die("Getting and expanding {} did not result in a directory {}.".format(package_url, package_source_dir))
try:
os.chdir(package_source_dir)
except Exception:
die("Could not chdir into {}".format(package_source_dir))
# Change PREFIX_GOES_HERE in package_make_str into TARGET_DIR/package_name
full_make_str = package_make_str.replace("PREFIX", "{}/{}".format(TARGET_DIR, package_source_dir))
log("Making with '{}'".format(full_make_str))
# Make
p = subprocess.Popen(full_make_str, stderr=subprocess.PIPE, shell=True)
p_ret = p.wait()
if p_ret > 0:
die("making failed with '{}'.".format((p.stderr.read()).decode("latin-1")))
log("## Finished run")
this_log_f.close()
try:
os.remove(this_log_filename)
except Exception:
print("Weird, could not delete the log file")
exit()