~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
16
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
17
"""Export a Tree to a zip file.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
18
"""
19
20
import os
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
21
import stat
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
22
import sys
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
23
import time
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
24
import zipfile
25
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
26
from bzrlib import (
27
    osutils,
28
    )
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
29
from bzrlib.export import _export_iter_entries
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
30
from bzrlib.trace import mutter
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
31
32
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
33
# Windows expects this bit to be set in the 'external_attr' section,
34
# or it won't consider the entry a directory.
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
35
ZIP_DIRECTORY_BIT = (1 << 4)
4823.1.2 by Ivan Sagalaev
File mode is given a name and gets tested
36
FILE_PERMISSIONS = (0644 << 16)
5664.2.1 by Jelmer Vernooij
Fix setting of mode on directories in zip files.
37
DIR_PERMISSIONS = (0755 << 16)
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
38
4823.1.2 by Ivan Sagalaev
File mode is given a name and gets tested
39
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
5664.2.1 by Jelmer Vernooij
Fix setting of mode on directories in zip files.
40
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
41
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
42
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
43
def zip_exporter_generator(tree, dest, root, subdir=None,
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
44
    force_mtime=None, fileobj=None):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
45
    """ Export this tree to a new zip file.
46
47
    `dest` will be created holding the contents of this tree; if it
48
    already exists, it will be overwritten".
49
    """
50
51
    compression = zipfile.ZIP_DEFLATED
5952.1.15 by geoffreyfishing at gmail
Major code cleanup.
52
    if fileobj is not None:
53
        dest = fileobj
54
    elif dest == "-":
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
55
        dest = sys.stdout
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
56
    zipf = zipfile.ZipFile(dest, "w", compression)
57
    try:
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
58
        for dp, ie in _export_iter_entries(tree, subdir):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
59
            file_id = ie.file_id
60
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
61
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
62
            # zipfile.ZipFile switches all paths to forward
63
            # slashes anyway, so just stick with that.
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
64
            if force_mtime is not None:
65
                mtime = force_mtime
66
            else:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
67
                mtime = tree.get_file_mtime(ie.file_id, dp)
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
68
            date_time = time.localtime(mtime)[:6]
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
69
            filename = osutils.pathjoin(root, dp).encode('utf8')
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
70
            if ie.kind == "file":
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
71
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
72
                            filename=filename,
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
73
                            date_time=date_time)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
74
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
75
                zinfo.external_attr = _FILE_ATTR
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
76
                content = tree.get_file_text(file_id)
3368.2.32 by Ian Clatworthy
add --filters to export command
77
                zipf.writestr(zinfo, content)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
78
            elif ie.kind == "directory":
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
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.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
82
                zinfo = zipfile.ZipInfo(
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
83
                            filename=filename + '/',
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
84
                            date_time=date_time)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
85
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
86
                zinfo.external_attr = _DIR_ATTR
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
87
                zipf.writestr(zinfo, '')
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
88
            elif ie.kind == "symlink":
89
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
90
                            filename=(filename + '.lnk'),
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
91
                            date_time=date_time)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
92
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
93
                zinfo.external_attr = _FILE_ATTR
5809.3.8 by Aaron Bentley
Test zip files with all tree types.
94
                zipf.writestr(zinfo, tree.get_symlink_target(file_id))
5952.1.7 by geoffreyfishing at gmail
Made zip exporter a generator.
95
            yield
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
96
97
        zipf.close()
98
99
    except UnicodeEncodeError:
100
        zipf.close()
101
        os.remove(dest)
102
        from bzrlib.errors import BzrError
103
        raise BzrError("Can't export non-ascii filenames to zip")