/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5724.1.3 by John Arbash Meinel
Change the exporters to ensure that we are writing the data out in binary mode.
1
# Copyright (C) 2005, 2006, 2008-2011 Canonical Ltd
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
2
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
7
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
12
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
16
17
"""Export a Tree to a non-versioned directory.
18
"""
19
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
20
import os
3368.2.32 by Ian Clatworthy
add --filters to export command
21
import StringIO
22
import sys
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
23
import tarfile
3408.7.1 by Martin Pool
Support tarball export to stdout
24
5718.1.1 by Jelmer Vernooij
Fix import of BzrError.
25
from bzrlib import (
26
    errors,
27
    osutils,
28
    )
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
29
from bzrlib.export import _export_iter_entries
3368.2.32 by Ian Clatworthy
add --filters to export command
30
from bzrlib.filters import (
31
    ContentFilterContext,
32
    filtered_output_bytes,
33
    )
5718.5.2 by Jelmer Vernooij
Factor out export_tarball.
34
35
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
36
def export_tarball(tree, ball, root, subdir=None, filtered=False,
5718.5.2 by Jelmer Vernooij
Factor out export_tarball.
37
                   force_mtime=None):
38
    """Export tree contents to a tarball.
39
40
    :param tree: Tree to export
41
    :param ball: Tarball to export to
42
    :param filtered: Whether to apply filters
43
    :param subdir: Sub directory to export
44
    :param force_mtime: Option mtime to force, instead of using
45
        tree timestamps.
46
    """
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
47
    for dp, ie in _export_iter_entries(tree, subdir):
3368.2.32 by Ian Clatworthy
add --filters to export command
48
        filename = osutils.pathjoin(root, dp).encode('utf8')
49
        item = tarfile.TarInfo(filename)
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
50
        if force_mtime is not None:
51
            item.mtime = force_mtime
52
        else:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
53
            item.mtime = tree.get_file_mtime(ie.file_id, dp)
3368.2.32 by Ian Clatworthy
add --filters to export command
54
        if ie.kind == "file":
55
            item.type = tarfile.REGTYPE
56
            if tree.is_executable(ie.file_id):
57
                item.mode = 0755
58
            else:
59
                item.mode = 0644
60
            if filtered:
61
                chunks = tree.get_file_lines(ie.file_id)
62
                filters = tree._content_filter_stack(dp)
3368.2.33 by Ian Clatworthy
expand filter context to support interesting stuff
63
                context = ContentFilterContext(dp, tree, ie)
3368.2.32 by Ian Clatworthy
add --filters to export command
64
                contents = filtered_output_bytes(chunks, filters, context)
65
                content = ''.join(contents)
66
                item.size = len(content)
67
                fileobj = StringIO.StringIO(content)
68
            else:
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
69
                item.size = tree.get_file_size(ie.file_id)
3368.2.32 by Ian Clatworthy
add --filters to export command
70
                fileobj = tree.get_file(ie.file_id)
71
        elif ie.kind == "directory":
72
            item.type = tarfile.DIRTYPE
73
            item.name += '/'
74
            item.size = 0
75
            item.mode = 0755
76
            fileobj = None
77
        elif ie.kind == "symlink":
78
            item.type = tarfile.SYMTYPE
79
            item.size = 0
80
            item.mode = 0755
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
81
            item.linkname = tree.get_symlink_target(ie.file_id)
3368.2.32 by Ian Clatworthy
add --filters to export command
82
            fileobj = None
83
        else:
5718.1.1 by Jelmer Vernooij
Fix import of BzrError.
84
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
3368.2.32 by Ian Clatworthy
add --filters to export command
85
                           (ie.file_id, ie.kind))
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
86
        ball.addfile(item, fileobj)
87
88
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
89
def tgz_exporter(tree, dest, root, subdir, filtered=False, force_mtime=None):
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
90
    """Export this tree to a new tar file.
91
92
    `dest` will be created holding the contents of this tree; if it
93
    already exists, it will be clobbered, like with "tar -c".
94
    """
95
    import gzip
5718.5.16 by Jelmer Vernooij
Use revision tree timestamp if possible.
96
    if force_mtime is not None:
97
        root_mtime = force_mtime
98
    elif (getattr(tree, "repository", None) and
99
          getattr(tree, "get_revision_id", None)):
100
        # If this is a revision tree, use the revisions' timestamp
101
        rev = tree.repository.get_revision(tree.get_revision_id())
102
        root_mtime = rev.timestamp
103
    elif tree.get_root_id() is not None:
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
104
        root_mtime = tree.get_file_mtime(tree.get_root_id())
5718.5.9 by Jelmer Vernooij
Add test for export zip to stdout.
105
    else:
5718.5.23 by Jelmer Vernooij
Leave default timestamp.
106
        root_mtime = None
5861.2.2 by Wouter van Heyst
Heavy handed approach to ensuring all streams are closed
107
108
    is_stdout = False
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
109
    if dest == '-':
5718.5.22 by Jelmer Vernooij
Cope with mtime not always being available.
110
        basename = None
111
        stream = sys.stdout
5861.2.2 by Wouter van Heyst
Heavy handed approach to ensuring all streams are closed
112
        is_stdout = True
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
113
    else:
5724.1.4 by John Arbash Meinel
actually make the plain_tar export work again.
114
        stream = open(dest, 'wb')
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
115
        # gzip file is used with an explicit fileobj so that
116
        # the basename can be stored in the gzip file rather than
117
        # dest. (bug 102234)
5718.5.22 by Jelmer Vernooij
Cope with mtime not always being available.
118
        basename = os.path.basename(dest)
119
    try:
5861.2.2 by Wouter van Heyst
Heavy handed approach to ensuring all streams are closed
120
        zipstream = gzip.GzipFile(basename, 'w', fileobj=stream, mtime=root_mtime)
5718.5.22 by Jelmer Vernooij
Cope with mtime not always being available.
121
    except TypeError:
122
        # Python < 2.7 doesn't support the mtime argument
5861.2.2 by Wouter van Heyst
Heavy handed approach to ensuring all streams are closed
123
        zipstream = gzip.GzipFile(basename, 'w', fileobj=stream)
124
    ball = tarfile.open(None, 'w|', fileobj=zipstream)
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
125
    export_tarball(tree, ball, root, subdir, filtered=filtered,
126
                   force_mtime=force_mtime)
127
    ball.close()
5861.2.2 by Wouter van Heyst
Heavy handed approach to ensuring all streams are closed
128
    zipstream.close()
129
    if not is_stdout:
130
        stream.close()
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
131
132
133
def tbz_exporter(tree, dest, root, subdir, filtered=False, force_mtime=None):
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
134
    """Export this tree to a new tar file.
135
136
    `dest` will be created holding the contents of this tree; if it
137
    already exists, it will be clobbered, like with "tar -c".
138
    """
139
    if dest == '-':
140
        ball = tarfile.open(None, 'w|bz2', sys.stdout)
141
    else:
142
        # tarfile.open goes on to do 'os.getcwd() + dest' for opening
143
        # the tar file. With dest being unicode, this throws UnicodeDecodeError
144
        # unless we encode dest before passing it on. This works around
145
        # upstream python bug http://bugs.python.org/issue8396
146
        # (fixed in Python 2.6.5 and 2.7b1)
147
        ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:bz2')
148
    export_tarball(tree, ball, root, subdir, filtered=filtered,
149
                   force_mtime=force_mtime)
150
    ball.close()
151
5718.5.2 by Jelmer Vernooij
Factor out export_tarball.
152
153
def plain_tar_exporter(tree, dest, root, subdir, compression=None,
154
                       filtered=False, force_mtime=None):
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
155
    """Export this tree to a new tar file.
156
157
    `dest` will be created holding the contents of this tree; if it
158
    already exists, it will be clobbered, like with "tar -c".
159
    """
160
    if dest == '-':
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
161
        stream = sys.stdout
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
162
    else:
5724.1.3 by John Arbash Meinel
Change the exporters to ensure that we are writing the data out in binary mode.
163
        stream = open(dest, 'wb')
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
164
    ball = tarfile.open(None, 'w|', stream)
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
165
    export_tarball(tree, ball, root, subdir, filtered=filtered,
166
                   force_mtime=force_mtime)
167
    ball.close()
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
168
169
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
170
def tar_xz_exporter(tree, dest, root, subdir, filtered=False,
171
                    force_mtime=None):
172
    return tar_lzma_exporter(tree, dest, root, subdir, filtered=filtered,
173
        force_mtime=force_mtime, compression_format="xz")
174
175
5718.5.21 by Jelmer Vernooij
Remove assertion - lzma already checks that the format is supported.
176
def tar_lzma_exporter(tree, dest, root, subdir, filtered=False, force_mtime=None, compression_format="alone"):
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
177
    """Export this tree to a new .tar.lzma file.
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
178
179
    `dest` will be created holding the contents of this tree; if it
180
    already exists, it will be clobbered, like with "tar -c".
181
    """
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
182
    if dest == '-':
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
183
        raise errors.BzrError("Writing to stdout not supported for .tar.lzma")
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
184
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
185
    try:
186
        import lzma
187
    except ImportError, e:
188
        raise errors.DependencyNotPresent('lzma', e)
189
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
190
    stream = lzma.LZMAFile(dest.encode(osutils._fs_enc), 'w',
191
            options={"format": compression_format})
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
192
    ball = tarfile.open(None, 'w:', fileobj=stream)
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
193
    export_tarball(tree, ball, root, subdir, filtered=filtered,
194
                   force_mtime=force_mtime)
195
    ball.close()
196