/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
3408.7.1 by Martin Pool
Support tarball export to stdout
26
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.
27
28
29
def tar_exporter(tree, dest, root, compression=None):
30
    """Export this tree to a new tar file.
31
32
    `dest` will be created holding the contents of this tree; if it
33
    already exists, it will be clobbered, like with "tar -c".
34
    """
3408.7.1 by Martin Pool
Support tarball export to stdout
35
    now = time.time()
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
36
    compression = str(compression or '')
3408.7.1 by Martin Pool
Support tarball export to stdout
37
    if dest == '-':
38
        # XXX: If no root is given, the output tarball will contain files
39
        # named '-/foo'; perhaps this is the most reasonable thing.
40
        ball = tarfile.open(None, 'w|' + compression, sys.stdout)
41
    else:
42
        if root is None:
43
            root = export.get_root_name(dest)
44
        ball = tarfile.open(dest, 'w:' + compression)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
45
    mutter('export version %r', tree)
46
    inv = tree.inventory
1852.6.6 by Robert Collins
Finish updating iter_entries change to make all tests pass.
47
    entries = inv.iter_entries()
48
    entries.next() # skip root
49
    for dp, ie in entries:
3577.3.1 by Ian Clatworthy
do not export .bzrrules
50
        # The .bzr* namespace is reserved for "magic" files like
51
        # .bzrignore and .bzrrules - do not export these
52
        if dp.startswith(".bzr"):
1185.61.3 by Jamie Wilkinson
convert tabs to 4-spaces
53
            continue
3577.2.1 by Ian Clatworthy
deprecate export-related InventoryEntry methods and refactor export code accordingly
54
55
        filename = osutils.pathjoin(root, dp).encode('utf8')
56
        item = tarfile.TarInfo(filename)
57
        item.mtime = now
58
        if ie.kind == "file":
59
            item.type = tarfile.REGTYPE
60
            if tree.is_executable(ie.file_id):
61
                item.mode = 0755
62
            else:
63
                item.mode = 0644
64
            item.size = ie.text_size
65
            fileobj = tree.get_file(ie.file_id)
66
        elif ie.kind == "directory":
67
            item.type = tarfile.DIRTYPE
68
            item.name += '/'
69
            item.size = 0
70
            item.mode = 0755
71
            fileobj = None
72
        elif ie.kind == "symlink":
73
            item.type = tarfile.SYMTYPE
74
            item.size = 0
75
            item.mode = 0755
76
            item.linkname = ie.symlink_target
77
            fileobj = None
78
        else:
79
            raise BzrError("don't know how to export {%s} of kind %r" %
80
                           (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.
81
        ball.addfile(item, fileobj)
82
    ball.close()
83
84
85
def tgz_exporter(tree, dest, root):
86
    tar_exporter(tree, dest, root, compression='gz')
87
88
89
def tbz_exporter(tree, dest, root):
90
    tar_exporter(tree, dest, root, compression='bz2')