~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
2
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.
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.
12
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
21
from bzrlib.trace import mutter
22
import zipfile
23
24
def zip_exporter(tree, dest, root):
25
    """ Export this tree to a new zip file.
26
27
    `dest` will be created holding the contents of this tree; if it
28
    already exists, it will be overwritten".
29
    """
30
    import time
31
32
    now = time.localtime()[:6]
33
    mutter('export version %r', tree)
34
35
    compression = zipfile.ZIP_DEFLATED
36
    zipf = zipfile.ZipFile(dest, "w", compression)
37
38
    inv = tree.inventory
39
40
    try:
41
        for dp, ie in inv.iter_entries():
1185.61.2 by Jamie Wilkinson
exclude .bzrignore from exports
42
            # .bzrignore has no meaning outside of a working tree
43
            # so do not export it
44
            if dp == ".bzrignore":
45
                continue
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
46
47
            file_id = ie.file_id
48
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
49
50
            if ie.kind == "file": 
51
                zinfo = zipfile.ZipInfo(
1185.31.35 by John Arbash Meinel
Couple small fixes, all tests pass on cygwin.
52
                            filename=str(os.path.join(root, dp)),
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
53
                            date_time=now)
54
                zinfo.compress_type = compression
55
                zipf.writestr(zinfo, tree.get_file_text(file_id))
56
            elif ie.kind == "directory":
57
                zinfo = zipfile.ZipInfo(
1185.31.35 by John Arbash Meinel
Couple small fixes, all tests pass on cygwin.
58
                            filename=str(os.path.join(root, dp)+os.sep),
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
59
                            date_time=now)
60
                zinfo.compress_type = compression
61
                zipf.writestr(zinfo,'')
62
            elif ie.kind == "symlink":
63
                zinfo = zipfile.ZipInfo(
1185.31.35 by John Arbash Meinel
Couple small fixes, all tests pass on cygwin.
64
                            filename=str(os.path.join(root, dp+".lnk")),
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
65
                            date_time=now)
66
                zinfo.compress_type = compression
67
                zipf.writestr(zinfo, ie.symlink_target)
68
69
        zipf.close()
70
71
    except UnicodeEncodeError:
72
        zipf.close()
73
        os.remove(dest)
74
        from bzrlib.errors import BzrError
75
        raise BzrError("Can't export non-ascii filenames to zip")
76