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
17
17
"""Export a Tree to a non-versioned directory.
26
from bzrlib import errors, export, osutils
27
from bzrlib.export import _export_iter_entries
28
from bzrlib.filters import (
30
filtered_output_bytes,
21
32
from bzrlib.trace import mutter
23
from bzrlib import errors, export
26
def tar_exporter(tree, dest, root, compression=None):
35
def tar_exporter(tree, dest, root, subdir, compression=None, filtered=False):
27
36
"""Export this tree to a new tar file.
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".
41
mutter('export version %r', tree)
34
43
compression = str(compression or '')
36
root = export.get_root_name(dest)
37
ball = tarfile.open(dest, 'w:' + compression)
38
mutter('export version %r', tree)
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
45
if dp == ".bzrignore":
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)
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)
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)
57
item.type = tarfile.REGTYPE
58
if tree.is_executable(ie.file_id):
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)
71
item.size = ie.text_size
72
fileobj = tree.get_file(ie.file_id)
73
elif ie.kind == "directory":
74
item.type = tarfile.DIRTYPE
79
elif ie.kind == "symlink":
80
item.type = tarfile.SYMTYPE
83
item.linkname = ie.symlink_target
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)
54
def tgz_exporter(tree, dest, root):
55
tar_exporter(tree, dest, root, compression='gz')
58
def tbz_exporter(tree, dest, root):
59
tar_exporter(tree, dest, root, compression='bz2')
92
def tgz_exporter(tree, dest, root, subdir, filtered=False):
93
tar_exporter(tree, dest, root, subdir, compression='gz', filtered=filtered)
96
def tbz_exporter(tree, dest, root, subdir, filtered=False):
97
tar_exporter(tree, dest, root, subdir, compression='bz2', filtered=filtered)