~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/tar_exporter.py

  • Committer: Jelmer Vernooij
  • Date: 2011-03-13 19:10:45 UTC
  • mto: This revision was merged to the branch mainline in revision 5724.
  • Revision ID: jelmer@samba.org-20110313191045-3s2f15pi4vgds9as
fix timestamp in tgz files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    ContentFilterContext,
32
32
    filtered_output_bytes,
33
33
    )
34
 
from bzrlib.trace import mutter
35
 
 
36
 
 
37
 
def tar_exporter(tree, dest, root, subdir, compression, filtered=False,
38
 
                 force_mtime=None):
39
 
    """Export this tree to a new tar file.
40
 
 
41
 
    `dest` will be created holding the contents of this tree; if it
42
 
    already exists, it will be clobbered, like with "tar -c".
43
 
    """
44
 
    mutter('export version %r', tree)
45
 
    if dest == '-':
46
 
        # XXX: If no root is given, the output tarball will contain files
47
 
        # named '-/foo'; perhaps this is the most reasonable thing.
48
 
        ball = tarfile.open(None, 'w|' + compression, sys.stdout)
49
 
    else:
50
 
        if root is None:
51
 
            root = export.get_root_name(dest)
52
 
 
53
 
        # tarfile.open goes on to do 'os.getcwd() + dest' for opening
54
 
        # the tar file. With dest being unicode, this throws UnicodeDecodeError
55
 
        # unless we encode dest before passing it on. This works around
56
 
        # upstream python bug http://bugs.python.org/issue8396
57
 
        # (fixed in Python 2.6.5 and 2.7b1)
58
 
        ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:' + compression)
59
 
    export_tarball(tree, ball, root, subdir, filtered=filtered,
60
 
                   force_mtime=force_mtime)
61
 
    ball.close()
62
34
 
63
35
 
64
36
def export_tarball(tree, ball, root, subdir, filtered=False,
115
87
 
116
88
 
117
89
def tgz_exporter(tree, dest, root, subdir, filtered=False, force_mtime=None):
118
 
    tar_exporter(tree, dest, root, subdir, compression='gz',
119
 
                 filtered=filtered, force_mtime=force_mtime)
 
90
    """Export this tree to a new tar file.
 
91
 
 
92
    `dest` will be created holding the contents of this tree; if it
 
93
    already exists, it will be clobbered, like with "tar -c".
 
94
    """
 
95
    import gzip
 
96
    if force_mtime is not None:
 
97
        root_mtime = force_mtime
 
98
    else:
 
99
        # FIXME: Use tree.get_revision_id()'s timestamp ?
 
100
        root_mtime = tree.get_file_mtime(tree.root.id, "")
 
101
    if dest == '-':
 
102
        # XXX: If no root is given, the output tarball will contain files
 
103
        # named '-/foo'; perhaps this is the most reasonable thing.
 
104
        stream = gzip.GzipFile(None, mode='w', mtime=root_mtime,
 
105
            fileobj=sys.stdout)
 
106
    else:
 
107
        if root is None:
 
108
            root = export.get_root_name(dest)
 
109
        stream = gzip.GzipFile(dest.encode(osutils._fs_enc), 'w',
 
110
            mtime=root_mtime)
 
111
    ball = tarfile.open(None, 'w:', fileobj=stream)
 
112
    export_tarball(tree, ball, root, subdir, filtered=filtered,
 
113
                   force_mtime=force_mtime)
 
114
    ball.close()
120
115
 
121
116
 
122
117
def tbz_exporter(tree, dest, root, subdir, filtered=False, force_mtime=None):
123
 
    tar_exporter(tree, dest, root, subdir, compression='bz2',
124
 
                 filtered=filtered, force_mtime=force_mtime)
 
118
    """Export this tree to a new tar file.
 
119
 
 
120
    `dest` will be created holding the contents of this tree; if it
 
121
    already exists, it will be clobbered, like with "tar -c".
 
122
    """
 
123
    if dest == '-':
 
124
        # XXX: If no root is given, the output tarball will contain files
 
125
        # named '-/foo'; perhaps this is the most reasonable thing.
 
126
        ball = tarfile.open(None, 'w|bz2', sys.stdout)
 
127
    else:
 
128
        if root is None:
 
129
            root = export.get_root_name(dest)
 
130
 
 
131
        # tarfile.open goes on to do 'os.getcwd() + dest' for opening
 
132
        # the tar file. With dest being unicode, this throws UnicodeDecodeError
 
133
        # unless we encode dest before passing it on. This works around
 
134
        # upstream python bug http://bugs.python.org/issue8396
 
135
        # (fixed in Python 2.6.5 and 2.7b1)
 
136
        ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:bz2')
 
137
    export_tarball(tree, ball, root, subdir, filtered=filtered,
 
138
                   force_mtime=force_mtime)
 
139
    ball.close()
 
140
 
125
141
 
126
142
def plain_tar_exporter(tree, dest, root, subdir, compression=None,
127
143
                       filtered=False, force_mtime=None):
128
 
    tar_exporter(tree, dest, root, subdir, compression='',
129
 
                 filtered=filtered, force_mtime=force_mtime)
 
144
    """Export this tree to a new tar file.
 
145
 
 
146
    `dest` will be created holding the contents of this tree; if it
 
147
    already exists, it will be clobbered, like with "tar -c".
 
148
    """
 
149
    if dest == '-':
 
150
        # XXX: If no root is given, the output tarball will contain files
 
151
        # named '-/foo'; perhaps this is the most reasonable thing.
 
152
        ball = tarfile.open(None, 'w|', sys.stdout)
 
153
    else:
 
154
        if root is None:
 
155
            root = export.get_root_name(dest)
 
156
 
 
157
        # tarfile.open goes on to do 'os.getcwd() + dest' for opening
 
158
        # the tar file. With dest being unicode, this throws UnicodeDecodeError
 
159
        # unless we encode dest before passing it on. This works around
 
160
        # upstream python bug http://bugs.python.org/issue8396
 
161
        # (fixed in Python 2.6.5 and 2.7b1)
 
162
        ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:')
 
163
    export_tarball(tree, ball, root, subdir, filtered=filtered,
 
164
                   force_mtime=force_mtime)
 
165
    ball.close()