~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/tar_exporter.py

  • Committer: Ian Clatworthy
  • Date: 2008-07-23 13:29:55 UTC
  • mto: (4171.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 4173.
  • Revision ID: ian.clatworthy@canonical.com-20080723132955-kzxal29bslx51u36
add --filters to export command

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""
19
19
 
20
20
import os
 
21
import StringIO
 
22
import sys
21
23
import tarfile
22
24
import time
23
 
import sys
24
25
 
25
 
from bzrlib import errors, export
 
26
from bzrlib import errors, export, osutils
 
27
from bzrlib.filters import (
 
28
    ContentFilterContext,
 
29
    filtered_output_bytes,
 
30
    )
26
31
from bzrlib.trace import mutter
27
32
 
28
33
 
29
 
def tar_exporter(tree, dest, root, compression=None):
 
34
def tar_exporter(tree, dest, root, compression=None, filtered=False):
30
35
    """Export this tree to a new tar file.
31
36
 
32
37
    `dest` will be created holding the contents of this tree; if it
51
56
        # so do not export it
52
57
        if dp == ".bzrignore":
53
58
            continue
54
 
        item, fileobj = ie.get_tar_item(root, dp, now, tree)
 
59
 
 
60
        filename = osutils.pathjoin(root, dp).encode('utf8')
 
61
        item = tarfile.TarInfo(filename)
 
62
        item.mtime = now
 
63
        if ie.kind == "file":
 
64
            item.type = tarfile.REGTYPE
 
65
            if tree.is_executable(ie.file_id):
 
66
                item.mode = 0755
 
67
            else:
 
68
                item.mode = 0644
 
69
            if filtered:
 
70
                chunks = tree.get_file_lines(ie.file_id)
 
71
                filters = tree._content_filter_stack(dp)
 
72
                context = ContentFilterContext(dp)
 
73
                contents = filtered_output_bytes(chunks, filters, context)
 
74
                content = ''.join(contents)
 
75
                item.size = len(content)
 
76
                fileobj = StringIO.StringIO(content)
 
77
            else:
 
78
                item.size = ie.text_size
 
79
                fileobj = tree.get_file(ie.file_id)
 
80
        elif ie.kind == "directory":
 
81
            item.type = tarfile.DIRTYPE
 
82
            item.name += '/'
 
83
            item.size = 0
 
84
            item.mode = 0755
 
85
            fileobj = None
 
86
        elif ie.kind == "symlink":
 
87
            item.type = tarfile.SYMTYPE
 
88
            item.size = 0
 
89
            item.mode = 0755
 
90
            item.linkname = ie.symlink_target
 
91
            fileobj = None
 
92
        else:
 
93
            raise BzrError("don't know how to export {%s} of kind %r" %
 
94
                           (ie.file_id, ie.kind))
55
95
        ball.addfile(item, fileobj)
56
96
    ball.close()
57
97
 
58
98
 
59
 
def tgz_exporter(tree, dest, root):
60
 
    tar_exporter(tree, dest, root, compression='gz')
61
 
 
62
 
 
63
 
def tbz_exporter(tree, dest, root):
64
 
    tar_exporter(tree, dest, root, compression='bz2')
65
 
 
 
99
def tgz_exporter(tree, dest, root, filtered=False):
 
100
    tar_exporter(tree, dest, root, compression='gz', filtered=filtered)
 
101
 
 
102
 
 
103
def tbz_exporter(tree, dest, root, filtered=False):
 
104
    tar_exporter(tree, dest, root, compression='bz2', filtered=filtered)