annotate Lib/zipfile.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 16a876746285
children 87f30fcfad48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1 "Read and write ZIP files."
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Written by James C. Ahlstrom jim@interet.com
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # All rights transferred to CNRI pursuant to the Python contribution agreement
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import struct, os, time
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
6 import binascii
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 try:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
9 import zlib # We may need its compression method
17377
5f68754eb3b6 Try an except: after an import into "except ImportError".
Guido van Rossum <guido@python.org>
parents: 17312
diff changeset
10 except ImportError:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 zlib = None
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16840
diff changeset
13 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16840
diff changeset
14 "ZipInfo", "ZipFile", "PyZipFile"]
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16840
diff changeset
15
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
16 class BadZipfile(Exception):
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 pass
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
18 error = BadZipfile # The exception raised by this module
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # constants for Zip file compression methods
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 ZIP_STORED = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 ZIP_DEFLATED = 8
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # Other ZIP compression methods not supported
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # Here are some struct module formats for reading headers
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 structEndArchive = "<4s4H2lH" # 9 items, end of archive, 22 bytes
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 stringEndArchive = "PK\005\006" # magic number for end of archive record
24756
9c25aa13c36e Avoid warnings about <<. external_attr is now an unsigned long.
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
28 structCentralDir = "<4s4B4H3l5HLl"# 19 items, central directory, 46 bytes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 stringCentralDir = "PK\001\002" # magic number for central directory
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 structFileHeader = "<4s2B4H3l2H" # 12 items, file header record, 30 bytes
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 stringFileHeader = "PK\003\004" # magic number for file header
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
33 # indexes of entries in the central directory structure
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
34 _CD_SIGNATURE = 0
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
35 _CD_CREATE_VERSION = 1
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
36 _CD_CREATE_SYSTEM = 2
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
37 _CD_EXTRACT_VERSION = 3
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
38 _CD_EXTRACT_SYSTEM = 4 # is this meaningful?
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
39 _CD_FLAG_BITS = 5
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
40 _CD_COMPRESS_TYPE = 6
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
41 _CD_TIME = 7
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
42 _CD_DATE = 8
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
43 _CD_CRC = 9
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
44 _CD_COMPRESSED_SIZE = 10
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
45 _CD_UNCOMPRESSED_SIZE = 11
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
46 _CD_FILENAME_LENGTH = 12
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
47 _CD_EXTRA_FIELD_LENGTH = 13
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
48 _CD_COMMENT_LENGTH = 14
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
49 _CD_DISK_NUMBER_START = 15
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
50 _CD_INTERNAL_FILE_ATTRIBUTES = 16
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
51 _CD_EXTERNAL_FILE_ATTRIBUTES = 17
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
52 _CD_LOCAL_HEADER_OFFSET = 18
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
53
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
54 # indexes of entries in the local file header structure
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
55 _FH_SIGNATURE = 0
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
56 _FH_EXTRACT_VERSION = 1
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
57 _FH_EXTRACT_SYSTEM = 2 # is this meaningful?
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
58 _FH_GENERAL_PURPOSE_FLAG_BITS = 3
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
59 _FH_COMPRESSION_METHOD = 4
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
60 _FH_LAST_MOD_TIME = 5
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
61 _FH_LAST_MOD_DATE = 6
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
62 _FH_CRC = 7
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
63 _FH_COMPRESSED_SIZE = 8
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
64 _FH_UNCOMPRESSED_SIZE = 9
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
65 _FH_FILENAME_LENGTH = 10
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
66 _FH_EXTRA_FIELD_LENGTH = 11
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
67
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def is_zipfile(filename):
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
69 """Quickly see if file is a ZIP file by checking the magic number."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 try:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 fpin = open(filename, "rb")
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
72 endrec = _EndRecData(fpin)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 fpin.close()
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
74 if endrec:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 21251
diff changeset
75 return True # file has correct magic number
17805
bf70d1f4f61f Fix one bare except: clause.
Fred Drake <fdrake@acm.org>
parents: 17517
diff changeset
76 except IOError:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 pass
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 21251
diff changeset
78 return False
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
80 def _EndRecData(fpin):
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
81 """Return data from the "End of Central Directory" record, or None.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
82
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
83 The data is a list of the nine items in the ZIP "End of central dir"
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
84 record followed by a tenth item, the file seek offset of this record."""
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
85 fpin.seek(-22, 2) # Assume no archive comment.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
86 filesize = fpin.tell() + 22 # Get file size
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
87 data = fpin.read()
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
88 if data[0:4] == stringEndArchive and data[-2:] == "\000\000":
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
89 endrec = struct.unpack(structEndArchive, data)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
90 endrec = list(endrec)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
91 endrec.append("") # Append the archive comment
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
92 endrec.append(filesize - 22) # Append the record start offset
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
93 return endrec
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
94 # Search the last END_BLOCK bytes of the file for the record signature.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
95 # The comment is appended to the ZIP file and has a 16 bit length.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
96 # So the comment may be up to 64K long. We limit the search for the
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
97 # signature to a few Kbytes at the end of the file for efficiency.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
98 # also, the signature must not appear in the comment.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
99 END_BLOCK = min(filesize, 1024 * 4)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
100 fpin.seek(filesize - END_BLOCK, 0)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
101 data = fpin.read()
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
102 start = data.rfind(stringEndArchive)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
103 if start >= 0: # Correct signature string was found
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
104 endrec = struct.unpack(structEndArchive, data[start:start+22])
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
105 endrec = list(endrec)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
106 comment = data[start+22:]
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
107 if endrec[7] == len(comment): # Comment length checks out
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
108 # Append the archive comment and start offset
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
109 endrec.append(comment)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
110 endrec.append(filesize - END_BLOCK + start)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
111 return endrec
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
112 return # Error, return None
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
113
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
114
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 class ZipInfo:
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
116 """Class with attributes describing each file in the ZIP archive."""
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
117
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
18746
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
119 self.filename = _normpath(filename) # Name of the file in the archive
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
120 self.date_time = date_time # year, month, day, hour, min, sec
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 # Standard values:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
122 self.compress_type = ZIP_STORED # Type of compression for the file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
123 self.comment = "" # Comment for each file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
124 self.extra = "" # ZIP extra data
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
125 self.create_system = 0 # System which created ZIP archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
126 self.create_version = 20 # Version which created ZIP archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
127 self.extract_version = 20 # Version needed to extract archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
128 self.reserved = 0 # Must be zero
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
129 self.flag_bits = 0 # ZIP flag bits
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
130 self.volume = 0 # Volume number of file header
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
131 self.internal_attr = 0 # Internal attributes
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
132 self.external_attr = 0 # External file attributes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # Other attributes are set by class ZipFile:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
134 # header_offset Byte offset to the file header
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
135 # file_offset Byte offset to the start of the file data
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
136 # CRC CRC-32 of the uncompressed file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
137 # compress_size Size of the compressed file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
138 # file_size Size of the uncompressed file
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 def FileHeader(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
141 """Return the per-file header as a string."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 dt = self.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21098
diff changeset
144 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 if self.flag_bits & 0x08:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
146 # Set these to zero because we write them after the file data
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
147 CRC = compress_size = file_size = 0
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 else:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
149 CRC = self.CRC
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
150 compress_size = self.compress_size
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
151 file_size = self.file_size
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 header = struct.pack(structFileHeader, stringFileHeader,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 self.extract_version, self.reserved, self.flag_bits,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 self.compress_type, dostime, dosdate, CRC,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 compress_size, file_size,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 len(self.filename), len(self.extra))
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 return header + self.filename + self.extra
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
18746
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
160 # This is used to ensure paths in generated ZIP files always use
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
161 # forward slashes as the directory separator, as required by the
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
162 # ZIP format specification.
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
163 if os.sep != "/":
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
164 def _normpath(path):
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
165 return path.replace(os.sep, "/")
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
166 else:
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
167 def _normpath(path):
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
168 return path
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
169
df9c4c54ef3e Make sure path names inserted into ZIP files are normalized to use "/" as
Fred Drake <fdrake@acm.org>
parents: 17805
diff changeset
170
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 class ZipFile:
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
172 """ Class with methods to open, read, write, close, list zip files.
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
173
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
174 z = ZipFile(file, mode="r", compression=ZIP_STORED)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
175
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
176 file: Either the path to the file, or a file-like object.
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
177 If it is a path, the file will be opened and closed by ZipFile.
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
178 mode: The mode can be either read "r", write "w" or append "a".
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
179 compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
180 """
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
181
16826
a95693491eca Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents: 15874
diff changeset
182 fp = None # Set here since __del__ checks it
a95693491eca Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents: 15874
diff changeset
183
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
184 def __init__(self, file, mode="r", compression=ZIP_STORED):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
185 """Open the ZIP file with mode read "r", write "w" or append "a"."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 if compression == ZIP_STORED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 pass
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 elif compression == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 if not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 raise RuntimeError,\
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
191 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 raise RuntimeError, "That compression method is not supported"
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
194 self.debug = 0 # Level of printing: 0 through 3
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
195 self.NameToInfo = {} # Find file info given name
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
196 self.filelist = [] # List of ZipInfo instances for archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
197 self.compression = compression # Method of compression
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 self.mode = key = mode[0]
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
199
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
200 # Check if we were passed a file-like object
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
201 if isinstance(file, basestring):
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
202 self._filePassed = 0
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
203 self.filename = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
204 modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
205 self.fp = open(file, modeDict[mode])
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
206 else:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
207 self._filePassed = 1
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
208 self.fp = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
209 self.filename = getattr(file, 'name', None)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
210
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 if key == 'r':
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 self._GetContents()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 elif key == 'w':
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
214 pass
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 elif key == 'a':
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
216 try: # See if file is a zip file
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
217 self._RealGetContents()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 # seek to start of directory and overwrite
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
219 self.fp.seek(self.start_dir, 0)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
220 except BadZipfile: # file is not a zip file, just append
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
221 self.fp.seek(0, 2)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 else:
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
223 if not self._filePassed:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
224 self.fp.close()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
225 self.fp = None
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 raise RuntimeError, 'Mode must be "r", "w" or "a"'
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 def _GetContents(self):
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
229 """Read the directory, making sure we close the file if the format
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
230 is bad."""
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
231 try:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
232 self._RealGetContents()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
233 except BadZipfile:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
234 if not self._filePassed:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
235 self.fp.close()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
236 self.fp = None
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
237 raise
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
238
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
239 def _RealGetContents(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
240 """Read in the table of contents for the ZIP file."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 fp = self.fp
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
242 endrec = _EndRecData(fp)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
243 if not endrec:
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
244 raise BadZipfile, "File is not a zip file"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 if self.debug > 1:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 print endrec
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
247 size_cd = endrec[5] # bytes in central directory
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
248 offset_cd = endrec[6] # offset of central directory
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
249 self.comment = endrec[8] # archive comment
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
250 # endrec[9] is the offset of the "End of Central Dir" record
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
251 x = endrec[9] - size_cd
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 # "concat" is zero, unless zip was concatenated to another file
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 concat = x - offset_cd
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 print "given, inferred, offset", offset_cd, x, concat
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 # self.start_dir: Position of start of central directory
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 self.start_dir = offset_cd + concat
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 fp.seek(self.start_dir, 0)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 total = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 while total < size_cd:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 centdir = fp.read(46)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 total = total + 46
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 if centdir[0:4] != stringCentralDir:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 raise BadZipfile, "Bad magic number for central directory"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 centdir = struct.unpack(structCentralDir, centdir)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 print centdir
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
268 filename = fp.read(centdir[_CD_FILENAME_LENGTH])
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 x = ZipInfo(filename)
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
271 x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
272 x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
273 total = (total + centdir[_CD_FILENAME_LENGTH]
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
274 + centdir[_CD_EXTRA_FIELD_LENGTH]
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
275 + centdir[_CD_COMMENT_LENGTH])
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
276 x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
277 # file_offset must be computed below...
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 (x.create_version, x.create_system, x.extract_version, x.reserved,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 x.flag_bits, x.compress_type, t, d,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 x.CRC, x.compress_size, x.file_size) = centdir[1:12]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 x.volume, x.internal_attr, x.external_attr = centdir[15:18]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 # Convert date/time code to (year, month, day, hour, min, sec)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
12919
31c725d70e65 James C. Ahlstron <jim@interet.com>:
Fred Drake <fdrake@acm.org>
parents: 12197
diff changeset
284 t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 self.filelist.append(x)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 self.NameToInfo[x.filename] = x
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 print "total", total
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 for data in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 fp.seek(data.header_offset, 0)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 fheader = fp.read(30)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 if fheader[0:4] != stringFileHeader:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 raise BadZipfile, "Bad magic number for file header"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 fheader = struct.unpack(structFileHeader, fheader)
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
295 # file_offset is computed here, since the extra field for
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
296 # the central directory and for the local file header
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
297 # refer to different fields, and they can have different
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
298 # lengths
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
299 data.file_offset = (data.header_offset + 30
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
300 + fheader[_FH_FILENAME_LENGTH]
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
301 + fheader[_FH_EXTRA_FIELD_LENGTH])
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
302 fname = fp.read(fheader[_FH_FILENAME_LENGTH])
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 if fname != data.filename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
305 'File name in directory "%s" and header "%s" differ.' % (
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
306 data.filename, fname)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 def namelist(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
309 """Return a list of file names in the archive."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 l = []
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 for data in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 l.append(data.filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 return l
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 def infolist(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
316 """Return a list of class ZipInfo instances for files in the
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
317 archive."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 return self.filelist
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 def printdir(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
321 """Print a table of contents for the zip file."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 print "%-46s %19s %12s" % ("File Name", "Modified ", "Size")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 for zinfo in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 print "%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 def testzip(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
328 """Read all the files and check the CRC."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 for zinfo in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 try:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
331 self.read(zinfo.filename) # Check CRC-32
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 except:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 return zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 def getinfo(self, name):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
336 """Return the instance of ZipInfo given 'name'."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 return self.NameToInfo[name]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 def read(self, name):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
340 """Return file bytes (as a string) for name."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 if self.mode not in ("r", "a"):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 raise RuntimeError, 'read() requires mode "r" or "a"'
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
345 "Attempt to read ZIP archive that was already closed"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 zinfo = self.getinfo(name)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 filepos = self.fp.tell()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 self.fp.seek(zinfo.file_offset, 0)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 bytes = self.fp.read(zinfo.compress_size)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 self.fp.seek(filepos, 0)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 if zinfo.compress_type == ZIP_STORED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 pass
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 elif zinfo.compress_type == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 if not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
356 "De-compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 # zlib compress/decompress code by Jeremy Hylton of CNRI
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 dc = zlib.decompressobj(-15)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 bytes = dc.decompress(bytes)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 # need to feed in unused pad byte so that zlib won't choke
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 ex = dc.decompress('Z') + dc.flush()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 if ex:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 bytes = bytes + ex
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 raise BadZipfile, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
366 "Unsupported compression method %d for file %s" % \
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 (zinfo.compress_type, name)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 crc = binascii.crc32(bytes)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 if crc != zinfo.CRC:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 raise BadZipfile, "Bad CRC-32 for file %s" % name
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 return bytes
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 def _writecheck(self, zinfo):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
374 """Check for errors before writing a file to the archive."""
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
375 if zinfo.filename in self.NameToInfo:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
376 if self.debug: # Warning for duplicate names
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 print "Duplicate name:", zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 if self.mode not in ("w", "a"):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 raise RuntimeError, 'write() requires mode "w" or "a"'
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
382 "Attempt to write ZIP archive that was already closed"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
385 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
388 "That compression method is not supported"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 def write(self, filename, arcname=None, compress_type=None):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
391 """Put the bytes from filename into the archive under the name
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
392 arcname."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 st = os.stat(filename)
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
394 mtime = time.localtime(st.st_mtime)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 date_time = mtime[0:6]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 if arcname is None:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
398 zinfo = ZipInfo(filename, date_time)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 else:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
400 zinfo = ZipInfo(arcname, date_time)
24756
9c25aa13c36e Avoid warnings about <<. external_attr is now an unsigned long.
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
401 zinfo.external_attr = st[0] << 16L # Unix attributes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 if compress_type is None:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
403 zinfo.compress_type = self.compression
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 else:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
405 zinfo.compress_type = compress_type
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 self._writecheck(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 fp = open(filename, "rb")
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
408 zinfo.flag_bits = 0x00
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
409 zinfo.header_offset = self.fp.tell() # Start of header bytes
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
410 # Must overwrite CRC and sizes with correct data later
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
411 zinfo.CRC = CRC = 0
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
412 zinfo.compress_size = compress_size = 0
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
413 zinfo.file_size = file_size = 0
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 self.fp.write(zinfo.FileHeader())
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
415 zinfo.file_offset = self.fp.tell() # Start of file bytes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 if zinfo.compress_type == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 zlib.DEFLATED, -15)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 cmpr = None
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 while 1:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 buf = fp.read(1024 * 8)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 if not buf:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 break
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 file_size = file_size + len(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 CRC = binascii.crc32(buf, CRC)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 if cmpr:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 buf = cmpr.compress(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 compress_size = compress_size + len(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 self.fp.write(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 fp.close()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 if cmpr:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 buf = cmpr.flush()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 compress_size = compress_size + len(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 self.fp.write(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 zinfo.compress_size = compress_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 zinfo.compress_size = file_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 zinfo.CRC = CRC
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 zinfo.file_size = file_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
441 # Seek backwards and write CRC and file sizes
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19737
diff changeset
442 position = self.fp.tell() # Preserve current position in file
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
443 self.fp.seek(zinfo.header_offset + 14, 0)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 self.fp.write(struct.pack("<lll", zinfo.CRC, zinfo.compress_size,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 zinfo.file_size))
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
446 self.fp.seek(position, 0)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
450 def writestr(self, zinfo_or_arcname, bytes):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
451 """Write a file into the archive. The contents is the string
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
452 'bytes'. 'zinfo_or_arcname' is either a ZipInfo instance or
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
453 the name of the file in the archive."""
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
454 if not isinstance(zinfo_or_arcname, ZipInfo):
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
455 zinfo = ZipInfo(filename=zinfo_or_arcname,
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
456 date_time=time.localtime(time.time()))
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
457 zinfo.compress_type = self.compression
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
458 else:
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
459 zinfo = zinfo_or_arcname
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 self._writecheck(zinfo)
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
461 zinfo.file_size = len(bytes) # Uncompressed size
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
462 zinfo.CRC = binascii.crc32(bytes) # CRC-32 checksum
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 if zinfo.compress_type == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 zlib.DEFLATED, -15)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 bytes = co.compress(bytes) + co.flush()
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
467 zinfo.compress_size = len(bytes) # Compressed size
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 zinfo.compress_size = zinfo.file_size
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
470 zinfo.header_offset = self.fp.tell() # Start of header bytes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 self.fp.write(zinfo.FileHeader())
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
472 zinfo.file_offset = self.fp.tell() # Start of file bytes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 self.fp.write(bytes)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 if zinfo.flag_bits & 0x08:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
475 # Write CRC and file sizes after the file data
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
476 self.fp.write(struct.pack("<lll", zinfo.CRC, zinfo.compress_size,
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
477 zinfo.file_size))
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 def __del__(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
482 """Call the "close()" method in case the user forgot."""
21098
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
483 self.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 def close(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
486 """Close the file, and for mode "w" and "a" write the ending
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
487 records."""
21098
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
488 if self.fp is None:
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
489 return
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
490 if self.mode in ("w", "a"): # write ending records
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 count = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 pos1 = self.fp.tell()
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
493 for zinfo in self.filelist: # write central directory
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 count = count + 1
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 dt = zinfo.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21098
diff changeset
497 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 centdir = struct.pack(structCentralDir,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 stringCentralDir, zinfo.create_version,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 zinfo.create_system, zinfo.extract_version, zinfo.reserved,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 zinfo.CRC, zinfo.compress_size, zinfo.file_size,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 len(zinfo.filename), len(zinfo.extra), len(zinfo.comment),
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 0, zinfo.internal_attr, zinfo.external_attr,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 zinfo.header_offset)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 self.fp.write(centdir)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 self.fp.write(zinfo.filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 self.fp.write(zinfo.extra)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 self.fp.write(zinfo.comment)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 pos2 = self.fp.tell()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 # Write end-of-zip-archive record
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 endrec = struct.pack(structEndArchive, stringEndArchive,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 0, 0, count, count, pos2 - pos1, pos1, 0)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 self.fp.write(endrec)
17517
1e23d91c0a82 Mark Favas points out that there's an 'self.fp.flush()' call in the
Guido van Rossum <guido@python.org>
parents: 17377
diff changeset
515 self.fp.flush()
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
516 if not self._filePassed:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
517 self.fp.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 self.fp = None
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
519
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
520
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 class PyZipFile(ZipFile):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
522 """Class to create ZIP archives with Python library files and packages."""
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
523
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 def writepy(self, pathname, basename = ""):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 """Add all files from "pathname" to the ZIP archive.
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
527 If pathname is a package directory, search the directory and
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
528 all package subdirectories recursively for all *.py and enter
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
529 the modules into the archive. If pathname is a plain
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
530 directory, listdir *.py and enter all modules. Else, pathname
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
531 must be a Python *.py file and the module will be put into the
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
532 archive. Added modules are always module.pyo or module.pyc.
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
533 This method will compile the module.py into module.pyc if
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
534 necessary.
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
535 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 dir, name = os.path.split(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 if os.path.isdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 initname = os.path.join(pathname, "__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 if os.path.isfile(initname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 basename = "%s/%s" % (basename, name)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 basename = name
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 print "Adding package in", pathname, "as", basename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 fname, arcname = self._get_codename(initname[0:-3], basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 dirlist = os.listdir(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 dirlist.remove("__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 # Add all *.py files and package subdirectories
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 for filename in dirlist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 if os.path.isdir(path):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 if os.path.isfile(os.path.join(path, "__init__.py")):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 self.writepy(path, basename) # Recursive call
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 elif ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 fname, arcname = self._get_codename(path[0:-3],
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 # This is NOT a package directory, add its files at top level
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 print "Adding files from directory", pathname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 for filename in os.listdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 if ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 fname, arcname = self._get_codename(path[0:-3],
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 if pathname[-3:] != ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
583 'Files added with writepy() must end with ".py"'
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 fname, arcname = self._get_codename(pathname[0:-3], basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 print "Adding file", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 def _get_codename(self, pathname, basename):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 """Return (filename, archivename) for the path.
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
592 Given a module name path, return the correct file path and
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
593 archive name, compiling if necessary. For example, given
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
594 /python/lib/string, return (/python/lib/string.pyc, string).
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
595 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 file_py = pathname + ".py"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 file_pyc = pathname + ".pyc"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 file_pyo = pathname + ".pyo"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 if os.path.isfile(file_pyo) and \
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
600 os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
601 fname = file_pyo # Use .pyo file
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 elif not os.path.isfile(file_pyc) or \
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
603 os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
604 import py_compile
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 print "Compiling", file_py
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
607 try:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
608 py_compile.compile(file_py, file_pyc, None, True)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
609 except py_compile.PyCompileError,err:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
610 print err.msg
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 archivename = os.path.split(fname)[1]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 archivename = "%s/%s" % (basename, archivename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 return (fname, archivename)