-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·188 lines (159 loc) · 5.1 KB
/
build.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
# This build script is responsible for building and running all modules in
# this project. The following commands are supported:
#
# clean : Remove all files in the output directory. This will not remove the
# the output directory itself to allow the output directory to be
# a symlink.
#
# build : Build the full project. This will build all java files found in any
# of the src directories.
#
# rebuild : Call the clean and the build commands. For more information on
# CLEAN and BUILD see the above entries.
#
# run <class path> [ arguments ... ] : Run the specified class. All arguments
# after the class path will be passed to
# the java class when it runs.
###############################################################################
import os
import shutil
import subprocess
import sys
# Dictionary of settings that control java source compilation
CONFIG = {
'out' : 'bin',
'src' : [ 'src', 'test' ],
'libraries' : [
'third_party/junit4-4.11.jar',
'third_party/hamcrest-core-1.3.jar',
'third_party/gson-2.8.1.jar'
],
'separators' : {
'nt' : ';',
'posix' : ':'
}
}
# CLEAN
#
# Remove all files from the build output directory.
#
def clean(config) :
out = config['out']
for entry in [ os.path.join(out, name) for name in os.listdir(out) ] :
if os.path.isdir(entry) :
shutil.rmtree(entry)
else :
os.remove(entry)
print('Clean PASSED')
# BUILD
#
# Build the project defined by the config object. This will find all source
# files in the source directories, link all specified libraries, and write
# all output to the out directory.
#
def build(config) :
libraries = config['libraries']
out = config['out']
separator = config['separators'][os.name]
src = config['src']
# Find all the java source files in the given source directories.
# Non-java source files are ignored.
src_files = [ ]
for src_path in src :
for root, dirs, files in os.walk(src_path) :
src_files += [ os.path.join(root, file) for file in files if file.endswith('.java') ]
# Take everything so far and construct a single command to build the project.
command = [ ]
command += [ 'javac' ]
command += [ '-d', out ]
command += [ '-cp', separator.join([ out ] + libraries) ]
command += [ '-Xlint' ]
command += src_files
print('running : %s' % command)
if subprocess.call(command) != 0:
print('Build FAILED')
sys.exit(1)
print('Build PASSED')
# RUN
#
# Run a class from within the project.
#
def run(config, start_class_path, arguments):
libraries = config['libraries']
out = config['out']
separator = config['separators'][os.name]
command = [ ]
command += [ 'java' ]
command += [ '-cp', separator.join([ out ] + libraries) ]
command += [ start_class_path ]
command += arguments
print 'Running: [',
for x in command :
print x,
print ']'
if subprocess.call(command) != 0:
print('Run FAILED')
sys.exit(1)
print('Run PASSED')
# USAGE
#
# Print basic usage info.
#
def usage() :
print('Usage: python build.py clean | build | rebuild | run | help')
print(' clean : Remove all files in the output directory.')
print(' This does not remove the root of the output tree.')
print(' build : Build the full project. This will build all java files')
print(' found in all of the src directories.')
print(' rebuild : perform clean followed by build.')
print(' run <class path> [ arguments ... ] : Run the specified class.')
print(' All arguments after the class path will be passed to')
print(' the java class when it runs.')
print(' help : Print this helpful message.')
# MAIN
def main(args) :
if len(args) > 1 :
command = args[1]
if 'help' == command :
usage()
elif 'clean' == command :
clean(CONFIG)
elif 'build' == command :
build(CONFIG)
elif 'rebuild' == command :
clean(CONFIG)
build(CONFIG)
elif 'run' == command :
if len(args) > 2 :
java_class = args[2]
java_params = args[3:]
run(CONFIG, java_class, java_params)
else :
print('Run command requires a java class to run.')
usage();
else :
print 'Unknown command: [',
for x in args :
print x,
print ']'
usage();
else :
print ('No parameters provided.')
usage()
if __name__ == '__main__':
main(sys.argv)