1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
from bzrlib import errors, export, osutils
26
from bzrlib.export import _export_iter_entries
21
27
from bzrlib.trace import mutter
25
def tar_exporter(tree, dest, root, compression=None):
30
def tar_exporter(tree, dest, root, subdir, compression=None):
26
31
"""Export this tree to a new tar file.
28
33
`dest` will be created holding the contents of this tree; if it
29
34
already exists, it will be clobbered, like with "tar -c".
36
mutter('export version %r', tree)
33
38
compression = str(compression or '')
35
root = get_root_name(dest)
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)
37
46
ball = tarfile.open(dest, 'w:' + compression)
38
except tarfile.CompressionError, e:
39
raise BzrError(str(e))
40
mutter('export version %r', tree)
42
for dp, ie in inv.iter_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)
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))
50
74
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')
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')