1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
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.
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.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Export a Tree to a non-versioned directory.
25
from bzrlib import errors, export, osutils
26
from bzrlib.export import _export_iter_entries
27
from bzrlib.trace import mutter
30
def tar_exporter(tree, dest, root, subdir, compression=None):
31
"""Export this tree to a new tar file.
33
`dest` will be created holding the contents of this tree; if it
34
already exists, it will be clobbered, like with "tar -c".
36
mutter('export version %r', tree)
38
compression = str(compression or '')
40
# XXX: If no root is given, the output tarball will contain files
41
# named '-/foo'; perhaps this is the most reasonable thing.
42
ball = tarfile.open(None, 'w|' + compression, sys.stdout)
45
root = export.get_root_name(dest)
46
ball = tarfile.open(dest, 'w:' + compression)
47
for dp, ie in _export_iter_entries(tree, subdir):
48
filename = osutils.pathjoin(root, dp).encode('utf8')
49
item = tarfile.TarInfo(filename)
52
item.type = tarfile.REGTYPE
53
if tree.is_executable(ie.file_id):
57
item.size = ie.text_size
58
fileobj = tree.get_file(ie.file_id)
59
elif ie.kind == "directory":
60
item.type = tarfile.DIRTYPE
65
elif ie.kind == "symlink":
66
item.type = tarfile.SYMTYPE
69
item.linkname = ie.symlink_target
72
raise BzrError("don't know how to export {%s} of kind %r" %
73
(ie.file_id, ie.kind))
74
ball.addfile(item, fileobj)
78
def tgz_exporter(tree, dest, root, subdir):
79
tar_exporter(tree, dest, root, subdir, compression='gz')
82
def tbz_exporter(tree, dest, root, subdir):
83
tar_exporter(tree, dest, root, subdir, compression='bz2')