1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export a Tree to a non-versioned directory.
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
38
ZIP_DIRECTORY_BIT = (1 << 4)
39
FILE_PERMISSIONS = (0644 << 16)
41
_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
""" Export this tree to a new zip file.
48
`dest` will be created holding the contents of this tree; if it
49
already exists, it will be overwritten".
51
mutter('export version %r', tree)
53
now = time.localtime()[:6]
55
compression = zipfile.ZIP_DEFLATED
56
zipf = zipfile.ZipFile(dest, "w", compression)
59
for dp, ie in _export_iter_entries(tree, subdir):
61
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest)
63
# zipfile.ZipFile switches all paths to forward
64
# slashes anyway, so just stick with that.
65
filename = osutils.pathjoin(root, dp).encode('utf8')
67
zinfo = zipfile.ZipInfo(
70
zinfo.compress_type = compression
71
zinfo.external_attr = _FILE_ATTR
73
chunks = tree.get_file_lines(file_id)
74
filters = tree._content_filter_stack(dp)
75
context = ContentFilterContext(dp, tree, ie)
76
contents = filtered_output_bytes(chunks, filters, context)
77
content = ''.join(contents)
79
content = tree.get_file_text(file_id)
80
zipf.writestr(zinfo, content)
81
elif ie.kind == "directory":
82
# Directories must contain a trailing slash, to indicate
83
# to the zip routine that they are really directories and
84
# not just empty files.
85
zinfo = zipfile.ZipInfo(
86
filename=filename + '/',
88
zinfo.compress_type = compression
89
zinfo.external_attr = _DIR_ATTR
90
zipf.writestr(zinfo,'')
91
elif ie.kind == "symlink":
92
zinfo = zipfile.ZipInfo(
93
filename=(filename + '.lnk'),
95
zinfo.compress_type = compression
96
zinfo.external_attr = _FILE_ATTR
97
zipf.writestr(zinfo, ie.symlink_target)
101
except UnicodeEncodeError:
104
from bzrlib.errors import BzrError
105
raise BzrError("Can't export non-ascii filenames to zip")