/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/export/tar_exporter.py

  • Committer: Martin von Gagern
  • Date: 2010-04-20 08:47:38 UTC
  • mfrom: (5167 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5195.
  • Revision ID: martin.vgagern@gmx.net-20100420084738-ygymnqmdllzrhpfn
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
2
2
#
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
12
12
#
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
16
16
 
17
17
"""Export a Tree to a non-versioned directory.
18
18
"""
19
19
 
20
 
import os
 
20
import StringIO
21
21
import sys
22
22
import tarfile
23
23
import time
24
24
 
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 (
 
28
    ContentFilterContext,
 
29
    filtered_output_bytes,
 
30
    )
27
31
from bzrlib.trace import mutter
28
32
 
29
33
 
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.
32
37
 
33
38
    `dest` will be created holding the contents of this tree; if it
43
48
    else:
44
49
        if root is None:
45
50
            root = export.get_root_name(dest)
46
 
        ball = tarfile.open(dest, 'w:' + compression)
 
51
 
 
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)
 
58
 
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)
50
 
        item.mtime = now
 
62
        if per_file_timestamps:
 
63
            item.mtime = tree.get_file_mtime(ie.file_id, dp)
 
64
        else:
 
65
            item.mtime = now
51
66
        if ie.kind == "file":
52
67
            item.type = tarfile.REGTYPE
53
68
            if tree.is_executable(ie.file_id):
54
69
                item.mode = 0755
55
70
            else:
56
71
                item.mode = 0644
57
 
            item.size = ie.text_size
58
 
            fileobj = tree.get_file(ie.file_id)
 
72
            if filtered:
 
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)
 
80
            else:
 
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
61
85
            item.name += '/'
75
99
    ball.close()
76
100
 
77
101
 
78
 
def tgz_exporter(tree, dest, root, subdir):
79
 
    tar_exporter(tree, dest, root, subdir, compression='gz')
80
 
 
81
 
 
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)
 
106
 
 
107
 
 
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)