@@ -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 ):
0 commit comments