def from_zip(cls, file_obj, stages_name, stages_root):
"Unpack zip from file_obj into os.path.join(stages_root, stages_name)."
try:
assignment_root = os.path.join(stages_root, stages_name)
os.mkdir(assignment_root)
with zipfile.ZipFile(file_obj, 'r') as zf:
bad_filename = zf.testzip()
if bad_filename is not None:
raise Error('Corrupt file in zip: ' + bad_filename)
# TODO: Handle case where zf.namelist() uses a lot of memory
archived_files = zf.namelist()
for af in archived_files:
zf.extract(af, assignment_root)
# TODO: The stage.save_main_script() code below is used as a workaround
# to ensure that the main script is executable. Ideally, file
# permissions would be preserved.
stages = cls(assignment_root)
for stage in stages.stages.itervalues():
stage.save_main_script()
return stages
except (zipfile.BadZipfile, zipfile.LargeZipFile) as e:
raise Error(e)
评论列表
文章目录