~ubuntu-branches/ubuntu/maverick/bzr/maverick

1.4.6 by Jelmer Vernooij
Import upstream version 2.1.1
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
1.1.11 by Martin Pitt
Import upstream version 0.9
2
#
1.1.1 by Andres Salomon
Import upstream version 0.7
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.
1.1.11 by Martin Pitt
Import upstream version 0.9
7
#
1.1.1 by Andres Salomon
Import upstream version 0.7
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.
1.1.11 by Martin Pitt
Import upstream version 0.9
12
#
1.1.1 by Andres Salomon
Import upstream version 0.7
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
1.2.3 by Jelmer Vernooij
Import upstream version 1.14~rc1
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1.1.1 by Andres Salomon
Import upstream version 0.7
16
17
"""Export a Tree to a non-versioned directory.
18
"""
19
20
import os
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
21
import stat
1.1.46 by Jelmer Vernooij
Import upstream version 1.9
22
import time
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
23
import zipfile
24
25
from bzrlib import (
26
    osutils,
27
    )
1.1.46 by Jelmer Vernooij
Import upstream version 1.9
28
from bzrlib.export import _export_iter_entries
1.2.3 by Jelmer Vernooij
Import upstream version 1.14~rc1
29
from bzrlib.filters import (
30
    ContentFilterContext,
31
    filtered_output_bytes,
32
    )
1.1.1 by Andres Salomon
Import upstream version 0.7
33
from bzrlib.trace import mutter
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
34
35
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)
1.5.2 by Jelmer Vernooij
Import upstream version 2.1.0~rc2
39
FILE_PERMISSIONS = (0644 << 16)
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
40
1.5.2 by Jelmer Vernooij
Import upstream version 2.1.0~rc2
41
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
42
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
43
1.1.1 by Andres Salomon
Import upstream version 0.7
44
1.2.3 by Jelmer Vernooij
Import upstream version 1.14~rc1
45
def zip_exporter(tree, dest, root, subdir, filtered=False):
1.1.1 by Andres Salomon
Import upstream version 0.7
46
    """ Export this tree to a new zip file.
47
48
    `dest` will be created holding the contents of this tree; if it
49
    already exists, it will be overwritten".
50
    """
1.1.46 by Jelmer Vernooij
Import upstream version 1.9
51
    mutter('export version %r', tree)
1.1.1 by Andres Salomon
Import upstream version 0.7
52
53
    now = time.localtime()[:6]
54
55
    compression = zipfile.ZIP_DEFLATED
56
    zipf = zipfile.ZipFile(dest, "w", compression)
57
58
    try:
1.1.46 by Jelmer Vernooij
Import upstream version 1.9
59
        for dp, ie in _export_iter_entries(tree, subdir):
1.1.1 by Andres Salomon
Import upstream version 0.7
60
            file_id = ie.file_id
61
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
62
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
63
            # zipfile.ZipFile switches all paths to forward
64
            # slashes anyway, so just stick with that.
65
            filename = osutils.pathjoin(root, dp).encode('utf8')
66
            if ie.kind == "file":
1.1.1 by Andres Salomon
Import upstream version 0.7
67
                zinfo = zipfile.ZipInfo(
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
68
                            filename=filename,
1.1.1 by Andres Salomon
Import upstream version 0.7
69
                            date_time=now)
70
                zinfo.compress_type = compression
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
71
                zinfo.external_attr = _FILE_ATTR
1.2.3 by Jelmer Vernooij
Import upstream version 1.14~rc1
72
                if filtered:
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)
78
                else:
79
                    content = tree.get_file_text(file_id)
80
                zipf.writestr(zinfo, content)
1.1.1 by Andres Salomon
Import upstream version 0.7
81
            elif ie.kind == "directory":
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
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.
1.1.1 by Andres Salomon
Import upstream version 0.7
85
                zinfo = zipfile.ZipInfo(
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
86
                            filename=filename + '/',
1.1.1 by Andres Salomon
Import upstream version 0.7
87
                            date_time=now)
88
                zinfo.compress_type = compression
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
89
                zinfo.external_attr = _DIR_ATTR
1.1.1 by Andres Salomon
Import upstream version 0.7
90
                zipf.writestr(zinfo,'')
91
            elif ie.kind == "symlink":
92
                zinfo = zipfile.ZipInfo(
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
93
                            filename=(filename + '.lnk'),
1.1.1 by Andres Salomon
Import upstream version 0.7
94
                            date_time=now)
95
                zinfo.compress_type = compression
1.1.14 by Etienne Goyer
Import upstream version 0.11~rc2
96
                zinfo.external_attr = _FILE_ATTR
1.1.1 by Andres Salomon
Import upstream version 0.7
97
                zipf.writestr(zinfo, ie.symlink_target)
98
99
        zipf.close()
100
101
    except UnicodeEncodeError:
102
        zipf.close()
103
        os.remove(dest)
104
        from bzrlib.errors import BzrError
105
        raise BzrError("Can't export non-ascii filenames to zip")