/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

Merge with serialize-transform

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Export a Tree to a non-versioned directory.
 
18
"""
 
19
 
 
20
import os
 
21
import sys
 
22
import tarfile
 
23
import time
 
24
 
 
25
from bzrlib import errors, export, osutils
 
26
from bzrlib.export import _export_iter_entries
 
27
from bzrlib.trace import mutter
 
28
 
 
29
 
 
30
def tar_exporter(tree, dest, root, subdir, compression=None):
 
31
    """Export this tree to a new tar file.
 
32
 
 
33
    `dest` will be created holding the contents of this tree; if it
 
34
    already exists, it will be clobbered, like with "tar -c".
 
35
    """
 
36
    mutter('export version %r', tree)
 
37
    now = time.time()
 
38
    compression = str(compression or '')
 
39
    if 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)
 
43
    else:
 
44
        if root is None:
 
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)
 
50
        item.mtime = now
 
51
        if ie.kind == "file":
 
52
            item.type = tarfile.REGTYPE
 
53
            if tree.is_executable(ie.file_id):
 
54
                item.mode = 0755
 
55
            else:
 
56
                item.mode = 0644
 
57
            item.size = ie.text_size
 
58
            fileobj = tree.get_file(ie.file_id)
 
59
        elif ie.kind == "directory":
 
60
            item.type = tarfile.DIRTYPE
 
61
            item.name += '/'
 
62
            item.size = 0
 
63
            item.mode = 0755
 
64
            fileobj = None
 
65
        elif ie.kind == "symlink":
 
66
            item.type = tarfile.SYMTYPE
 
67
            item.size = 0
 
68
            item.mode = 0755
 
69
            item.linkname = ie.symlink_target
 
70
            fileobj = None
 
71
        else:
 
72
            raise BzrError("don't know how to export {%s} of kind %r" %
 
73
                           (ie.file_id, ie.kind))
 
74
        ball.addfile(item, fileobj)
 
75
    ball.close()
 
76
 
 
77
 
 
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')