~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Export a Tree to a non-versioned directory.
 
17
"""Export a Tree to a zip file.
18
18
"""
19
19
 
20
20
import os
21
21
import stat
 
22
import sys
22
23
import time
23
24
import zipfile
24
25
 
33
34
from bzrlib.trace import mutter
34
35
 
35
36
 
36
 
# Windows expects this bit to be set in the 'external_attr' section
37
 
# Or it won't consider the entry a directory
 
37
# Windows expects this bit to be set in the 'external_attr' section,
 
38
# or it won't consider the entry a directory.
38
39
ZIP_DIRECTORY_BIT = (1 << 4)
39
40
FILE_PERMISSIONS = (0644 << 16)
 
41
DIR_PERMISSIONS = (0755 << 16)
40
42
 
41
43
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
42
 
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
43
 
 
44
 
 
45
 
def zip_exporter(tree, dest, root, subdir, filtered=False,
46
 
                 per_file_timestamps=False):
 
44
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
 
45
 
 
46
 
 
47
def zip_exporter_generator(tree, dest, root, subdir=None, filtered=False,
 
48
    force_mtime=None, fileobj=None):
47
49
    """ Export this tree to a new zip file.
48
50
 
49
51
    `dest` will be created holding the contents of this tree; if it
50
52
    already exists, it will be overwritten".
51
53
    """
52
 
    mutter('export version %r', tree)
53
 
 
54
 
    now = time.localtime()[:6]
55
54
 
56
55
    compression = zipfile.ZIP_DEFLATED
 
56
    if fileobj is not None:
 
57
        dest = fileobj
 
58
    elif dest == "-":
 
59
        dest = sys.stdout
57
60
    zipf = zipfile.ZipFile(dest, "w", compression)
58
 
 
59
61
    try:
60
62
        for dp, ie in _export_iter_entries(tree, subdir):
61
63
            file_id = ie.file_id
63
65
 
64
66
            # zipfile.ZipFile switches all paths to forward
65
67
            # slashes anyway, so just stick with that.
66
 
            if per_file_timestamps:
 
68
            if force_mtime is not None:
 
69
                mtime = force_mtime
 
70
            else:
67
71
                mtime = tree.get_file_mtime(ie.file_id, dp)
68
 
            else:
69
 
                mtime = now
 
72
            date_time = time.localtime(mtime)[:6]
70
73
            filename = osutils.pathjoin(root, dp).encode('utf8')
71
74
            if ie.kind == "file":
72
75
                zinfo = zipfile.ZipInfo(
73
76
                            filename=filename,
74
 
                            date_time=mtime)
 
77
                            date_time=date_time)
75
78
                zinfo.compress_type = compression
76
79
                zinfo.external_attr = _FILE_ATTR
77
80
                if filtered:
89
92
                # not just empty files.
90
93
                zinfo = zipfile.ZipInfo(
91
94
                            filename=filename + '/',
92
 
                            date_time=mtime)
 
95
                            date_time=date_time)
93
96
                zinfo.compress_type = compression
94
97
                zinfo.external_attr = _DIR_ATTR
95
 
                zipf.writestr(zinfo,'')
 
98
                zipf.writestr(zinfo, '')
96
99
            elif ie.kind == "symlink":
97
100
                zinfo = zipfile.ZipInfo(
98
101
                            filename=(filename + '.lnk'),
99
 
                            date_time=mtime)
 
102
                            date_time=date_time)
100
103
                zinfo.compress_type = compression
101
104
                zinfo.external_attr = _FILE_ATTR
102
 
                zipf.writestr(zinfo, ie.symlink_target)
 
105
                zipf.writestr(zinfo, tree.get_symlink_target(file_id))
 
106
            yield
103
107
 
104
108
        zipf.close()
105
109