~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)
5152.1.1 by Parth Malwankar
Fixed bug #413406. unicode names for parent directory
51
52
        # tarfile.open goes on to do 'os.getcwd() + dest' for opening
5152.1.4 by Parth Malwankar
improved bug description.
53
        # the tar file. With dest being unicode, this throws UnicodeDecodeError
54
        # unless we encode dest before passing it on. This works around
55
        # upstream python bug http://bugs.python.org/issue8396
56
        # (fixed in Python 2.6.5 and 2.7b1)
5152.1.1 by Parth Malwankar
Fixed bug #413406. unicode names for parent directory
57
        ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:' + compression)
58
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
59
    for dp, ie in _export_iter_entries(tree, subdir):
3368.2.32 by Ian Clatworthy
add --filters to export command
60
        filename = osutils.pathjoin(root, dp).encode('utf8')
61
        item = tarfile.TarInfo(filename)
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
62
        if per_file_timestamps:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
63
            item.mtime = tree.get_file_mtime(ie.file_id, dp)
64
        else:
65
            item.mtime = now
3368.2.32 by Ian Clatworthy
add --filters to export command
66
        if ie.kind == "file":
67
            item.type = tarfile.REGTYPE
68
            if tree.is_executable(ie.file_id):
69
                item.mode = 0755
70
            else:
71
                item.mode = 0644
72
            if filtered:
73
                chunks = tree.get_file_lines(ie.file_id)
74
                filters = tree._content_filter_stack(dp)
3368.2.33 by Ian Clatworthy
expand filter context to support interesting stuff
75
                context = ContentFilterContext(dp, tree, ie)
3368.2.32 by Ian Clatworthy
add --filters to export command
76
                contents = filtered_output_bytes(chunks, filters, context)
77
                content = ''.join(contents)
78
                item.size = len(content)
79
                fileobj = StringIO.StringIO(content)
80
            else:
81
                item.size = ie.text_size
82
                fileobj = tree.get_file(ie.file_id)
83
        elif ie.kind == "directory":
84
            item.type = tarfile.DIRTYPE
85
            item.name += '/'
86
            item.size = 0
87
            item.mode = 0755
88
            fileobj = None
89
        elif ie.kind == "symlink":
90
            item.type = tarfile.SYMTYPE
91
            item.size = 0
92
            item.mode = 0755
93
            item.linkname = ie.symlink_target
94
            fileobj = None
95
        else:
96
            raise BzrError("don't know how to export {%s} of kind %r" %
97
                           (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.
98
        ball.addfile(item, fileobj)
99
    ball.close()
100
101
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
102
def tgz_exporter(tree, dest, root, subdir, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
103
                 per_file_timestamps=False):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
104
    tar_exporter(tree, dest, root, subdir, compression='gz',
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
105
                 filtered=filtered, per_file_timestamps=per_file_timestamps)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
106
107
108
def tbz_exporter(tree, dest, root, subdir, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
109
                 per_file_timestamps=False):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
110
    tar_exporter(tree, dest, root, subdir, compression='bz2',
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
111
                 filtered=filtered, per_file_timestamps=per_file_timestamps)