~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
 
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
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""Export a Tree to a zip file.
 
17
"""Export a Tree to a non-versioned directory.
18
18
"""
19
19
 
20
 
from __future__ import absolute_import
21
 
 
22
20
import os
23
21
import stat
24
 
import sys
25
22
import time
26
23
import zipfile
27
24
 
32
29
from bzrlib.trace import mutter
33
30
 
34
31
 
35
 
# Windows expects this bit to be set in the 'external_attr' section,
36
 
# or it won't consider the entry a directory.
 
32
# Windows expects this bit to be set in the 'external_attr' section
 
33
# Or it won't consider the entry a directory
37
34
ZIP_DIRECTORY_BIT = (1 << 4)
38
 
FILE_PERMISSIONS = (0644 << 16)
39
 
DIR_PERMISSIONS = (0755 << 16)
40
 
 
41
 
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
42
 
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
43
 
 
44
 
 
45
 
def zip_exporter_generator(tree, dest, root, subdir=None,
46
 
    force_mtime=None, fileobj=None):
 
35
 
 
36
_FILE_ATTR = stat.S_IFREG
 
37
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
 
38
 
 
39
 
 
40
def zip_exporter(tree, dest, root, subdir):
47
41
    """ Export this tree to a new zip file.
48
42
 
49
43
    `dest` will be created holding the contents of this tree; if it
50
44
    already exists, it will be overwritten".
51
45
    """
 
46
    mutter('export version %r', tree)
 
47
 
 
48
    now = time.localtime()[:6]
52
49
 
53
50
    compression = zipfile.ZIP_DEFLATED
54
 
    if fileobj is not None:
55
 
        dest = fileobj
56
 
    elif dest == "-":
57
 
        dest = sys.stdout
58
51
    zipf = zipfile.ZipFile(dest, "w", compression)
 
52
 
59
53
    try:
60
 
        for dp, tp, ie in _export_iter_entries(tree, subdir):
 
54
        for dp, ie in _export_iter_entries(tree, subdir):
61
55
            file_id = ie.file_id
62
56
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
63
57
 
64
58
            # zipfile.ZipFile switches all paths to forward
65
59
            # slashes anyway, so just stick with that.
66
 
            if force_mtime is not None:
67
 
                mtime = force_mtime
68
 
            else:
69
 
                mtime = tree.get_file_mtime(ie.file_id, tp)
70
 
            date_time = time.localtime(mtime)[:6]
71
60
            filename = osutils.pathjoin(root, dp).encode('utf8')
72
61
            if ie.kind == "file":
73
62
                zinfo = zipfile.ZipInfo(
74
63
                            filename=filename,
75
 
                            date_time=date_time)
 
64
                            date_time=now)
76
65
                zinfo.compress_type = compression
77
66
                zinfo.external_attr = _FILE_ATTR
78
 
                content = tree.get_file_text(file_id, tp)
79
 
                zipf.writestr(zinfo, content)
 
67
                zipf.writestr(zinfo, tree.get_file_text(file_id))
80
68
            elif ie.kind == "directory":
81
69
                # Directories must contain a trailing slash, to indicate
82
70
                # to the zip routine that they are really directories and
83
71
                # not just empty files.
84
72
                zinfo = zipfile.ZipInfo(
85
73
                            filename=filename + '/',
86
 
                            date_time=date_time)
 
74
                            date_time=now)
87
75
                zinfo.compress_type = compression
88
76
                zinfo.external_attr = _DIR_ATTR
89
 
                zipf.writestr(zinfo, '')
 
77
                zipf.writestr(zinfo,'')
90
78
            elif ie.kind == "symlink":
91
79
                zinfo = zipfile.ZipInfo(
92
80
                            filename=(filename + '.lnk'),
93
 
                            date_time=date_time)
 
81
                            date_time=now)
94
82
                zinfo.compress_type = compression
95
83
                zinfo.external_attr = _FILE_ATTR
96
 
                zipf.writestr(zinfo, tree.get_symlink_target(file_id, tp))
97
 
            yield
 
84
                zipf.writestr(zinfo, ie.symlink_target)
98
85
 
99
86
        zipf.close()
100
87
 
103
90
        os.remove(dest)
104
91
        from bzrlib.errors import BzrError
105
92
        raise BzrError("Can't export non-ascii filenames to zip")
 
93