-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·180 lines (149 loc) · 5.19 KB
/
run.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
#!/usr/bin/env python3
# This program was developed using python version 3.4.3
# Copyright (c) 2015-2016 Joshua Gutow under the MIT License
import subprocess
import argparse
import sys
import os
# Global flag to determine if the program should be verbose or not
VERBOSE = False
jfile = "Main"
blddir = "build/"
srcdir = "src/"
libs = ":/home/joshua/lib/*"
# Reads the config file in the current directory
def load_config():
# Set global vars
global jfile
global blddir
global srcdir
global classpath
import configparser
config = configparser.ConfigParser()
config.read(["run.cfg", ".run.cfg", ".run"])
if config.has_option("OPTIONS", "main_file"):
jfile = config["OPTIONS"]["main_file"]
if config.has_option("OPTIONS", "build_dir"):
blddir = config["OPTIONS"]["build_dir"]
if config.has_option("OPTIONS", "src_dir"):
srcdir = config["OPTIONS"]["src_dir"]
if config.has_option("OPTIONS", "classpath"):
libs = config["OPTIONS"]["classpath"]
# Write a standard config to the current directory
def write_default_config():
import configparser
config = configparser.ConfigParser()
config["OPTIONS"] = {"main_file" : jfile,
"build_dir" : blddir,
"src_dir" : srcdir,
"classpath" : libs}
with open("run.cfg", 'w') as configfile:
config.write(configfile)
# Prints the message if verbose mode is on
def verbose(msg):
if VERBOSE:
print(msg)
# Nukes the build directory
def clean():
path_list = lsfiles(blddir)
dir_list = lsdirs(blddir)
for obj in path_list:
verbose("Deleting " + obj)
os.remove(obj)
for obj in dir_list:
verbose("Deleting " + obj)
os.rmdir(obj)
# Compiles the file that matches the filename
def build(filename):
if not os.path.isdir(blddir):
os.mkdir(blddir)
compiler = "javac"
j_file = [srcdir + filename + ".java"]
j_flags = ["-d", blddir, "-cp", srcdir + libs, "-g", "-Xprefer:newer", "-Xlint:unchecked"]
j_params = [compiler] + j_flags + j_file
try:
tmp = subprocess.call(j_params)
verbose(" ".join(j_params))
except subprocess.CalledProcessError:
sys.exit(1) # Could not build a file needed
# Runs the build
def execute():
params = ["java", "-cp", blddir + libs, jfile]
verbose(" ".join(params) + "\n----Output Below----")
try:
subprocess.call(params)
except subprocess.CalledProcessError:
sys.exit(1) # Error when running the program
# Gets all the files in a dir tree where the given dir is the root
def lsfiles(directory):
if directory[-1] != "/":
directory = directory + "/"
tmp = os.listdir(directory)
ret = []
for path in tmp:
if os.path.isdir(directory + path):
ret = ret + lsfiles(directory + path)
else:
ret.append(directory + path)
return ret
# Gets all the directories under a parent directory
# List begins with the most nested directories
def lsdirs(directory):
if directory[-1] != "/":
directory = directory + "/"
tmp = os.listdir(directory)
ret = []
for path in tmp:
if os.path.isdir(directory + path):
ret.append(directory + path)
ret = lsdirs(directory + path) + ret
return ret
# Main method of the program
def main():
# Sets up the flags that can be passed to the build system
parser = argparse.ArgumentParser(
description="By default, this program will compile the project and then attempt to run it if the build succeeded.")
parser.add_argument("-c", "--clean", action="store_true",
help="Deletes all build files")
parser.add_argument("-r", "--run", action="store_true",
help="Attempts to run the project without building it first")
parser.add_argument("-v", "--verbose", action="store_true",
help="Prints what the program is doing")
parser.add_argument("-b", "--build", action="store_true",
help="Builds the project")
parser.add_argument("-f", "--fresh", action="store_true",
help="Deletes build files then recompiles the project")
parser.add_argument("-w", "--config", action="store_true",
help="Writes the default options to a cfg file.")
args = parser.parse_args()
# Default: compile then run
# --clean: clean up --clean, -c
# --build: only build --build, -b
# --run: only run --run, -r
# --fresh-build: clean up then compile then run --fresh, -f
# --gen-config: generate the config and do nothing else
# Check for custom config file.
load_config()
if args.verbose == True:
global VERBOSE
VERBOSE = True
if not os.path.isdir("/home/joshua/lib/"):
global libs
libs = ""
if args.config:
write_default_config()
sys.exit(0)
if args.clean:
clean()
elif args.build:
build(jfile)
elif args.run:
execute()
elif args.fresh:
clean()
build(jfile)
else:
build(jfile)
execute()
# Runs the program
main()