~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

  • Committer: Robert Collins
  • Date: 2007-03-08 04:06:06 UTC
  • mfrom: (2323.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070308040606-84gsniv56huiyjt4
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Canonical Ltd
2
 
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
"""
19
19
 
20
20
import os
 
21
import stat
 
22
import zipfile
 
23
 
 
24
from bzrlib import (
 
25
    osutils,
 
26
    )
21
27
from bzrlib.trace import mutter
22
 
import zipfile
 
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
 
 
34
_FILE_ATTR = stat.S_IFREG
 
35
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
 
36
 
23
37
 
24
38
def zip_exporter(tree, dest, root):
25
39
    """ Export this tree to a new zip file.
38
52
    inv = tree.inventory
39
53
 
40
54
    try:
41
 
        for dp, ie in inv.iter_entries():
 
55
        entries = inv.iter_entries()
 
56
        entries.next() # skip root
 
57
        for dp, ie in entries:
42
58
            # .bzrignore has no meaning outside of a working tree
43
59
            # so do not export it
44
60
            if dp == ".bzrignore":
47
63
            file_id = ie.file_id
48
64
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
49
65
 
50
 
            if ie.kind == "file": 
 
66
            # zipfile.ZipFile switches all paths to forward
 
67
            # slashes anyway, so just stick with that.
 
68
            filename = osutils.pathjoin(root, dp).encode('utf8')
 
69
            if ie.kind == "file":
51
70
                zinfo = zipfile.ZipInfo(
52
 
                            filename=str(os.path.join(root, dp)),
 
71
                            filename=filename,
53
72
                            date_time=now)
54
73
                zinfo.compress_type = compression
 
74
                zinfo.external_attr = _FILE_ATTR
55
75
                zipf.writestr(zinfo, tree.get_file_text(file_id))
56
76
            elif ie.kind == "directory":
 
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.
57
80
                zinfo = zipfile.ZipInfo(
58
 
                            filename=str(os.path.join(root, dp)+os.sep),
 
81
                            filename=filename + '/',
59
82
                            date_time=now)
60
83
                zinfo.compress_type = compression
 
84
                zinfo.external_attr = _DIR_ATTR
61
85
                zipf.writestr(zinfo,'')
62
86
            elif ie.kind == "symlink":
63
87
                zinfo = zipfile.ZipInfo(
64
 
                            filename=str(os.path.join(root, dp+".lnk")),
 
88
                            filename=(filename + '.lnk'),
65
89
                            date_time=now)
66
90
                zinfo.compress_type = compression
 
91
                zinfo.external_attr = _FILE_ATTR
67
92
                zipf.writestr(zinfo, ie.symlink_target)
68
93
 
69
94
        zipf.close()