1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 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
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.
25
from bzrlib import errors, export, osutils
25
from bzrlib import export, osutils
26
26
from bzrlib.export import _export_iter_entries
27
from bzrlib.filters import (
29
filtered_output_bytes,
27
31
from bzrlib.trace import mutter
30
def tar_exporter(tree, dest, root, subdir, compression=None):
34
def tar_exporter(tree, dest, root, subdir, compression=None, filtered=False,
35
per_file_timestamps=False):
31
36
"""Export this tree to a new tar file.
33
38
`dest` will be created holding the contents of this tree; if it
45
50
root = export.get_root_name(dest)
46
ball = tarfile.open(dest, 'w:' + compression)
52
# tarfile.open goes on to do 'os.getcwd() + dest' for opening
53
# the tar file. With dest being unicode, this throws UnicodeDecodeError
54
# unless we encode dest before passing it on. This works around
55
# upstream python bug http://bugs.python.org/issue8396
56
# (fixed in Python 2.6.5 and 2.7b1)
57
ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:' + compression)
47
59
for dp, ie in _export_iter_entries(tree, subdir):
48
60
filename = osutils.pathjoin(root, dp).encode('utf8')
49
61
item = tarfile.TarInfo(filename)
62
if per_file_timestamps:
63
item.mtime = tree.get_file_mtime(ie.file_id, dp)
51
66
if ie.kind == "file":
52
67
item.type = tarfile.REGTYPE
53
68
if tree.is_executable(ie.file_id):
57
item.size = ie.text_size
58
fileobj = tree.get_file(ie.file_id)
73
chunks = tree.get_file_lines(ie.file_id)
74
filters = tree._content_filter_stack(dp)
75
context = ContentFilterContext(dp, tree, ie)
76
contents = filtered_output_bytes(chunks, filters, context)
77
content = ''.join(contents)
78
item.size = len(content)
79
fileobj = StringIO.StringIO(content)
81
item.size = ie.text_size
82
fileobj = tree.get_file(ie.file_id)
59
83
elif ie.kind == "directory":
60
84
item.type = tarfile.DIRTYPE
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')
102
def tgz_exporter(tree, dest, root, subdir, filtered=False,
103
per_file_timestamps=False):
104
tar_exporter(tree, dest, root, subdir, compression='gz',
105
filtered=filtered, per_file_timestamps=per_file_timestamps)
108
def tbz_exporter(tree, dest, root, subdir, filtered=False,
109
per_file_timestamps=False):
110
tar_exporter(tree, dest, root, subdir, compression='bz2',
111
filtered=filtered, per_file_timestamps=per_file_timestamps)