~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to bin/ivle-fetchsubmissions

  • Committer: Matt Giuca
  • Date: 2009-05-12 15:08:45 UTC
  • mto: This revision was merged to the branch mainline in revision 1247.
  • Revision ID: matt.giuca@gmail.com-20090512150845-fg480l1qh7le0ypz
ivle-fetchsubmissions: In a stunningly awful hack, detects pre-Python2.6 and
    changes make_zip to behave correctly for the awful restriction in zipfile
    that you can't add directories.
    (Stops trying to add directories, and hacks in the footer writing if there
    is an empty file).

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
 
46
46
from ivle.database import Project, ProjectSet, Offering, Subject
47
47
 
 
48
# Is Python version 2.6 or higher?
 
49
PYTHON26 = map(int, sys.version[:3].split('.')) >= [2, 6]
 
50
 
48
51
def fetch_submission(submission, target, svnclient, config, zip=False,
49
52
    txt=False):
50
53
    """Fetch a submission from a user's repository, and dump it in a given
119
122
def make_zip(source, dest):
120
123
    """Zip up a directory tree or file. The ZIP file will always contain just
121
124
    a single directory or file at the top level (it will not be a ZIP bomb).
 
125
    XXX In Python 2.5 and earlier, this does NOT create empty directories
 
126
    (it's not possible with the Python2.5 version of zipfile).
122
127
    @param source: Path to a directory or file to zip up.
123
128
    @param dest: Path to a zip file to create.
124
129
    """
127
132
 
128
133
    # Write the source file/directory itself
129
134
    # (If this is a directory it will NOT recurse)
130
 
    zip.write(source, os.path.basename(source))
 
135
    if PYTHON26 or not os.path.isdir(source):
 
136
        # Python < 2.6 errors if you add a directory
 
137
        # (This means you can't add an empty directory)
 
138
        zip.write(source, os.path.basename(source))
131
139
 
132
140
    if os.path.isdir(source):
133
141
        # All paths within the zip file are relative to relativeto
139
147
        for (dirpath, dirnames, filenames) in \
140
148
            os.walk(source, onerror=error):
141
149
            arc_dirpath = os.path.relpath(dirpath, relativeto)
142
 
            for filename in dirnames + filenames:
 
150
            # Python < 2.6 errors if you add a directory
 
151
            # (This means you can't add an empty directory)
 
152
            if PYTHON26:
 
153
                filenames = dirnames + filenames
 
154
            for filename in filenames:
143
155
                zip.write(os.path.join(dirpath, filename),
144
156
                            os.path.join(arc_dirpath, filename))
145
157
 
 
158
    if not PYTHON26:
 
159
        # XXX Write this _didModify attribute of zip, to trick it into writing
 
160
        # footer bytes even if there are no files in the archive (otherwise it
 
161
        # writes a 0-byte archive which is invalid).
 
162
        # Note that in Python2.6 we avoid this by always writing the top-level
 
163
        # file or directory, at least.
 
164
        zip._didModify = True
146
165
    zip.close()
147
166
 
148
167
def write_submission_info(filename, submission):