~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

  • Committer: Patch Queue Manager
  • Date: 2016-01-15 09:21:49 UTC
  • mfrom: (6606.2.1 autodoc-unicode)
  • Revision ID: pqm@pqm.ubuntu.com-20160115092149-z5f4sfq3jvaz0enb
(vila) Fix autodoc runner when LANG=C. (Jelmer Vernooij)

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
from __future__ import absolute_import
 
21
 
20
22
import os
21
23
import stat
 
24
import sys
22
25
import time
23
26
import zipfile
24
27
 
26
29
    osutils,
27
30
    )
28
31
from bzrlib.export import _export_iter_entries
29
 
from bzrlib.filters import (
30
 
    ContentFilterContext,
31
 
    filtered_output_bytes,
32
 
    )
33
32
from bzrlib.trace import mutter
34
33
 
35
34
 
36
 
# Windows expects this bit to be set in the 'external_attr' section
37
 
# Or it won't consider the entry a directory
 
35
# Windows expects this bit to be set in the 'external_attr' section,
 
36
# or it won't consider the entry a directory.
38
37
ZIP_DIRECTORY_BIT = (1 << 4)
39
38
FILE_PERMISSIONS = (0644 << 16)
 
39
DIR_PERMISSIONS = (0755 << 16)
40
40
 
41
41
_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):
 
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):
47
47
    """ Export this tree to a new zip file.
48
48
 
49
49
    `dest` will be created holding the contents of this tree; if it
50
50
    already exists, it will be overwritten".
51
51
    """
52
 
    mutter('export version %r', tree)
53
 
 
54
 
    now = time.localtime()[:6]
55
52
 
56
53
    compression = zipfile.ZIP_DEFLATED
 
54
    if fileobj is not None:
 
55
        dest = fileobj
 
56
    elif dest == "-":
 
57
        dest = sys.stdout
57
58
    zipf = zipfile.ZipFile(dest, "w", compression)
58
 
 
59
59
    try:
60
 
        for dp, ie in _export_iter_entries(tree, subdir):
 
60
        for dp, tp, ie in _export_iter_entries(tree, subdir):
61
61
            file_id = ie.file_id
62
62
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
63
63
 
64
64
            # zipfile.ZipFile switches all paths to forward
65
65
            # slashes anyway, so just stick with that.
66
 
            if per_file_timestamps:
67
 
                mtime = tree.get_file_mtime(ie.file_id, dp)
 
66
            if force_mtime is not None:
 
67
                mtime = force_mtime
68
68
            else:
69
 
                mtime = now
 
69
                mtime = tree.get_file_mtime(ie.file_id, tp)
 
70
            date_time = time.localtime(mtime)[:6]
70
71
            filename = osutils.pathjoin(root, dp).encode('utf8')
71
72
            if ie.kind == "file":
72
73
                zinfo = zipfile.ZipInfo(
73
74
                            filename=filename,
74
 
                            date_time=mtime)
 
75
                            date_time=date_time)
75
76
                zinfo.compress_type = compression
76
77
                zinfo.external_attr = _FILE_ATTR
77
 
                if filtered:
78
 
                    chunks = tree.get_file_lines(file_id)
79
 
                    filters = tree._content_filter_stack(dp)
80
 
                    context = ContentFilterContext(dp, tree, ie)
81
 
                    contents = filtered_output_bytes(chunks, filters, context)
82
 
                    content = ''.join(contents)
83
 
                else:
84
 
                    content = tree.get_file_text(file_id)
 
78
                content = tree.get_file_text(file_id, tp)
85
79
                zipf.writestr(zinfo, content)
86
80
            elif ie.kind == "directory":
87
81
                # Directories must contain a trailing slash, to indicate
89
83
                # not just empty files.
90
84
                zinfo = zipfile.ZipInfo(
91
85
                            filename=filename + '/',
92
 
                            date_time=mtime)
 
86
                            date_time=date_time)
93
87
                zinfo.compress_type = compression
94
88
                zinfo.external_attr = _DIR_ATTR
95
 
                zipf.writestr(zinfo,'')
 
89
                zipf.writestr(zinfo, '')
96
90
            elif ie.kind == "symlink":
97
91
                zinfo = zipfile.ZipInfo(
98
92
                            filename=(filename + '.lnk'),
99
 
                            date_time=mtime)
 
93
                            date_time=date_time)
100
94
                zinfo.compress_type = compression
101
95
                zinfo.external_attr = _FILE_ATTR
102
 
                zipf.writestr(zinfo, ie.symlink_target)
 
96
                zipf.writestr(zinfo, tree.get_symlink_target(file_id, tp))
 
97
            yield
103
98
 
104
99
        zipf.close()
105
100