-
Notifications
You must be signed in to change notification settings - Fork 313
/
build-labs
executable file
·175 lines (131 loc) · 4.61 KB
/
build-labs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
import sys
import os
import shutil
from subprocess import call as call_process
TMP_DIR = "tmp"
BUILD_DIR = os.path.join(TMP_DIR, "build_mp")
COPY_DIR = os.path.join(TMP_DIR, "copy")
def error(msg, wrap=True):
import textwrap
if wrap:
t = textwrap.TextWrapper()
msg = '\n'.join(t.wrap(msg))
sys.stderr.write(msg + "\n")
def die(msg, wrap=True):
error(msg, wrap)
sys.exit(1)
def find_in_path(executable):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, executable)
if is_exe(exe_file):
return exe_file
return None
def cp(source, target):
print "cp {0} {1}".format(source, target)
shutil.copy(source, target)
def mv(source, target):
print "mv {0} {1}".format(source, target)
shutil.move(source, target)
def rm_rf(f):
if os.path.exists(f):
if os.path.isdir(f):
shutil.rmtree(f)
else:
os.unlink(f)
def sh(cmd):
try:
print ' '.join(cmd)
rc = call_process(cmd)
if rc != 0:
raise OSError('Command failed.')
except OSError as e:
raise Exception(
'Failed to run {0}: {1}'.format(' '.join(cmd), e.message)
)
USAGE="""Usage: {0} <course> [<path_to_training_repo>]
If <path_to_training_repo> is not specified, then environment variable
TRAINING_REPO is used. """.format(sys.argv[0])
if len(sys.argv) == 2:
course = sys.argv[1]
training_repo = os.getenv("TRAINING_REPO")
if not training_repo:
error("You did not specify the path to the training repo, and the " +
"TRAINING_REPO environment variable is not set.")
error("\n")
die(USAGE, wrap=False)
elif len(sys.argv) == 3:
course = sys.argv[1]
training_repo = sys.argv[2]
else:
die(USAGE, wrap=False)
course_dir = os.path.join("src", course)
manifest = os.path.join(course_dir, "labs.txt")
if not os.path.exists(training_repo):
die('Training repo "{0}" does not exist.'.format(training_repo))
if not os.path.exists(course_dir):
die('Course directory "{0}" does not exist.'.format(course_dir))
if not os.path.exists(manifest):
die('Course manifest "{0}" does not exist.'.format(manifest))
master_parse = find_in_path("master_parse")
if master_parse is None:
die("Can't find master_parse in path.")
if os.path.exists(TMP_DIR):
shutil.rmtree(TMP_DIR)
rc = 0
try:
for i in (TMP_DIR, COPY_DIR):
try:
os.mkdir(i)
except OSError as e:
die('Cannot make directory "{0}": {1}'.format(i, e.message))
with open(manifest) as f:
for i, line in enumerate(f.readlines()):
line_number = i + 1
line = line.strip()
if len(line) == 0:
continue
if line.startswith("#"):
continue
tokens = line.split('|')
if len(tokens) != 2:
raise Exception(
'"{0}", line {1}: 2 fields expected, not {2}.'.format(
manifest, line_number, len(tokens)
)
)
source, target = tokens
source = os.path.join(training_repo,
os.path.expanduser(source.strip()))
target = os.path.expanduser(target.strip())
if not os.path.exists(source):
raise Exception(
'"{0}", line {1}: "{2}" does not exist.'.format(
manifest, line_number, source
)
)
rm_rf(BUILD_DIR)
cmd = ["master_parse", "-d", BUILD_DIR, "-ei", "UTF-8", "-eo",
"UTF8", "-py", "-db", "-in", "-st", "-cc", source]
sh(cmd)
dir, file = os.path.split(source)
base, ext = os.path.splitext(file)
student_lab = os.path.join(BUILD_DIR, base, 'python',
'{0}_student.py'.format(base))
target_filename = os.path.basename(target)
tmp_target = os.path.join(COPY_DIR, target_filename)
mv(student_lab, tmp_target)
target_base, _ = os.path.splitext(target_filename)
cmd = ["gendbc", "--flatten", COPY_DIR,
"{0}.dbc".format(target_base)]
sh(cmd)
mv(tmp_target, target)
except Exception as e:
sys.stderr.write(e.message + "\n")
rc = 1
finally:
rm_rf(TMP_DIR)
sys.exit(rc)