Mercurial > ~astiob > upreckon > hgweb
annotate zipfiles/zipfile272.diff @ 205:166a23999bf7
Added confvar okexitcodemask; changed the validator protocol
Callable validators now return three-tuples (number granted, bool correct,
str comment) instead of two-tuples (number granted, str comment). They are
still allowed to return single numbers.
Callable validators must now explicitly raise
upreckon.exceptions.WrongAnswer if they want the verdict to be Wrong
Answer rather than Partly Correct.
okexitcodemask specifies a bitmask ANDed with the exit code of the
external validator to get a boolean flag showing whether the answer is to
be marked as 'OK' rather than 'partly correct'. The bits covered by the
bitmask are reset to zeroes before devising the number of points granted
from the resulting number.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Wed, 17 Aug 2011 20:44:54 +0300 |
parents | b993d9257400 |
children |
rev | line source |
---|---|
170 | 1 --- /usr/local/lib/python2.7/zipfile.py 2011-06-15 13:20:07.000000000 +0100 |
2 +++ zipfile27.py 2011-06-03 20:20:40.000000000 +0100 | |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
3 @@ -1,6 +1,7 @@ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
4 """ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
5 Read and write ZIP files. |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
6 """ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
7 +# Improved by Chortos-2 in 2010 (added bzip2 support) |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
8 import struct, os, time, sys, shutil |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
9 import binascii, cStringIO, stat |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
10 import io |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
11 @@ -13,8 +14,13 @@ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
12 zlib = None |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
13 crc32 = binascii.crc32 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
14 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
15 +try: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
16 + import bz2 # We may need its compression method |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
17 +except ImportError: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
18 + bz2 = None |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
19 + |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
20 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile", |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
21 - "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ] |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
22 + "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", "ZIP_BZIP2" ] |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
23 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
24 class BadZipfile(Exception): |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
25 pass |
170 | 26 @@ -35,6 +41,7 @@ class LargeZipFile(Exception): |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
27 # constants for Zip file compression methods |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
28 ZIP_STORED = 0 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
29 ZIP_DEFLATED = 8 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
30 +ZIP_BZIP2 = 12 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
31 # Other ZIP compression methods not supported |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
32 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
33 # Below are some formats and associated data for reading/writing headers using |
170 | 34 @@ -483,6 +490,9 @@ def __init__(self, fileobj, mode, zi |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
35 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
36 if self._compress_type == ZIP_DEFLATED: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
37 self._decompressor = zlib.decompressobj(-15) |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
38 + elif self._compress_type == ZIP_BZIP2: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
39 + self._decompressor = bz2.BZ2Decompressor() |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
40 + self.MIN_READ_SIZE = 900000 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
41 self._unconsumed = '' |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
42 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
43 self._readbuffer = '' |
170 | 44 @@ -641,6 +651,13 @@ def read1(self, n): |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
45 self._update_crc(data, eof=eof) |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
46 self._readbuffer = self._readbuffer[self._offset:] + data |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
47 self._offset = 0 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
48 + elif (len(self._unconsumed) > 0 and n > len_readbuffer and |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
49 + self._compress_type == ZIP_BZIP2): |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
50 + data = self._decompressor.decompress(self._unconsumed) |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
51 + |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
52 + self._unconsumed = '' |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
53 + self._readbuffer = self._readbuffer[self._offset:] + data |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
54 + self._offset = 0 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
55 |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
56 # Read from buffer. |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
57 data = self._readbuffer[self._offset: self._offset + n] |
170 | 58 @@ -657,7 +674,8 @@ class ZipFile: |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
59 file: Either the path to the file, or a file-like object. |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
60 If it is a path, the file will be opened and closed by ZipFile. |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
61 mode: The mode can be either read "r", write "w" or append "a". |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
62 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
63 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
64 + or ZIP_BZIP2 (requires bz2). |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
65 allowZip64: if True ZipFile will create files with ZIP64 extensions when |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
66 needed, otherwise it will raise an exception when this would |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
67 be necessary. |
170 | 68 @@ -677,6 +695,10 @@ def __init__(self, file, mode="r", c |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
69 if not zlib: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
70 raise RuntimeError,\ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
71 "Compression requires the (missing) zlib module" |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
72 + elif compression == ZIP_BZIP2: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
73 + if not bz2: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
74 + raise RuntimeError,\ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
75 + "Compression requires the (missing) bz2 module" |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
76 else: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
77 raise RuntimeError, "That compression method is not supported" |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
78 |
170 | 79 @@ -1011,7 +1033,10 @@ def _writecheck(self, zinfo): |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
80 if zinfo.compress_type == ZIP_DEFLATED and not zlib: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
81 raise RuntimeError, \ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
82 "Compression requires the (missing) zlib module" |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
83 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED): |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
84 + if zinfo.compress_type == ZIP_BZIP2 and not bz2: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
85 + raise RuntimeError, \ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
86 + "Compression requires the (missing) bz2 module" |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
87 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2): |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
88 raise RuntimeError, \ |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
89 "That compression method is not supported" |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
90 if zinfo.file_size > ZIP64_LIMIT: |
170 | 91 @@ -1072,6 +1097,8 @@ def write(self, filename, arcname=No |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
92 if zinfo.compress_type == ZIP_DEFLATED: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
93 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
94 zlib.DEFLATED, -15) |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
95 + elif zinfo.compress_type == ZIP_BZIP2: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
96 + cmpr = bz2.BZ2Compressor() |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
97 else: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
98 cmpr = None |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
99 while 1: |
170 | 100 @@ -1132,6 +1159,10 @@ def writestr(self, zinfo_or_arcname, |
29
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
101 zlib.DEFLATED, -15) |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
102 bytes = co.compress(bytes) + co.flush() |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
103 zinfo.compress_size = len(bytes) # Compressed size |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
104 + elif zinfo.compress_type == ZIP_BZIP2: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
105 + co = bz2.BZ2Compressor() |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
106 + bytes = co.compress(bytes) + co.flush() |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
107 + zinfo.compress_size = len(bytes) # Compressed size |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
108 else: |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
109 zinfo.compress_size = zinfo.file_size |
a8cc383b787c
Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
110 zinfo.header_offset = self.fp.tell() # Start of header bytes |