/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 breezy/archive/tar.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2008-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Export a tree to a tarball."""
 
18
 
 
19
from contextlib import closing
 
20
from io import BytesIO
 
21
import os
 
22
import sys
 
23
import tarfile
 
24
 
 
25
from .. import (
 
26
    errors,
 
27
    osutils,
 
28
    )
 
29
from ..export import _export_iter_entries
 
30
 
 
31
 
 
32
def prepare_tarball_item(tree, root, final_path, tree_path, entry, force_mtime=None):
 
33
    """Prepare a tarball item for exporting
 
34
 
 
35
    :param tree: Tree to export
 
36
    :param final_path: Final path to place item
 
37
    :param tree_path: Path for the entry in the tree
 
38
    :param entry: Entry to export
 
39
    :param force_mtime: Option mtime to force, instead of using tree
 
40
        timestamps.
 
41
 
 
42
    Returns a (tarinfo, fileobj) tuple
 
43
    """
 
44
    file_id = getattr(entry, 'file_id', None)
 
45
    filename = osutils.pathjoin(root, final_path)
 
46
    item = tarfile.TarInfo(filename)
 
47
    if force_mtime is not None:
 
48
        item.mtime = force_mtime
 
49
    else:
 
50
        item.mtime = tree.get_file_mtime(tree_path)
 
51
    if entry.kind == "file":
 
52
        item.type = tarfile.REGTYPE
 
53
        if tree.is_executable(tree_path):
 
54
            item.mode = 0o755
 
55
        else:
 
56
            item.mode = 0o644
 
57
        # This brings the whole file into memory, but that's almost needed for
 
58
        # the tarfile contract, which wants the size of the file up front.  We
 
59
        # want to make sure it doesn't change, and we need to read it in one
 
60
        # go for content filtering.
 
61
        content = tree.get_file_text(tree_path)
 
62
        item.size = len(content)
 
63
        fileobj = BytesIO(content)
 
64
    elif entry.kind in ("directory", "tree-reference"):
 
65
        item.type = tarfile.DIRTYPE
 
66
        item.name += '/'
 
67
        item.size = 0
 
68
        item.mode = 0o755
 
69
        fileobj = None
 
70
    elif entry.kind == "symlink":
 
71
        item.type = tarfile.SYMTYPE
 
72
        item.size = 0
 
73
        item.mode = 0o755
 
74
        item.linkname = tree.get_symlink_target(tree_path)
 
75
        fileobj = None
 
76
    else:
 
77
        raise errors.BzrError("don't know how to export {%s} of kind %r"
 
78
                              % (file_id, entry.kind))
 
79
    return (item, fileobj)
 
80
 
 
81
 
 
82
def tarball_generator(tree, root, subdir=None, force_mtime=None, format=''):
 
83
    """Export tree contents to a tarball.
 
84
 
 
85
    :returns: A generator that will produce file content chunks.
 
86
 
 
87
    :param tree: Tree to export
 
88
 
 
89
    :param subdir: Sub directory to export
 
90
 
 
91
    :param force_mtime: Option mtime to force, instead of using tree
 
92
        timestamps.
 
93
    """
 
94
    buf = BytesIO()
 
95
    with closing(tarfile.open(None, "w:%s" % format, buf)) as ball, tree.lock_read():
 
96
        for final_path, tree_path, entry in _export_iter_entries(tree, subdir):
 
97
            (item, fileobj) = prepare_tarball_item(
 
98
                tree, root, final_path, tree_path, entry, force_mtime)
 
99
            ball.addfile(item, fileobj)
 
100
            # Yield the data that was written so far, rinse, repeat.
 
101
            yield buf.getvalue()
 
102
            buf.truncate(0)
 
103
            buf.seek(0)
 
104
    yield buf.getvalue()
 
105
 
 
106
 
 
107
def tgz_generator(tree, dest, root, subdir, force_mtime=None):
 
108
    """Export this tree to a new tar file.
 
109
 
 
110
    `dest` will be created holding the contents of this tree; if it
 
111
    already exists, it will be clobbered, like with "tar -c".
 
112
    """
 
113
    with tree.lock_read():
 
114
        import gzip
 
115
        if force_mtime is not None:
 
116
            root_mtime = force_mtime
 
117
        elif (getattr(tree, "repository", None) and
 
118
              getattr(tree, "get_revision_id", None)):
 
119
            # If this is a revision tree, use the revisions' timestamp
 
120
            rev = tree.repository.get_revision(tree.get_revision_id())
 
121
            root_mtime = rev.timestamp
 
122
        elif tree.is_versioned(u''):
 
123
            root_mtime = tree.get_file_mtime('')
 
124
        else:
 
125
            root_mtime = None
 
126
 
 
127
        is_stdout = False
 
128
        basename = None
 
129
        # gzip file is used with an explicit fileobj so that
 
130
        # the basename can be stored in the gzip file rather than
 
131
        # dest. (bug 102234)
 
132
        basename = os.path.basename(dest)
 
133
        buf = BytesIO()
 
134
        zipstream = gzip.GzipFile(basename, 'w', fileobj=buf,
 
135
                                  mtime=root_mtime)
 
136
        for chunk in tarball_generator(tree, root, subdir, force_mtime):
 
137
            zipstream.write(chunk)
 
138
            # Yield the data that was written so far, rinse, repeat.
 
139
            yield buf.getvalue()
 
140
            buf.truncate(0)
 
141
            buf.seek(0)
 
142
        # Closing zipstream may trigger writes to stream
 
143
        zipstream.close()
 
144
        yield buf.getvalue()
 
145
 
 
146
 
 
147
def tbz_generator(tree, dest, root, subdir, force_mtime=None):
 
148
    """Export this tree to a new tar file.
 
149
 
 
150
    `dest` will be created holding the contents of this tree; if it
 
151
    already exists, it will be clobbered, like with "tar -c".
 
152
    """
 
153
    return tarball_generator(
 
154
        tree, root, subdir, force_mtime, format='bz2')
 
155
 
 
156
 
 
157
def plain_tar_generator(tree, dest, root, subdir,
 
158
                        force_mtime=None):
 
159
    """Export this tree to a new tar file.
 
160
 
 
161
    `dest` will be created holding the contents of this tree; if it
 
162
    already exists, it will be clobbered, like with "tar -c".
 
163
    """
 
164
    return tarball_generator(
 
165
        tree, root, subdir, force_mtime, format='')
 
166
 
 
167
 
 
168
def tar_xz_generator(tree, dest, root, subdir, force_mtime=None):
 
169
    return tar_lzma_generator(tree, dest, root, subdir, force_mtime, "xz")
 
170
 
 
171
 
 
172
def tar_lzma_generator(tree, dest, root, subdir, force_mtime=None,
 
173
                       compression_format="alone"):
 
174
    """Export this tree to a new .tar.lzma file.
 
175
 
 
176
    `dest` will be created holding the contents of this tree; if it
 
177
    already exists, it will be clobbered, like with "tar -c".
 
178
    """
 
179
    try:
 
180
        import lzma
 
181
    except ImportError as e:
 
182
        raise errors.DependencyNotPresent('lzma', e)
 
183
 
 
184
    compressor = lzma.LZMACompressor(
 
185
        format={
 
186
            'xz': lzma.FORMAT_XZ,
 
187
            'raw': lzma.FORMAT_RAW,
 
188
            'alone': lzma.FORMAT_ALONE,
 
189
            }[compression_format])
 
190
 
 
191
    for chunk in tarball_generator(
 
192
            tree, root, subdir, force_mtime=force_mtime):
 
193
        yield compressor.compress(chunk)
 
194
 
 
195
    yield compressor.flush()