~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
17
"""Export a Tree to a non-versioned directory.
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
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
22
import time
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
23
import zipfile
24
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
25
from bzrlib import (
26
    osutils,
27
    )
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
28
from bzrlib.export import _export_iter_entries
3368.2.32 by Ian Clatworthy
add --filters to export command
29
from bzrlib.filters import (
30
    ContentFilterContext,
31
    filtered_output_bytes,
32
    )
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
33
from bzrlib.trace import mutter
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
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)
4823.1.2 by Ivan Sagalaev
File mode is given a name and gets tested
39
FILE_PERMISSIONS = (0644 << 16)
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
40
4823.1.2 by Ivan Sagalaev
File mode is given a name and gets tested
41
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
42
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
43
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
44
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
45
def zip_exporter(tree, dest, root, subdir, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
46
                 per_file_timestamps=False):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
47
    """ Export this tree to a new zip file.
48
49
    `dest` will be created holding the contents of this tree; if it
50
    already exists, it will be overwritten".
51
    """
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
52
    mutter('export version %r', tree)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
53
54
    now = time.localtime()[:6]
55
56
    compression = zipfile.ZIP_DEFLATED
57
    zipf = zipfile.ZipFile(dest, "w", compression)
58
59
    try:
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
60
        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.
61
            file_id = ie.file_id
62
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
63
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
64
            # zipfile.ZipFile switches all paths to forward
65
            # slashes anyway, so just stick with that.
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
66
            if per_file_timestamps:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
67
                mtime = tree.get_file_mtime(ie.file_id, dp)
68
            else:
69
                mtime = now
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
70
            filename = osutils.pathjoin(root, dp).encode('utf8')
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
71
            if ie.kind == "file":
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
72
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
73
                            filename=filename,
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
74
                            date_time=mtime)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
75
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
76
                zinfo.external_attr = _FILE_ATTR
3368.2.32 by Ian Clatworthy
add --filters to export command
77
                if filtered:
78
                    chunks = tree.get_file_lines(file_id)
79
                    filters = tree._content_filter_stack(dp)
3368.2.33 by Ian Clatworthy
expand filter context to support interesting stuff
80
                    context = ContentFilterContext(dp, tree, ie)
3368.2.32 by Ian Clatworthy
add --filters to export command
81
                    contents = filtered_output_bytes(chunks, filters, context)
82
                    content = ''.join(contents)
83
                else:
84
                    content = tree.get_file_text(file_id)
85
                zipf.writestr(zinfo, content)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
86
            elif ie.kind == "directory":
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
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.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
90
                zinfo = zipfile.ZipInfo(
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
91
                            filename=filename + '/',
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
92
                            date_time=mtime)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
93
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
94
                zinfo.external_attr = _DIR_ATTR
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
95
                zipf.writestr(zinfo,'')
96
            elif ie.kind == "symlink":
97
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
98
                            filename=(filename + '.lnk'),
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
99
                            date_time=mtime)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
100
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
101
                zinfo.external_attr = _FILE_ATTR
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
102
                zipf.writestr(zinfo, ie.symlink_target)
103
104
        zipf.close()
105
106
    except UnicodeEncodeError:
107
        zipf.close()
108
        os.remove(dest)
109
        from bzrlib.errors import BzrError
110
        raise BzrError("Can't export non-ascii filenames to zip")