/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: Robert Collins
  • Date: 2008-08-20 02:07:36 UTC
  • mfrom: (3640 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3682.
  • Revision ID: robertc@robertcollins.net-20080820020736-g2xe4921zzxtymle
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
"""
19
19
 
20
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
21
27
from bzrlib.trace import mutter
22
 
import tarfile
23
 
 
24
 
 
25
 
def tar_exporter(tree, dest, root, compression=None):
 
28
 
 
29
 
 
30
def tar_exporter(tree, dest, root, subdir, compression=None):
26
31
    """Export this tree to a new tar file.
27
32
 
28
33
    `dest` will be created holding the contents of this tree; if it
29
34
    already exists, it will be clobbered, like with "tar -c".
30
35
    """
31
 
    from time import time
32
 
    now = time()
 
36
    mutter('export version %r', tree)
 
37
    now = time.time()
33
38
    compression = str(compression or '')
34
 
    if root is None:
35
 
        root = get_root_name(dest)
36
 
    try:
 
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)
37
46
        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
 
        # .bzrignore has no meaning outside of a working tree
44
 
        # so do not export it
45
 
        if dp == ".bzrignore":
46
 
            continue
47
 
        
48
 
        mutter("  export {%s} kind %s to %s", ie.file_id, ie.kind, dest)
49
 
        item, fileobj = ie.get_tar_item(root, dp, now, tree)
 
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))
50
74
        ball.addfile(item, fileobj)
51
75
    ball.close()
52
76
 
53
77
 
54
 
def tgz_exporter(tree, dest, root):
55
 
    tar_exporter(tree, dest, root, compression='gz')
56
 
 
57
 
 
58
 
def tbz_exporter(tree, dest, root):
59
 
    tar_exporter(tree, dest, root, compression='bz2')
60
 
 
 
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')