19
19
20
20
21
21
22
+ import glob
22
23
import httplib
23
24
import json
24
25
import os
@@ -51,14 +52,16 @@ def __init__(self, host, cookie, middleware_token, contest_id, problem_id):
51
52
self .contest_id = contest_id
52
53
self .problem_id = problem_id
53
54
54
- def _PrepareSourceFiles (self , source_names ):
55
+ def _PrepareSourceFiles (self , source_patterns ):
55
56
"""Zip all source directories into files and return them.
56
57
57
58
The returned list will contain (filename, file_data) tuples, where file_data
58
59
will be None for files that have not been read yet.
59
60
60
61
Args:
61
- source_names: List with all the files to include with the solution.
62
+ source_patterns: List with all the file patterns to include with the
63
+ solution. These file patterns will be expanded using python's glob
64
+ module.
62
65
63
66
Returns:
64
67
A (source_files, ignored_zips) tuple, where source_files is a list of
@@ -70,23 +73,25 @@ def _PrepareSourceFiles(self, source_names):
70
73
source_files = []
71
74
ignored_zips = set ()
72
75
73
- # Process each source specified by the user.
74
- for source in source_names :
75
- # Check if the source is a directory or a file.
76
- if os .path .isdir (source ):
77
- # Create a zip file in memory for the directory, add it to the source
78
- # files and update the ignored_zips set.
79
- sys .stdout .write ('Compressing directory "{0}"...\n ' .format (source ))
80
- zipped_contents , newly_ignored_zips = (
81
- zip_utils .MakeZipFileInMemory ([source ], ignore_exts = ['.zip' ]))
82
- ignored_zips .update (newly_ignored_zips )
83
- flat_source_name = source .replace ('\\ ' , '_' ).replace ('/' , '_' )
84
- zip_filename = '{1}_{0}.zip' .format (random .randrange (0 , 2 ** 31 - 1 ),
85
- flat_source_name )
86
- source_files .append ((zip_filename , zipped_contents ))
87
- else :
88
- # Add files directly to the prepared sources.
89
- source_files .append ((source , None ))
76
+ # Process each source pattern specified by the user, expanding them using
77
+ # the python's glob module.
78
+ for source_pattern in source_patterns :
79
+ for source in glob .iglob (source_pattern ):
80
+ # Check if the source is a directory or a file.
81
+ if os .path .isdir (source ):
82
+ # Create a zip file in memory for the directory, add it to the source
83
+ # files and update the ignored_zips set.
84
+ sys .stdout .write ('Compressing directory "{0}"...\n ' .format (source ))
85
+ zipped_contents , newly_ignored_zips = (
86
+ zip_utils .MakeZipFileInMemory ([source ], ignore_exts = ['.zip' ]))
87
+ ignored_zips .update (newly_ignored_zips )
88
+ flat_source_name = source .replace ('\\ ' , '_' ).replace ('/' , '_' )
89
+ zip_filename = '{1}_{0}.zip' .format (random .randrange (0 , 2 ** 31 - 1 ),
90
+ flat_source_name )
91
+ source_files .append ((zip_filename , zipped_contents ))
92
+ else :
93
+ # Add files directly to the prepared sources.
94
+ source_files .append ((source , None ))
90
95
91
96
# Return all generated sets.
92
97
return source_files , ignored_zips
@@ -123,15 +128,16 @@ def _ParseResult(self, response_data, input_public):
123
128
'cannot submit solution. Check that the host, '
124
129
'user and contest id are valid: {0}.\n ' .format (e ))
125
130
126
- def Submit (self , input_id , output_name , source_names , input_public ,
131
+ def Submit (self , input_id , output_name , source_patterns , input_public ,
127
132
gzip_body = True , zip_sources = False , add_ignored_zips = False ):
128
133
"""Submit the specified output and sources file to the problem.
129
134
130
135
Args:
131
136
input_id: Identifier of the output to submit ('0' for the small output,
132
137
'1' for the large output).
133
138
output_name: Name of the file with the output data.
134
- source_names: Names of the source files to be included with the output.
139
+ source_patterns: Name patterns of the source files to be included with the
140
+ output. These patterns will be expanded using Python's glob module.
135
141
input_public: Boolean indicating whether the answer is public or not.
136
142
gzip_body: Boolean indicating whether the body has to be gzipped or not.
137
143
zip_sources: Boolean indicating whether all sources should be put inside a
@@ -150,7 +156,7 @@ def Submit(self, input_id, output_name, source_names, input_public,
150
156
# Prepare the source files (zipping all directories). After this,
151
157
# source_files will only contain text files and zip files specified directly
152
158
# or by compressing a directory.
153
- source_files , ignored_zips = self ._PrepareSourceFiles (set (source_names ))
159
+ source_files , ignored_zips = self ._PrepareSourceFiles (set (source_patterns ))
154
160
155
161
# Check if the user requested to zip source files.
156
162
if zip_sources :
0 commit comments