~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import os
21
21
import stat
 
22
import sys
22
23
import time
23
24
import zipfile
24
25
 
37
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(tree, dest, root, subdir=None, filtered=False, force_mtime=None):
47
48
    """ Export this tree to a new zip file.
48
49
 
49
50
    `dest` will be created holding the contents of this tree; if it
50
51
    already exists, it will be overwritten".
51
52
    """
52
 
    mutter('export version %r', tree)
53
 
 
54
 
    now = time.localtime()[:6]
55
53
 
56
54
    compression = zipfile.ZIP_DEFLATED
 
55
    if dest == "-":
 
56
        dest = sys.stdout
57
57
    zipf = zipfile.ZipFile(dest, "w", compression)
58
 
 
59
58
    try:
60
59
        for dp, ie in _export_iter_entries(tree, subdir):
61
60
            file_id = ie.file_id
63
62
 
64
63
            # zipfile.ZipFile switches all paths to forward
65
64
            # slashes anyway, so just stick with that.
66
 
            if per_file_timestamps:
 
65
            if force_mtime is not None:
 
66
                mtime = force_mtime
 
67
            else:
67
68
                mtime = tree.get_file_mtime(ie.file_id, dp)
68
 
            else:
69
 
                mtime = now
 
69
            date_time = time.localtime(mtime)[:6]
70
70
            filename = osutils.pathjoin(root, dp).encode('utf8')
71
71
            if ie.kind == "file":
72
72
                zinfo = zipfile.ZipInfo(
73
73
                            filename=filename,
74
 
                            date_time=mtime)
 
74
                            date_time=date_time)
75
75
                zinfo.compress_type = compression
76
76
                zinfo.external_attr = _FILE_ATTR
77
77
                if filtered:
89
89
                # not just empty files.
90
90
                zinfo = zipfile.ZipInfo(
91
91
                            filename=filename + '/',
92
 
                            date_time=mtime)
 
92
                            date_time=date_time)
93
93
                zinfo.compress_type = compression
94
94
                zinfo.external_attr = _DIR_ATTR
95
95
                zipf.writestr(zinfo,'')
96
96
            elif ie.kind == "symlink":
97
97
                zinfo = zipfile.ZipInfo(
98
98
                            filename=(filename + '.lnk'),
99
 
                            date_time=mtime)
 
99
                            date_time=date_time)
100
100
                zinfo.compress_type = compression
101
101
                zinfo.external_attr = _FILE_ATTR
102
102
                zipf.writestr(zinfo, ie.symlink_target)