2
# IVLE - Informatics Virtual Learning Environment
3
# Copyright (C) 2007-2009 The University of Melbourne
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
# Program: Fetch Submissions
22
# Script to retrieve all submissions for a particular project.
23
# Requires root to run.
35
print >>sys.stderr, "Must run %s as root." % os.path.basename(sys.argv[0])
41
from ivle.database import Project, ProjectSet, Offering, Subject
43
def fetch_submission(submission, target, svnclient, config, zip=False):
44
"""Fetch a submission from a user's repository, and dump it in a given
46
@param submission: Submission object, detailing the submission.
47
@param target: Target directory for the project (will create a
48
subdirectory for each submission).
49
@param svnclient: pysvn.Client object.
50
@param config: Config object.
51
@param zip: If True, zips up the submission.
53
# submission_name is the name of the user or group who owns the repo
54
submission_name = submission.assessed.principal.name
55
# target_final is the directory to place the files in
56
target_final = os.path.join(target, submission.assessed.principal.name)
57
if not os.path.exists(target_final):
58
os.makedirs(target_final)
59
url = get_repo_url(submission, config)
60
revision = pysvn.Revision(pysvn.opt_revision_kind.number,
62
svnclient.export(url, target_final, force=True,
63
revision=revision, recurse=True)
65
# If required, zip up the directory
67
make_zip(target_final, target_final + ".zip")
68
# Remove the target tree
69
shutil.rmtree(target_final)
71
def get_repo_url(submission, config):
72
"""Gets a local (file:) URL to the repository for a given submission.
73
This will consult submission.path to find the path within the repository
75
@param submission: Submission object, detailing the submission.
76
@param config: Config object.
78
# NOTE: This code is mostly copied from services/usrmgt-server
79
if submission.assessed.is_group:
80
# The offering this group is in
81
offering = submission.assessed.project.project_set.offering
82
groupname = submission.assessed.principal.name
83
# The name of the repository directory within 'groups' is
84
# SUBJECT_YEAR_SEMESTER_GROUP
85
namespace = "_".join([offering.subject.short_name,
86
offering.semester.year, offering.semester.semester, groupname])
87
repo_path = os.path.join(config['paths']['svn']['repo_path'],
90
# The name of the repository directory within 'users' is the username
91
username = submission.assessed.principal.name
92
repo_path = os.path.join(config['paths']['svn']['repo_path'],
95
path_in_repo = submission.path
96
# Change an absolute path into a relative one (to the top of SVN)
97
if path_in_repo[:1] == os.sep or path_in_repo[:1] == os.altsep:
98
path_in_repo = path_in_repo[1:]
100
# Attach "file://" to the front of the absolute path, to make it a URL
101
return "file://" + os.path.join(os.path.abspath(repo_path), path_in_repo)
103
def make_zip(source, dest):
104
"""Zip up a directory tree or file. The ZIP file will always contain just
105
a single directory or file at the top level (it will not be a ZIP bomb).
106
@param source: Path to a directory or file to zip up.
107
@param dest: Path to a zip file to create.
109
# NOTE: This code is mostly copied from ivle.zip (but very different)
110
zip = zipfile.ZipFile(dest, 'w')
112
# Write the source file/directory itself
113
# (If this is a directory it will NOT recurse)
114
zip.write(source, os.path.basename(source))
116
if os.path.isdir(source):
117
# All paths within the zip file are relative to relativeto
118
relativeto = os.path.dirname(source)
119
# Write the top-level directory
120
# Walk the directory tree
122
raise OSError("Could not access a file (zipping)")
123
for (dirpath, dirnames, filenames) in \
124
os.walk(source, onerror=error):
125
arc_dirpath = os.path.relpath(dirpath, relativeto)
126
for filename in dirnames + filenames:
127
zip.write(os.path.join(dirpath, filename),
128
os.path.join(arc_dirpath, filename))
137
usage = """usage: %prog [options] subject projname
139
Retrieves all submissions for a given project. Places each submission in
140
its own directory, in a subdirectory of '.'. Any errors are reported to
141
stderr (otherwise is silent).
142
subject/projname is the subject/project's short name.
146
parser = optparse.OptionParser(usage)
147
parser.add_option("-s", "--semester",
148
action="store", dest="semester", metavar="YEAR/SEMESTER",
149
help="Semester of the subject's offering (eg. 2009/1). "
150
"Defaults to the currently active semester.",
152
# XXX -c has no effect
153
parser.add_option("-c", "--cutoff",
154
action="store", dest="cutoff", metavar="DATE",
155
help="Cutoff date (retrieve the submissions as of this date)."
158
parser.add_option("-d", "--dest",
159
action="store", dest="dest", metavar="PATH",
160
help="Destination directory (default to '.', creates a subdirectory, "
161
"so will not pollute PATH).",
163
parser.add_option("-z", "--zip",
164
action="store_true", dest="zip",
165
help="Store each submission in a Zip file.",
167
(options, args) = parser.parse_args(argv[1:])
173
subject_name = unicode(args[0])
174
project_name = unicode(args[1])
176
if options.semester is None:
177
year, semester = None, None
180
year, semester = options.semester.split('/')
181
if len(year) == 0 or len(semester) == 0:
184
parser.error('Invalid semester (must have form "year/semester")')
186
if options.cutoff is not None:
188
cutoff = datetime.datetime.strptime(options.cutoff,
191
parser.error("Invalid date format: '%s' "
192
"(must be YYYY-MM-DD H:M:S)." % options.cutoff)
196
svnclient = pysvn.Client()
197
config = ivle.config.Config(plugins=False)
198
store = ivle.database.get_store(config)
200
# Get the subject from the DB
201
subject = store.find(Subject,
202
Subject.short_name == subject_name).one()
204
print >>sys.stderr, "No subject with short name '%s'" % subject_name
207
# Get the offering from the DB
209
# None specified - get the current offering from the DB
210
offerings = list(subject.active_offerings())
211
if len(offerings) == 0:
212
print >>sys.stderr, ("No active offering for subject '%s'"
215
elif len(offerings) > 1:
216
print >>sys.stderr, ("Multiple active offerings for subject '%s':"
218
print >>sys.stderr, "Please use one of:"
219
for offering in offerings:
220
print >>sys.stderr, (" --semester=%s/%s"
221
% (offering.semester.year, offering.semester.semester))
224
offering = offerings[0]
226
# Get the offering for the specified semester
227
offering = subject.offering_for_semester(year, semester)
229
print >>sys.stderr, (
230
"No offering for subject '%s' in semester %s/%s"
231
% (subject_name, year, semester))
234
# Get the project from the DB
235
project = store.find(Project,
236
Project.project_set_id == ProjectSet.id,
237
ProjectSet.offering == offering,
238
Project.short_name == project_name).one()
240
print >>sys.stderr, "No project with short name '%s'" % project_name
243
# Target directory is DEST/subject/year/semester/project
244
target_dir = os.path.join(options.dest, subject_name,
245
offering.semester.year, offering.semester.semester, project_name)
246
if not os.path.exists(target_dir):
247
os.makedirs(target_dir)
249
for submission in project.latest_submissions:
251
fetch_submission(submission, target_dir, svnclient, config,
254
# Catch all exceptions (to ensure if one student has a problem, it
255
# is reported, and we can continue)
256
print >>sys.stderr, "%s: %s: %s" % (
257
submission.assessed.principal.display_name,
260
if __name__ == "__main__":
261
sys.exit(main(sys.argv))