1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Export a Tree to a non-versioned directory.
28
28
from bzrlib.export import _export_iter_entries
29
from bzrlib.filters import (
31
filtered_output_bytes,
29
33
from bzrlib.trace import mutter
32
36
# Windows expects this bit to be set in the 'external_attr' section
33
37
# Or it won't consider the entry a directory
34
38
ZIP_DIRECTORY_BIT = (1 << 4)
39
FILE_PERMISSIONS = (0644 << 16)
36
_FILE_ATTR = stat.S_IFREG
41
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
37
42
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
40
def zip_exporter(tree, dest, root, subdir):
45
def zip_exporter(tree, dest, root, subdir, filtered=False,
46
per_file_timestamps=False):
41
47
""" Export this tree to a new zip file.
43
49
`dest` will be created holding the contents of this tree; if it
58
64
# zipfile.ZipFile switches all paths to forward
59
65
# slashes anyway, so just stick with that.
66
if per_file_timestamps:
67
mtime = tree.get_file_mtime(ie.file_id, dp)
60
70
filename = osutils.pathjoin(root, dp).encode('utf8')
61
71
if ie.kind == "file":
62
72
zinfo = zipfile.ZipInfo(
65
75
zinfo.compress_type = compression
66
76
zinfo.external_attr = _FILE_ATTR
67
zipf.writestr(zinfo, tree.get_file_text(file_id))
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)
84
content = tree.get_file_text(file_id)
85
zipf.writestr(zinfo, content)
68
86
elif ie.kind == "directory":
69
87
# Directories must contain a trailing slash, to indicate
70
88
# to the zip routine that they are really directories and
71
89
# not just empty files.
72
90
zinfo = zipfile.ZipInfo(
73
91
filename=filename + '/',
75
93
zinfo.compress_type = compression
76
94
zinfo.external_attr = _DIR_ATTR
77
95
zipf.writestr(zinfo,'')
78
96
elif ie.kind == "symlink":
79
97
zinfo = zipfile.ZipInfo(
80
98
filename=(filename + '.lnk'),
82
100
zinfo.compress_type = compression
83
101
zinfo.external_attr = _FILE_ATTR
84
102
zipf.writestr(zinfo, ie.symlink_target)