14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export a Tree to a non-versioned directory.
17
"""Export a Tree to a zip file.
33
34
from bzrlib.trace import mutter
36
# Windows expects this bit to be set in the 'external_attr' section
37
# Or it won't consider the entry a directory
37
# Windows expects this bit to be set in the 'external_attr' section,
38
# or it won't consider the entry a directory.
38
39
ZIP_DIRECTORY_BIT = (1 << 4)
39
40
FILE_PERMISSIONS = (0644 << 16)
41
DIR_PERMISSIONS = (0755 << 16)
41
43
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
42
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
45
def zip_exporter(tree, dest, root, subdir, filtered=False,
46
per_file_timestamps=False):
44
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
47
def zip_exporter_generator(tree, dest, root, subdir=None, filtered=False,
48
force_mtime=None, fileobj=None):
47
49
""" Export this tree to a new zip file.
49
51
`dest` will be created holding the contents of this tree; if it
50
52
already exists, it will be overwritten".
52
mutter('export version %r', tree)
54
now = time.localtime()[:6]
56
55
compression = zipfile.ZIP_DEFLATED
56
if fileobj is not None:
57
60
zipf = zipfile.ZipFile(dest, "w", compression)
60
62
for dp, ie in _export_iter_entries(tree, subdir):
61
63
file_id = ie.file_id
64
66
# zipfile.ZipFile switches all paths to forward
65
67
# slashes anyway, so just stick with that.
66
if per_file_timestamps:
68
if force_mtime is not None:
67
71
mtime = tree.get_file_mtime(ie.file_id, dp)
72
date_time = time.localtime(mtime)[:6]
70
73
filename = osutils.pathjoin(root, dp).encode('utf8')
71
74
if ie.kind == "file":
72
75
zinfo = zipfile.ZipInfo(
75
78
zinfo.compress_type = compression
76
79
zinfo.external_attr = _FILE_ATTR
89
92
# not just empty files.
90
93
zinfo = zipfile.ZipInfo(
91
94
filename=filename + '/',
93
96
zinfo.compress_type = compression
94
97
zinfo.external_attr = _DIR_ATTR
95
zipf.writestr(zinfo,'')
98
zipf.writestr(zinfo, '')
96
99
elif ie.kind == "symlink":
97
100
zinfo = zipfile.ZipInfo(
98
101
filename=(filename + '.lnk'),
100
103
zinfo.compress_type = compression
101
104
zinfo.external_attr = _FILE_ATTR
102
zipf.writestr(zinfo, ie.symlink_target)
105
zipf.writestr(zinfo, tree.get_symlink_target(file_id))