~bzr-pqm/bzr/bzr.dev

1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
1
# Copyright (C) 2005 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
from bzrlib.trace import mutter
22
import tarfile
23
24
25
def tar_exporter(tree, dest, root, compression=None):
26
    """Export this tree to a new tar file.
27
28
    `dest` will be created holding the contents of this tree; if it
29
    already exists, it will be clobbered, like with "tar -c".
30
    """
31
    from time import time
32
    now = time()
33
    compression = str(compression or '')
34
    if root is None:
35
        root = get_root_name(dest)
36
    try:
37
        ball = tarfile.open(dest, 'w:' + compression)
38
    except tarfile.CompressionError, e:
39
        raise BzrError(str(e))
40
    mutter('export version %r', tree)
41
    inv = tree.inventory
42
    for dp, ie in inv.iter_entries():
43
        mutter("  export {%s} kind %s to %s", ie.file_id, ie.kind, dest)
44
        item, fileobj = ie.get_tar_item(root, dp, now, tree)
45
        ball.addfile(item, fileobj)
46
    ball.close()
47
48
49
def tgz_exporter(tree, dest, root):
50
    tar_exporter(tree, dest, root, compression='gz')
51
52
53
def tbz_exporter(tree, dest, root):
54
    tar_exporter(tree, dest, root, compression='bz2')
55