Skip to content

Commit d85f87a

Browse files
committed
Remove support for workdir and add default-contest
1 parent 68ae891 commit d85f87a

File tree

4 files changed

+51
-117
lines changed

4 files changed

+51
-117
lines changed

acedit/install_entry.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@ class InstallEntry(install):
77

88
def run(self):
99

10-
default_site = 'codeforces'
11-
workdir = os.path.join(os.path.expanduser('~'), 'ACedIt')
12-
cache_dir = os.path.join(os.path.expanduser('~'), '.cache', 'ACedIt')
10+
default_site = 'codeforces'
11+
cache_dir = os.path.join(os.path.expanduser('~'), '.cache', 'ACedIt')
1312

14-
from main import supported_sites
13+
from main import supported_sites
1514

16-
for site in supported_sites:
17-
# create cache directory and working directory structure
18-
if not os.path.isdir(os.path.join(cache_dir, site)):
19-
os.makedirs(os.path.join(cache_dir, site))
20-
if not os.path.isdir(os.path.join(workdir, site)):
21-
os.makedirs(os.path.join(workdir, site))
15+
for site in supported_sites:
16+
# create cache directory structure
17+
if not os.path.isdir(os.path.join(cache_dir, site)):
18+
os.makedirs(os.path.join(cache_dir, site))
2219

23-
data = {'default_site': default_site.strip(), 'workdir': os.path.expanduser('~'),
24-
'cachedir': cache_dir}
25-
with open(os.path.join(cache_dir, 'constants.json'), 'w') as f:
26-
f.write(json.dumps(data, indent=2))
27-
install.run(self)
20+
data = {'default_site': default_site.strip(
21+
), 'default_contest': None, 'cachedir': cache_dir}
22+
with open(os.path.join(cache_dir, 'constants.json'), 'w') as f:
23+
f.write(json.dumps(data, indent=2))
24+
install.run(self)

acedit/main.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,16 @@
77

88
def validate_args(args):
99

10-
if args['default_site'] is not None or args['workdir'] is not None:
11-
return
12-
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
10+
if args['default_site'] is not None or args['default_contest'] is not None:
2511
return
2612

2713
if not args['site'] == 'spoj' and args['contest'] is None:
28-
print 'Please specify a contest code.'
14+
print 'Please specify contest code or set a default contest.'
2915
sys.exit(0)
3016

17+
if args['source']:
18+
return
19+
3120
if args['site'] == 'spoj' and args['problem'] is None:
3221
print 'Please specify a problem code for Spoj.'
3322
sys.exit(0)
@@ -42,10 +31,9 @@ def main():
4231
# set default site
4332
util.Utilities.set_constants('default_site', args['default_site'])
4433

45-
if args['workdir']:
46-
# set working directory
47-
util.Utilities.set_constants(
48-
'workdir', args['workdir'], supported_sites)
34+
if args['default_contest']:
35+
# set default contest
36+
util.Utilities.set_constants('default_contest', args['default_contest'])
4937

5038
elif args['source']:
5139
# run code
@@ -59,6 +47,9 @@ def main():
5947
# fetch all problems for the contest
6048
util.Utilities.download_contest_testcases(args)
6149

50+
else:
51+
print 'Invalid combination of flags.'
52+
6253
except KeyboardInterrupt:
6354
# Clean up files here
6455
util.Utilities.handle_kbd_interrupt(

acedit/util.py

Lines changed: 23 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -62,100 +62,66 @@ def parse_flags(supported_sites):
6262
choices=supported_sites,
6363
help='Name of default site to be used when -s flag is not specified')
6464

65-
parser.add_argument('--set-workdir',
66-
dest='workdir',
67-
help='ABSOLUTE PATH to working directory')
65+
parser.add_argument('--set-default-contest',
66+
dest='default_contest',
67+
help='Name of default contest to be used when -c flag is not specified')
6868

6969
parser.set_defaults(force=False)
7070

7171
args = parser.parse_args()
7272

7373
flags = {}
7474

75-
if args.site is None:
75+
if args.site is None or args.contest is None:
7676
import json
77-
default_site = None
77+
site, contest = None, None
7878
try:
7979
with open(os.path.join(Utilities.cache_dir, 'constants.json'), 'r') as f:
8080
data = f.read()
8181
data = json.loads(data)
82-
default_site = data.get('default_site', None)
82+
site = data.get(
83+
'default_site', None) if args.site is None else args.site
84+
contest = data.get(
85+
'default_contest', None) if args.contest is None else args.contest
8386
except:
8487
pass
8588

86-
flags['site'] = default_site
89+
flags['site'] = site
90+
flags['contest'] = contest if not site == 'spoj' else None
8791
else:
8892
flags['site'] = args.site
93+
flags['contest'] = args.contest
8994

90-
flags['contest'] = args.contest
9195
flags['problem'] = args.problem
9296
flags['force'] = args.force
93-
flags['site'] = flags['site'].lower()
9497
flags['source'] = args.source_file
9598
flags['default_site'] = args.default_site
96-
flags['workdir'] = args.workdir
99+
flags['default_contest'] = args.default_contest
97100

98101
return flags
99102

100103
@staticmethod
101-
def set_constants(key, value, supported_sites=None):
104+
def set_constants(key, value):
102105
"""
103-
Utility method to set default site and working directory
106+
Utility method to set default site and contest
104107
"""
105108
with open(os.path.join(Utilities.cache_dir, 'constants.json'), 'r+') as f:
106109
data = f.read()
107110
data = json.loads(data)
108-
previous_value = data[key]
109111
data[key] = value
110112
f.seek(0)
111113
f.write(json.dumps(data, indent=2))
112114
f.truncate()
113115

114116
print 'Set %s to %s' % (key, value)
115117

116-
if key == 'workdir':
117-
workdir = os.path.join(value, 'ACedIt')
118-
previous_path = os.path.join(previous_value, 'ACedIt')
119-
for site in supported_sites:
120-
if not os.path.isdir(os.path.join(workdir, site)):
121-
os.makedirs(os.path.join(workdir, site))
122-
choice = raw_input(
123-
'Remove all files from previous working directory %s? (y/N) : ' % (previous_path))
124-
if choice == 'y':
125-
from shutil import rmtree
126-
rmtree(previous_path)
127-
128-
@staticmethod
129-
def create_workdir_structure(site, contest):
130-
"""
131-
Method to create the working directory structure
132-
"""
133-
134-
# No need to create directories for SPOJ as it does not have contests
135-
if site == 'spoj':
136-
return
137-
138-
try:
139-
with open(os.path.join(Utilities.cache_dir, 'constants.json'), 'r') as f:
140-
data = f.read()
141-
data = json.loads(data)
142-
except:
143-
pass
144-
145-
workdir = data.get('workdir', None)
146-
147-
if not os.path.isdir(os.path.join(workdir, site, contest)):
148-
os.makedirs(os.path.join(workdir, site, contest))
149-
150118
@staticmethod
151119
def check_cache(site, contest, problem):
152120
"""
153121
Method to check if the test cases already exist in cache
154122
If not, create the directory structure to store test cases
155123
"""
156124

157-
Utilities.create_workdir_structure(site, contest)
158-
159125
if problem is None:
160126
if not os.path.isdir(os.path.join(Utilities.cache_dir, site, contest)):
161127
os.makedirs(os.path.join(Utilities.cache_dir, site,
@@ -292,19 +258,11 @@ def run_solution(args):
292258
print 'ERROR : No such file'
293259
sys.exit(0)
294260

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
261+
problem_code = args['problem'] if args['problem'] else problem
262+
contest_code = '' if args['site'] == 'spoj' else args['contest']
305263

306-
testcases_path = os.path.join(
307-
Utilities.cache_dir, *problem_path.split('/')[-up_dir_level:])
264+
testcases_path = os.path.join(Utilities.cache_dir, args[
265+
'site'], contest_code, problem_code)
308266

309267
if os.path.isdir(testcases_path):
310268
num_cases = len(os.listdir(testcases_path)) / 2
@@ -432,26 +390,9 @@ def run_solution(args):
432390
else:
433391
print 'Test cases not found locally...'
434392

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
393+
args['problem'] = problem_code
394+
args['force'] = True
395+
args['source'] = problem + '.' + extension
455396

456397
Utilities.download_problem_testcases(args)
457398

@@ -750,7 +691,7 @@ class Spoj:
750691
def __init__(self, args):
751692
self.site = args['site']
752693
self.contest = args['contest']
753-
self.problem = args['problem']
694+
self.problem = args['problem'].upper()
754695
self.force_download = args['force']
755696

756697
def parse_html(self, req):

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
with open('requirements.txt', 'r') as f:
1111
requirements = [line.strip() for line in f.readlines()]
1212

13+
with open('README.rst', 'rb') as f:
14+
long_description = f.read().decode('utf-8')
15+
1316
extra = {}
1417
if sys.version_info >= (3,):
1518
extra['use_2to3'] = True
@@ -23,6 +26,8 @@
2326

2427
description='Download and test against sample test cases from any competitive programming website',
2528

29+
long_description=long_description,
30+
2631
author='Deep Bhattacharyya',
2732

2833
author_email='bhattacharyya.rick14@gmail.com',

0 commit comments

Comments
 (0)