Conditions | 7 |
Total Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | ''' |
||
128 | def prepare_working_directory(job, submission_path, validator_path): |
||
129 | ''' |
||
130 | Based on two downloaded files in the working directory, |
||
131 | the student submission and the validation package, |
||
132 | the working directory is prepared. |
||
133 | |||
134 | We unpack student submission first, so that teacher files overwrite |
||
135 | them in case. |
||
136 | |||
137 | When the student submission is a single directory, we change the |
||
138 | working directory and go directly into it, before dealing with the |
||
139 | validator stuff. |
||
140 | |||
141 | If unrecoverable errors happen, such as an empty student archive, |
||
142 | a JobException is raised. |
||
143 | ''' |
||
144 | submission_fname = os.path.basename(submission_path) |
||
145 | validator_fname = os.path.basename(validator_path) |
||
146 | |||
147 | # Un-archive student submission |
||
148 | single_dir, did_unpack = unpack_if_needed(job.working_dir, submission_path) |
||
149 | job.student_files = os.listdir(job.working_dir) |
||
150 | if did_unpack: |
||
151 | job.student_files.remove(submission_fname) |
||
152 | |||
153 | # Fail automatically on empty student submissions |
||
154 | if len(job.student_files) is 0: |
||
155 | info_student = "Your compressed upload is empty - no files in there." |
||
156 | info_tutor = "Submission archive file has no content." |
||
157 | logger.error(info_tutor) |
||
158 | raise JobException(info_student=info_student, info_tutor=info_tutor) |
||
159 | |||
160 | # Handle student archives containing a single directory with all data |
||
161 | if single_dir: |
||
162 | logger.warning( |
||
163 | "The submission archive contains only one directory. Changing working directory.") |
||
164 | # Set new working directory |
||
165 | job.working_dir = job.working_dir + single_dir + os.sep |
||
166 | # Move validator package there |
||
167 | shutil.move(validator_path, job.working_dir) |
||
168 | validator_path = job.working_dir + validator_fname |
||
169 | # Re-scan for list of student files |
||
170 | job.student_files = os.listdir(job.working_dir) |
||
171 | |||
172 | # The working directory now only contains the student data and the downloaded |
||
173 | # validator package. |
||
174 | # Update the file list accordingly. |
||
175 | job.student_files.remove(validator_fname) |
||
176 | logger.debug("Student files: {0}".format(job.student_files)) |
||
177 | |||
178 | # Unpack validator package |
||
179 | single_dir, did_unpack = unpack_if_needed(job.working_dir, validator_path) |
||
180 | if single_dir: |
||
181 | info_student = "Internal error with the validator. Please contact your course responsible." |
||
182 | info_tutor = "Error: Directories are not allowed in the validator archive." |
||
183 | logger.error(info_tutor) |
||
184 | raise JobException(info_student=info_student, info_tutor=info_tutor) |
||
185 | |||
186 | if not os.path.exists(job.validator_script_name): |
||
187 | if did_unpack: |
||
188 | # The download was an archive, but the validator was not inside. |
||
189 | # This is a failure of the tutor. |
||
190 | info_student = "Internal error with the validator. Please contact your course responsible." |
||
191 | info_tutor = "Error: Missing validator.py in the validator archive." |
||
192 | logger.error(info_tutor) |
||
193 | raise JobException(info_student=info_student, info_tutor=info_tutor) |
||
194 | else: |
||
195 | # The download is already the script, but has the wrong name |
||
196 | logger.warning("Renaming {0} to {1}.".format(validator_path, job.validator_script_name)) |
||
197 | shutil.move(validator_path, job.validator_script_name) |
||
198 | |||
201 |