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.
20
from __future__ import absolute_import
22
from contextlib import closing
28
from bzrlib.export import _export_iter_entries
29
from bzrlib.filters import (
31
filtered_output_bytes,
33
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
33
from ..export import _export_iter_entries
34
from ..trace import mutter
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
FILE_PERMISSIONS = (0644 << 16)
40
FILE_PERMISSIONS = (0o644 << 16)
41
DIR_PERMISSIONS = (0o755 << 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_archive_generator(tree, dest, root, subdir=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
54
compression = zipfile.ZIP_DEFLATED
57
zipf = zipfile.ZipFile(dest, "w", compression)
60
for dp, ie in _export_iter_entries(tree, subdir):
62
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest)
64
# zipfile.ZipFile switches all paths to forward
65
# slashes anyway, so just stick with that.
66
if per_file_timestamps:
67
mtime = tree.get_file_mtime(ie.file_id, dp)
70
filename = osutils.pathjoin(root, dp).encode('utf8')
72
zinfo = zipfile.ZipInfo(
75
zinfo.compress_type = compression
76
zinfo.external_attr = _FILE_ATTR
78
chunks = tree.get_file_lines(file_id)
79
filters = tree._content_filter_stack(dp)
80
context = ContentFilterContext(dp, tree, ie)
81
contents = filtered_output_bytes(chunks, filters, context)
82
content = ''.join(contents)
55
with tempfile.SpooledTemporaryFile() as buf:
56
with closing(zipfile.ZipFile(buf, "w", compression)) as zipf, \
58
for dp, tp, ie in _export_iter_entries(tree, subdir):
59
file_id = getattr(ie, 'file_id', None)
60
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest)
62
# zipfile.ZipFile switches all paths to forward
63
# slashes anyway, so just stick with that.
64
if force_mtime is not None:
84
content = tree.get_file_text(file_id)
85
zipf.writestr(zinfo, content)
86
elif ie.kind == "directory":
87
# Directories must contain a trailing slash, to indicate
88
# to the zip routine that they are really directories and
89
# not just empty files.
90
zinfo = zipfile.ZipInfo(
91
filename=filename + '/',
93
zinfo.compress_type = compression
94
zinfo.external_attr = _DIR_ATTR
95
zipf.writestr(zinfo,'')
96
elif ie.kind == "symlink":
97
zinfo = zipfile.ZipInfo(
98
filename=(filename + '.lnk'),
100
zinfo.compress_type = compression
101
zinfo.external_attr = _FILE_ATTR
102
zipf.writestr(zinfo, ie.symlink_target)
106
except UnicodeEncodeError:
109
from bzrlib.errors import BzrError
110
raise BzrError("Can't export non-ascii filenames to zip")
67
mtime = tree.get_file_mtime(tp, file_id)
68
date_time = time.localtime(mtime)[:6]
69
filename = osutils.pathjoin(root, dp)
71
zinfo = zipfile.ZipInfo(
74
zinfo.compress_type = compression
75
zinfo.external_attr = _FILE_ATTR
76
content = tree.get_file_text(tp, file_id)
77
zipf.writestr(zinfo, content)
78
elif ie.kind in ("directory", "tree-reference"):
79
# Directories must contain a trailing slash, to indicate
80
# to the zip routine that they are really directories and
81
# not just empty files.
82
zinfo = zipfile.ZipInfo(
83
filename=filename + '/',
85
zinfo.compress_type = compression
86
zinfo.external_attr = _DIR_ATTR
87
zipf.writestr(zinfo, '')
88
elif ie.kind == "symlink":
89
zinfo = zipfile.ZipInfo(
90
filename=(filename + '.lnk'),
92
zinfo.compress_type = compression
93
zinfo.external_attr = _FILE_ATTR
94
zipf.writestr(zinfo, tree.get_symlink_target(tp, file_id))
95
# Urgh, headers are written last since they include e.g. file size.
96
# So we have to buffer it all :(
98
for chunk in osutils.file_iterator(buf):