~bzr-pqm/bzr/bzr.dev

1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
1
# Copyright (C) 2005 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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
22
import zipfile
23
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
24
from bzrlib import (
25
    osutils,
26
    )
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
27
from bzrlib.trace import mutter
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
28
29
30
# Windows expects this bit to be set in the 'external_attr' section
31
# Or it won't consider the entry a directory
32
ZIP_DIRECTORY_BIT = (1 << 4)
33
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
34
_FILE_ATTR = stat.S_IFREG
35
_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
36
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
37
38
def zip_exporter(tree, dest, root):
39
    """ Export this tree to a new zip file.
40
41
    `dest` will be created holding the contents of this tree; if it
42
    already exists, it will be overwritten".
43
    """
44
    import time
45
46
    now = time.localtime()[:6]
47
    mutter('export version %r', tree)
48
49
    compression = zipfile.ZIP_DEFLATED
50
    zipf = zipfile.ZipFile(dest, "w", compression)
51
52
    inv = tree.inventory
53
54
    try:
1852.6.6 by Robert Collins
Finish updating iter_entries change to make all tests pass.
55
        entries = inv.iter_entries()
56
        entries.next() # skip root
57
        for dp, ie in entries:
1185.61.2 by Jamie Wilkinson
exclude .bzrignore from exports
58
            # .bzrignore has no meaning outside of a working tree
59
            # so do not export it
60
            if dp == ".bzrignore":
61
                continue
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
62
63
            file_id = ie.file_id
64
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
65
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
66
            # zipfile.ZipFile switches all paths to forward
67
            # slashes anyway, so just stick with that.
68
            filename = osutils.pathjoin(root, dp).encode('utf8')
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
69
            if ie.kind == "file":
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
70
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
71
                            filename=filename,
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
72
                            date_time=now)
73
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
74
                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.
75
                zipf.writestr(zinfo, tree.get_file_text(file_id))
76
            elif ie.kind == "directory":
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
77
                # Directories must contain a trailing slash, to indicate
78
                # to the zip routine that they are really directories and
79
                # not just empty files.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
80
                zinfo = zipfile.ZipInfo(
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
81
                            filename=filename + '/',
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
82
                            date_time=now)
83
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
84
                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.
85
                zipf.writestr(zinfo,'')
86
            elif ie.kind == "symlink":
87
                zinfo = zipfile.ZipInfo(
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
88
                            filename=(filename + '.lnk'),
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
89
                            date_time=now)
90
                zinfo.compress_type = compression
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
91
                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.
92
                zipf.writestr(zinfo, ie.symlink_target)
93
94
        zipf.close()
95
96
    except UnicodeEncodeError:
97
        zipf.close()
98
        os.remove(dest)
99
        from bzrlib.errors import BzrError
100
        raise BzrError("Can't export non-ascii filenames to zip")
101