/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/export/tar_exporter.py

merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Export a Tree to a non-versioned directory.
18
18
"""
19
19
 
20
20
import os
 
21
import StringIO
 
22
import sys
 
23
import tarfile
 
24
import time
 
25
 
 
26
from bzrlib import errors, export, osutils
 
27
from bzrlib.export import _export_iter_entries
 
28
from bzrlib.filters import (
 
29
    ContentFilterContext,
 
30
    filtered_output_bytes,
 
31
    )
21
32
from bzrlib.trace import mutter
22
 
import tarfile
23
 
from bzrlib import errors, export
24
 
 
25
 
 
26
 
def tar_exporter(tree, dest, root, compression=None):
 
33
 
 
34
 
 
35
def tar_exporter(tree, dest, root, subdir, compression=None, filtered=False):
27
36
    """Export this tree to a new tar file.
28
37
 
29
38
    `dest` will be created holding the contents of this tree; if it
30
39
    already exists, it will be clobbered, like with "tar -c".
31
40
    """
32
 
    from time import time
33
 
    now = time()
 
41
    mutter('export version %r', tree)
 
42
    now = time.time()
34
43
    compression = str(compression or '')
35
 
    if root is None:
36
 
        root = export.get_root_name(dest)
37
 
    ball = tarfile.open(dest, 'w:' + compression)
38
 
    mutter('export version %r', tree)
39
 
    inv = tree.inventory
40
 
    entries = inv.iter_entries()
41
 
    entries.next() # skip root
42
 
    for dp, ie in entries:
43
 
        # .bzrignore has no meaning outside of a working tree
44
 
        # so do not export it
45
 
        if dp == ".bzrignore":
46
 
            continue
47
 
        
48
 
        mutter("  export {%s} kind %s to %s", ie.file_id, ie.kind, dest)
49
 
        item, fileobj = ie.get_tar_item(root, dp, now, tree)
 
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)
 
52
    for dp, ie in _export_iter_entries(tree, subdir):
 
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)
 
65
                context = ContentFilterContext(dp, tree, ie)
 
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))
50
88
        ball.addfile(item, fileobj)
51
89
    ball.close()
52
90
 
53
91
 
54
 
def tgz_exporter(tree, dest, root):
55
 
    tar_exporter(tree, dest, root, compression='gz')
56
 
 
57
 
 
58
 
def tbz_exporter(tree, dest, root):
59
 
    tar_exporter(tree, dest, root, compression='bz2')
60
 
 
 
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)