~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
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
20
from __future__ import absolute_import
21
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
22
import os
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
23
import stat
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
24
import sys
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
25
import time
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
26
import zipfile
27
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
28
from bzrlib import (
29
    osutils,
30
    )
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
31
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.
32
from bzrlib.trace import mutter
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
33
34
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
35
# Windows expects this bit to be set in the 'external_attr' section,
36
# 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
37
ZIP_DIRECTORY_BIT = (1 << 4)
4823.1.2 by Ivan Sagalaev
File mode is given a name and gets tested
38
FILE_PERMISSIONS = (0644 << 16)
5664.2.1 by Jelmer Vernooij
Fix setting of mode on directories in zip files.
39
DIR_PERMISSIONS = (0755 << 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
5664.2.1 by Jelmer Vernooij
Fix setting of mode on directories in zip files.
42
_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
43
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
44
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
45
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.
46
    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.
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
    """
52
53
    compression = zipfile.ZIP_DEFLATED
5952.1.15 by geoffreyfishing at gmail
Major code cleanup.
54
    if fileobj is not None:
55
        dest = fileobj
56
    elif dest == "-":
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
57
        dest = sys.stdout
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
58
    zipf = zipfile.ZipFile(dest, "w", compression)
59
    try:
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
60
        for dp, tp, 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.
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
66
            if force_mtime is not None:
67
                mtime = force_mtime
68
            else:
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
69
                mtime = tree.get_file_mtime(ie.file_id, tp)
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
70
            date_time = time.localtime(mtime)[:6]
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
71
            filename = osutils.pathjoin(root, dp).encode('utf8')
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
72
            if ie.kind == "file":
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
73
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
74
                            filename=filename,
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
75
                            date_time=date_time)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
76
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
77
                zinfo.external_attr = _FILE_ATTR
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
78
                content = tree.get_file_text(file_id, tp)
3368.2.32 by Ian Clatworthy
add --filters to export command
79
                zipf.writestr(zinfo, content)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
80
            elif ie.kind == "directory":
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
81
                # Directories must contain a trailing slash, to indicate
82
                # to the zip routine that they are really directories and
83
                # not just empty files.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
84
                zinfo = zipfile.ZipInfo(
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
85
                            filename=filename + '/',
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
86
                            date_time=date_time)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
87
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
88
                zinfo.external_attr = _DIR_ATTR
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
89
                zipf.writestr(zinfo, '')
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
90
            elif ie.kind == "symlink":
91
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
92
                            filename=(filename + '.lnk'),
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
93
                            date_time=date_time)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
94
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
95
                zinfo.external_attr = _FILE_ATTR
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
96
                zipf.writestr(zinfo, tree.get_symlink_target(file_id, tp))
5952.1.7 by geoffreyfishing at gmail
Made zip exporter a generator.
97
            yield
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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")