Skip to content

Commit d94e455

Browse files
committed
Add method to execute code from any directory
1 parent 4760d9c commit d94e455

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

ACedIt/main.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,26 @@ def validate_args(args):
1010
if args['default_site'] is not None or args['workdir'] is not None:
1111
return
1212

13-
if args['source'] and (args['contest'] or args['problem'] or args['force']):
14-
print 'ACedIt --run <source_file> doesn\'t support other flags'
15-
sys.exit(0)
16-
elif args['source']:
13+
if args['source']:
14+
# Check if all flags have been specfied explicitly
15+
if (args['site'] == 'spoj' or args['contest']) and args['problem']:
16+
return
17+
18+
# If at least one is specified (but not all), it's not a valid
19+
# combination
20+
if args['site'] or args['contest'] or args['problem']:
21+
print 'ACedIt requires site, contest(not required for SPOJ) and problem explicitly in case workdir structure is not followed.'
22+
sys.exit(0)
23+
24+
# No flags specified, so take arguments from path
1725
return
1826

1927
if not args['site'] == 'spoj' and args['contest'] is None:
20-
print 'Please specify a contest code'
28+
print 'Please specify a contest code.'
2129
sys.exit(0)
2230

2331
if args['site'] == 'spoj' and args['problem'] is None:
24-
print 'Please specify a problem code for Spoj'
32+
print 'Please specify a problem code for Spoj.'
2533
sys.exit(0)
2634

2735

@@ -41,7 +49,7 @@ def main():
4149

4250
elif args['source']:
4351
# run code
44-
util.Utilities.run_solution(args['source'])
52+
util.Utilities.run_solution(args)
4553

4654
elif args['problem'] is not None:
4755
# fetch single problem

ACedIt/util.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,12 @@ def handle_kbd_interrupt(site, contest, problem):
278278
print 'Done. Exiting gracefully.'
279279

280280
@staticmethod
281-
def run_solution(problem):
281+
def run_solution(args):
282282
"""
283283
Method to run and test the user's solution against sample cases
284284
"""
285+
problem = args['source']
286+
285287
extension = problem.split('.')[-1]
286288
problem = problem.split('.')[0]
287289
problem_path = os.path.join(os.getcwd(), problem)
@@ -290,11 +292,19 @@ def run_solution(problem):
290292
print 'ERROR : No such file'
291293
sys.exit(0)
292294

293-
# For SPOJ, go up two directory levels as it does not have contests
294-
up_dir_level = 2 if problem_path.split('/')[-2] == 'spoj' else 3
295+
if args['problem']:
296+
# Check if problem code has been specified explicitly
297+
args['contest'] = '' if args['site'] == 'spoj' else args['contest']
298+
testcases_path = os.path.join(Utilities.cache_dir, args['site'], args[
299+
'contest'], args['problem'])
300+
else:
301+
# Take arguments from path
302+
303+
# For SPOJ, go up two directory levels as it does not have contests
304+
up_dir_level = 2 if problem_path.split('/')[-2] == 'spoj' else 3
295305

296-
testcases_path = os.path.join(
297-
Utilities.cache_dir, *problem_path.split('/')[-up_dir_level:])
306+
testcases_path = os.path.join(
307+
Utilities.cache_dir, *problem_path.split('/')[-up_dir_level:])
298308

299309
if os.path.isdir(testcases_path):
300310
num_cases = len(os.listdir(testcases_path)) / 2
@@ -311,7 +321,7 @@ def run_solution(problem):
311321
Utilities.colors['YELLOW'] + 'TLE' + Utilities.colors['ENDC']]
312322

313323
elif status == 0:
314-
324+
# Ran successfully
315325
with open('temp_output' + str(i), 'r') as temp_handler, open(os.path.join(testcases_path, 'Output' + str(i)), 'r') as out_handler:
316326
expected_output = out_handler.read().strip().split('\n')
317327
user_output = temp_handler.read().strip().split('\n')
@@ -345,6 +355,8 @@ def run_solution(problem):
345355
compiler + ' ' + problem_path + '.cpp')
346356

347357
if compile_status == 0:
358+
359+
# Compiled successfully
348360
for i in xrange(num_cases):
349361
status = os.system('timeout 2s ./a.out < ' + os.path.join(
350362
testcases_path, 'Input' + str(i)) + ' > temp_output' + str(i))
@@ -354,7 +366,7 @@ def run_solution(problem):
354366
'YELLOW'] + 'TLE' + Utilities.colors['ENDC']]
355367

356368
elif status == 0:
357-
369+
# Ran successfully
358370
with open('temp_output' + str(i), 'r') as temp_handler, open(os.path.join(testcases_path, 'Output' + str(i)), 'r') as out_handler:
359371
expected_output = out_handler.read().strip().split('\n')
360372
user_output = temp_handler.read().strip().split('\n')
@@ -388,7 +400,7 @@ def run_solution(problem):
388400
sys.exit(0)
389401

390402
else:
391-
print 'Supports only C++ and Python as of now. Support for Java coming soon.'
403+
print 'Supports only C, C++ and Python as of now. Support for Java coming soon.'
392404
sys.exit(0)
393405

394406
from terminaltables import AsciiTable
@@ -420,26 +432,31 @@ def run_solution(problem):
420432
else:
421433
print 'Test cases not found locally...'
422434

423-
# Handle case for SPOJ specially as it does not have contests
424-
if problem_path.split('/')[-2] == 'spoj':
425-
args = {
426-
'site': 'spoj',
427-
'contest': None,
428-
'problem': testcases_path.split('/')[-1],
429-
'force': True
430-
}
431-
else:
432-
args = {
433-
'site': testcases_path.split('/')[-3],
434-
'contest': testcases_path.split('/')[-2],
435-
'problem': testcases_path.split('/')[-1],
436-
'force': True
437-
}
435+
if args['problem'] is None:
436+
# Handle case for SPOJ specially as it does not have contests
437+
if problem_path.split('/')[-2] == 'spoj':
438+
args = {
439+
'site': 'spoj',
440+
'contest': None,
441+
'problem': testcases_path.split('/')[-1],
442+
'force': True,
443+
'source': problem + '.' + extension
444+
}
445+
else:
446+
args = {
447+
'site': testcases_path.split('/')[-3],
448+
'contest': testcases_path.split('/')[-2],
449+
'problem': testcases_path.split('/')[-1],
450+
'force': True,
451+
'source': problem + '.' + extension
452+
}
453+
elif args['site'] == 'spoj':
454+
args['contest'] = None
438455

439456
Utilities.download_problem_testcases(args)
440457

441458
print 'Running your solution against sample cases...'
442-
Utilities.run_solution(problem + '.' + extension)
459+
Utilities.run_solution(args)
443460

444461
@staticmethod
445462
def get_html(url):

0 commit comments

Comments
 (0)