/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
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
107
    if dest == '-':
5718.5.22 by Jelmer Vernooij
Cope with mtime not always being available.
108
        basename = None
109
        stream = sys.stdout
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
110
    else:
5724.1.4 by John Arbash Meinel
actually make the plain_tar export work again.
111
        stream = open(dest, 'wb')
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
112
        # gzip file is used with an explicit fileobj so that
113
        # the basename can be stored in the gzip file rather than
114
        # dest. (bug 102234)
5718.5.22 by Jelmer Vernooij
Cope with mtime not always being available.
115
        basename = os.path.basename(dest)
116
    try:
117
        stream = gzip.GzipFile(basename, 'w', fileobj=stream, mtime=root_mtime)
118
    except TypeError:
119
        # Python < 2.7 doesn't support the mtime argument
120
        stream = gzip.GzipFile(basename, 'w', fileobj=stream)
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
121
    ball = tarfile.open(None, 'w|', fileobj=stream)
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
122
    export_tarball(tree, ball, root, subdir, filtered=filtered,
123
                   force_mtime=force_mtime)
124
    ball.close()
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
125
126
127
def tbz_exporter(tree, dest, root, subdir, filtered=False, force_mtime=None):
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
128
    """Export this tree to a new tar file.
129
130
    `dest` will be created holding the contents of this tree; if it
131
    already exists, it will be clobbered, like with "tar -c".
132
    """
133
    if dest == '-':
134
        ball = tarfile.open(None, 'w|bz2', sys.stdout)
135
    else:
136
        # tarfile.open goes on to do 'os.getcwd() + dest' for opening
137
        # the tar file. With dest being unicode, this throws UnicodeDecodeError
138
        # unless we encode dest before passing it on. This works around
139
        # upstream python bug http://bugs.python.org/issue8396
140
        # (fixed in Python 2.6.5 and 2.7b1)
141
        ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:bz2')
142
    export_tarball(tree, ball, root, subdir, filtered=filtered,
143
                   force_mtime=force_mtime)
144
    ball.close()
145
5718.5.2 by Jelmer Vernooij
Factor out export_tarball.
146
147
def plain_tar_exporter(tree, dest, root, subdir, compression=None,
148
                       filtered=False, force_mtime=None):
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
149
    """Export this tree to a new tar file.
150
151
    `dest` will be created holding the contents of this tree; if it
152
    already exists, it will be clobbered, like with "tar -c".
153
    """
154
    if dest == '-':
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
155
        stream = sys.stdout
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
156
    else:
5724.1.3 by John Arbash Meinel
Change the exporters to ensure that we are writing the data out in binary mode.
157
        stream = open(dest, 'wb')
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
158
    ball = tarfile.open(None, 'w|', stream)
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
159
    export_tarball(tree, ball, root, subdir, filtered=filtered,
160
                   force_mtime=force_mtime)
161
    ball.close()
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
162
163
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
164
def tar_xz_exporter(tree, dest, root, subdir, filtered=False,
165
                    force_mtime=None):
166
    return tar_lzma_exporter(tree, dest, root, subdir, filtered=filtered,
167
        force_mtime=force_mtime, compression_format="xz")
168
169
5718.5.21 by Jelmer Vernooij
Remove assertion - lzma already checks that the format is supported.
170
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.
171
    """Export this tree to a new .tar.lzma file.
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
172
173
    `dest` will be created holding the contents of this tree; if it
174
    already exists, it will be clobbered, like with "tar -c".
175
    """
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
176
    if dest == '-':
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
177
        raise errors.BzrError("Writing to stdout not supported for .tar.lzma")
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
178
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
179
    try:
180
        import lzma
181
    except ImportError, e:
182
        raise errors.DependencyNotPresent('lzma', e)
183
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
184
    stream = lzma.LZMAFile(dest.encode(osutils._fs_enc), 'w',
185
            options={"format": compression_format})
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
186
    ball = tarfile.open(None, 'w:', fileobj=stream)
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
187
    export_tarball(tree, ball, root, subdir, filtered=filtered,
188
                   force_mtime=force_mtime)
189
    ball.close()
190