-
Notifications
You must be signed in to change notification settings - Fork 0
/
palint.py
326 lines (235 loc) · 8.96 KB
/
palint.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/software/common64/python/bin/python
import os
import sys
import argparse
import json
import subprocess
import glob
import shutil
from subprocess import Popen, PIPE
HOME = os.path.expanduser('~/')
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CHECKSTYLE_PATH = os.path.join(SCRIPT_DIR, 'bin', 'checkstyle.jar')
STYLE_CONFIG_PATH = os.path.join(SCRIPT_DIR, 'bin', 'google_checks.xml')
FORMATTER_PATH = os.path.join(SCRIPT_DIR, 'bin', 'google-java-format.jar')
CONFIG_FILE_NAME = 'config.json'
COMPILE_ERROR_FILE_NAME = 'compile_error.log'
STYLE_ERROR_FILE_NAME = 'style_error.log'
ERROR_STOP_MESSAGE = 'Exit now?'
AI_MESSAGE = 'I Excel with Integrity'
TURN_IN_PROMPT = 'Do you want to turn in assignment now?'
TURN_IN_COMMAND = 'cse11turnin {}'
VERIFY_COMMAND = 'cse11verify {}'
README_FILE = 'README'
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
def parse_args():
parser = argparse.ArgumentParser(description='Process CSE Programming Assignments')
parser.add_argument('project', metavar='project', type=str, nargs=1, help='project to be processed')
parser.add_argument('--init', action='store_true', help="initialize new programming assignments")
return parser.parse_args()
def check_selection(prompt):
""" Prompt the user for a yes/no question. """
sys.stdout.write('\033[93m' + prompt + '\033[0m [y/n] ')
answer = sys.stdin.readline().strip().lower()
if len(answer) == 0:
return True
if answer == 'yes' or answer == 'y':
return True
return False
def init():
""" Initialize a new project folder. """
values = None
if os.path.exists(project_directory):
if not os.path.exists(configuration_path):
os.chdir(project_directory)
files = glob.glob('*.java')
libs = glob.glob('*.jar')
values = dict(Files=files, OptionalFiles=list(), Libraries=libs)
else:
if not check_selection('Project directory already exists. Do you still want to create a new one?'):
return
path = project + '.old'
if os.path.exists(path):
shutil.rmtree(path)
os.rename(project_directory, path)
if values is None:
values = dict(Files=list(), OptionalFiles=list(), Libraries=list())
os.mkdir(project_directory)
with open(configuration_path, 'w') as config:
config.write(json.dumps(values, indent=4))
def process_project():
""" Process Programming Assignment project. """
if not os.path.exists(configuration_path):
error('Configuration does not exist.')
return
with open(configuration_path) as data_file:
data = json.load(data_file)
files = data['Files']
optional_files = data['OptionalFiles']
libraries = data['Libraries']
if len(files) == 0:
error('No files will be processed.')
return EXIT_FAILURE
print('\n')
print('Start checking files...')
result = check_files(files, optional_files, libraries)
if result:
if check_selection(ERROR_STOP_MESSAGE):
return EXIT_FAILURE
print('Completed!')
print('\n')
print('Start formatting file...')
format_code(files, optional_files)
print('Completed!')
print('\n')
print('Start checking style...')
result = check_style(files, optional_files)
if result:
if check_selection(ERROR_STOP_MESSAGE):
return EXIT_FAILURE
print('Completed!')
print('\n')
print('Start checking line width...')
check_line_width(files, optional_files)
print('Completed!')
print('\n')
print('Start compiling files...')
result = test_compile(files, optional_files, libraries)
if result:
if check_selection(ERROR_STOP_MESSAGE):
return EXIT_FAILURE
print('Completed!')
print('\n')
print('Performing cleanup...')
cleanup()
print('Completed!')
if not check_selection(TURN_IN_PROMPT):
return
print('\n')
print('Start turning in assignment...')
turnin()
print('\n')
print('Start verifying assignment...')
verify()
print('\n')
print('Completed!')
def check_files(files, optionals, libraries):
has_error = False
for name in files:
path = os.path.join(project_directory, name)
if not os.path.exists(path):
error(name + " does not exist.")
has_error = True
for name in optionals:
path = os.path.join(project_directory, name)
if not os.path.exists(path):
error(name + " does not exist.")
has_error = True
for lib in libraries:
path = os.path.join(project_directory, lib)
if not os.path.exists(path):
error(lib + " does not exist.")
has_error = True
return has_error
def test_compile(files, optionals, libraries):
has_error = False
libs = list()
for lib in libraries:
libs.append('./' + lib + ':')
class_path = ''.join(libs) + '.'
cmd = 'javac -cp {} {}'
java_list = java_files(files)
optional_list = java_files(optionals)
result = subprocess.check_output(cmd.format(class_path, ' '.join(java_list)), shell=True)
if len(result) != 0:
error('Your source codes cannot be compiled; see {} for details.'.format(COMPILE_ERROR_FILE_NAME))
has_error = True
if len(optionals) != 0:
result = subprocess.check_output(cmd.format(class_path, ' '.join(optional_list)), shell=True)
if len(result) != 0:
error('Your optional files cannot be compiled; see {} for details.'.format(COMPILE_ERROR_FILE_NAME))
has_error = True
return has_error
def check_style(files, optionals):
files_argument = ' '.join(java_files(files)) + ' '.join(java_files(optionals))
cmd = 'java -jar {} -c {} {}'.format(CHECKSTYLE_PATH, STYLE_CONFIG_PATH, files_argument)
result = subprocess.check_output(cmd, shell=True)
if len(result) != 0:
error('You have style error in your source files: See {} for details.'.format(STYLE_ERROR_FILE_NAME))
with open(STYLE_ERROR_FILE_NAME, 'w') as log:
log.writelines(result)
return True
return False
def check_line_width(files, optionals):
cmd = 'grep -n \'.\\{81,\\}\' '
for name in java_files(files):
print('Checking line width in ' + name)
os.system(cmd + name)
for name in java_files(optionals):
print('Checking line width in ' + name)
os.system(cmd + name)
print('Checking line width in ' + README_FILE)
os.system(cmd + README_FILE)
def format_code(files, optionals):
""" Format source files using google java formatter and show diff to user. """
backup_folder = 'bak'
if os.path.exists(backup_folder):
shutil.rmtree(backup_folder)
os.mkdir(backup_folder)
os.system('cp *.java {}/.'.format(backup_folder))
cmd = 'java -jar {} --replace {}'.format(FORMATTER_PATH, ' '.join(files))
os.system(cmd)
if len(optionals) != 0:
cmd = 'java -jar {} --replace {}'.format(FORMATTER_PATH, ' '.join(optionals))
os.system(cmd)
for name in java_files(files):
print('\n')
print('Showing diff of file {} ...'.format(name))
status = os.system('diff {0} {1}/{0}'.format(name, backup_folder))
if status == 0:
continue
if not check_selection('Do you want to keep the change in file {}?'.format(name)):
os.system('cp {}/{} .'.format(backup_folder, name))
print('Reverted file ' + name)
for name in java_files(optionals):
print('\n')
print('Showing diff of file {} ...'.format(name))
status = os.system('diff {0} {1}/{0}'.format(name, backup_folder))
if status == 0:
continue
if not check_selection('Do you want to keep the change in file {}?'.format(name)):
os.system('cp {}/{} .'.format(backup_folder, name))
print('Reverted file ' + name)
def cleanup():
""" Clean compiled class files. """
os.system('rm *.class')
def turnin():
""" Turn in this homework. """
p = Popen(TURN_IN_COMMAND.format(project), stdout=sys.stdout, stdin=PIPE, stderr=PIPE, shell=True)
p.stdin.write(AI_MESSAGE + '\n')
p.stdin.write('y')
p.stdin.close()
p.wait()
def verify():
""" Verify assignment. """
os.system(VERIFY_COMMAND.format(project))
def error(msg):
""" Show error message to user. """
sys.stderr.write('> \033[93m{} \033[0m\n'.format(msg))
def java_files(file_list):
return filter(lambda x: str(x).endswith('.java'), file_list)
args = parse_args()
project = args.project[0]
project_directory = os.path.expanduser(os.path.join(HOME, project))
configuration_path = os.path.expanduser(os.path.join(HOME, project, CONFIG_FILE_NAME))
os.chdir(HOME)
if args.init:
init()
exit(EXIT_SUCCESS)
if not os.path.exists(project_directory) and not args.init:
error('PA folder does not exist: {}'.format(project_directory))
exit(EXIT_FAILURE)
os.chdir(project_directory)
code = process_project()
exit(code)