~bzr-pqm/bzr/bzr.dev

4597.9.2 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 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
3368.2.32 by Ian Clatworthy
add --filters to export command
20
import StringIO
21
import sys
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
22
import tarfile
3408.7.1 by Martin Pool
Support tarball export to stdout
23
import time
24
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
25
from bzrlib import export, osutils
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
26
from bzrlib.export import _export_iter_entries
3368.2.32 by Ian Clatworthy
add --filters to export command
27
from bzrlib.filters import (
28
    ContentFilterContext,
29
    filtered_output_bytes,
30
    )
3408.7.1 by Martin Pool
Support tarball export to stdout
31
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.
32
33
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
34
def tar_exporter(tree, dest, root, subdir, compression=None, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
35
                 per_file_timestamps=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)
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
55
        if per_file_timestamps:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
56
            item.mtime = tree.get_file_mtime(ie.file_id, dp)
57
        else:
58
            item.mtime = now
3368.2.32 by Ian Clatworthy
add --filters to export command
59
        if ie.kind == "file":
60
            item.type = tarfile.REGTYPE
61
            if tree.is_executable(ie.file_id):
62
                item.mode = 0755
63
            else:
64
                item.mode = 0644
65
            if filtered:
66
                chunks = tree.get_file_lines(ie.file_id)
67
                filters = tree._content_filter_stack(dp)
3368.2.33 by Ian Clatworthy
expand filter context to support interesting stuff
68
                context = ContentFilterContext(dp, tree, ie)
3368.2.32 by Ian Clatworthy
add --filters to export command
69
                contents = filtered_output_bytes(chunks, filters, context)
70
                content = ''.join(contents)
71
                item.size = len(content)
72
                fileobj = StringIO.StringIO(content)
73
            else:
74
                item.size = ie.text_size
75
                fileobj = tree.get_file(ie.file_id)
76
        elif ie.kind == "directory":
77
            item.type = tarfile.DIRTYPE
78
            item.name += '/'
79
            item.size = 0
80
            item.mode = 0755
81
            fileobj = None
82
        elif ie.kind == "symlink":
83
            item.type = tarfile.SYMTYPE
84
            item.size = 0
85
            item.mode = 0755
86
            item.linkname = ie.symlink_target
87
            fileobj = None
88
        else:
89
            raise BzrError("don't know how to export {%s} of kind %r" %
90
                           (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.
91
        ball.addfile(item, fileobj)
92
    ball.close()
93
94
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
95
def tgz_exporter(tree, dest, root, subdir, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
96
                 per_file_timestamps=False):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
97
    tar_exporter(tree, dest, root, subdir, compression='gz',
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
98
                 filtered=filtered, per_file_timestamps=per_file_timestamps)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
99
100
101
def tbz_exporter(tree, dest, root, subdir, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
102
                 per_file_timestamps=False):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
103
    tar_exporter(tree, dest, root, subdir, compression='bz2',
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
104
                 filtered=filtered, per_file_timestamps=per_file_timestamps)