/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3408.7.1 by Martin Pool
Support tarball export to stdout
1
# Copyright (C) 2005, 2006, 2008 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
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
import time
25
3368.2.32 by Ian Clatworthy
add --filters to export command
26
from bzrlib import errors, export, osutils
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
27
from bzrlib.export import _export_iter_entries
3368.2.32 by Ian Clatworthy
add --filters to export command
28
from bzrlib.filters import (
29
    ContentFilterContext,
30
    filtered_output_bytes,
31
    )
3408.7.1 by Martin Pool
Support tarball export to stdout
32
from bzrlib.trace import mutter
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
33
34
3368.2.41 by Ian Clatworthy
1st cut merge of bzr.dev r3907
35
def tar_exporter(tree, dest, root, subdir, compression=None, filtered=False):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
36
    """Export this tree to a new tar file.
37
38
    `dest` will be created holding the contents of this tree; if it
39
    already exists, it will be clobbered, like with "tar -c".
40
    """
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
41
    mutter('export version %r', tree)
3408.7.1 by Martin Pool
Support tarball export to stdout
42
    now = time.time()
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
43
    compression = str(compression or '')
3408.7.1 by Martin Pool
Support tarball export to stdout
44
    if dest == '-':
45
        # XXX: If no root is given, the output tarball will contain files
46
        # named '-/foo'; perhaps this is the most reasonable thing.
47
        ball = tarfile.open(None, 'w|' + compression, sys.stdout)
48
    else:
49
        if root is None:
50
            root = export.get_root_name(dest)
51
        ball = tarfile.open(dest, 'w:' + compression)
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
52
    for dp, ie in _export_iter_entries(tree, subdir):
3368.2.32 by Ian Clatworthy
add --filters to export command
53
        filename = osutils.pathjoin(root, dp).encode('utf8')
54
        item = tarfile.TarInfo(filename)
55
        item.mtime = now
56
        if ie.kind == "file":
57
            item.type = tarfile.REGTYPE
58
            if tree.is_executable(ie.file_id):
59
                item.mode = 0755
60
            else:
61
                item.mode = 0644
62
            if filtered:
63
                chunks = tree.get_file_lines(ie.file_id)
64
                filters = tree._content_filter_stack(dp)
3368.2.33 by Ian Clatworthy
expand filter context to support interesting stuff
65
                context = ContentFilterContext(dp, tree, ie)
3368.2.32 by Ian Clatworthy
add --filters to export command
66
                contents = filtered_output_bytes(chunks, filters, context)
67
                content = ''.join(contents)
68
                item.size = len(content)
69
                fileobj = StringIO.StringIO(content)
70
            else:
71
                item.size = ie.text_size
72
                fileobj = tree.get_file(ie.file_id)
73
        elif ie.kind == "directory":
74
            item.type = tarfile.DIRTYPE
75
            item.name += '/'
76
            item.size = 0
77
            item.mode = 0755
78
            fileobj = None
79
        elif ie.kind == "symlink":
80
            item.type = tarfile.SYMTYPE
81
            item.size = 0
82
            item.mode = 0755
83
            item.linkname = ie.symlink_target
84
            fileobj = None
85
        else:
86
            raise BzrError("don't know how to export {%s} of kind %r" %
87
                           (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.
88
        ball.addfile(item, fileobj)
89
    ball.close()
90
91
3368.2.41 by Ian Clatworthy
1st cut merge of bzr.dev r3907
92
def tgz_exporter(tree, dest, root, subdir, filtered=False):
93
    tar_exporter(tree, dest, root, subdir, compression='gz', filtered=filtered)
94
95
96
def tbz_exporter(tree, dest, root, subdir, filtered=False):
97
    tar_exporter(tree, dest, root, subdir, compression='bz2', filtered=filtered)