~bzr-pqm/bzr/bzr.dev

3408.7.1 by Martin Pool
Support tarball export to stdout
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
2
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
7
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
12
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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
3577.2.1 by Ian Clatworthy
deprecate export-related InventoryEntry methods and refactor export code accordingly
21
import sys
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
22
import tarfile
3408.7.1 by Martin Pool
Support tarball export to stdout
23
import time
24
3577.2.1 by Ian Clatworthy
deprecate export-related InventoryEntry methods and refactor export code accordingly
25
from bzrlib import errors, export, osutils
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
26
from bzrlib.export import _export_iter_entries
3408.7.1 by Martin Pool
Support tarball export to stdout
27
from bzrlib.trace import mutter
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
28
29
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
30
def tar_exporter(tree, dest, root, subdir, compression=None):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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
    """
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
36
    mutter('export version %r', tree)
3408.7.1 by Martin Pool
Support tarball export to stdout
37
    now = time.time()
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
38
    compression = str(compression or '')
3408.7.1 by Martin Pool
Support tarball export to stdout
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)
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
47
    for dp, ie in _export_iter_entries(tree, subdir):
3577.2.1 by Ian Clatworthy
deprecate export-related InventoryEntry methods and refactor export code accordingly
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))
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
74
        ball.addfile(item, fileobj)
75
    ball.close()
76
77
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
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')