/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
16
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
17
"""Export a Tree to a zip file.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
18
"""
19
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
20
from __future__ import absolute_import
21
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
22
from contextlib import closing
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
23
import os
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
24
import stat
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
25
import sys
6968.2.9 by Jelmer Vernooij
Simplify export module.
26
import tempfile
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
27
import time
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
28
import zipfile
29
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
30
from .. import (
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
31
    osutils,
32
    )
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
33
from ..export import _export_iter_entries
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
34
from ..trace import mutter
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
35
36
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
37
# Windows expects this bit to be set in the 'external_attr' section,
38
# or it won't consider the entry a directory.
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
39
ZIP_DIRECTORY_BIT = (1 << 4)
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
40
FILE_PERMISSIONS = (0o644 << 16)
41
DIR_PERMISSIONS = (0o755 << 16)
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
42
4823.1.2 by Ivan Sagalaev
File mode is given a name and gets tested
43
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
5664.2.1 by Jelmer Vernooij
Fix setting of mode on directories in zip files.
44
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
45
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
46
6968.2.6 by Jelmer Vernooij
Move tar/zip to breezy.archive.
47
def zip_archive_generator(tree, dest, root, subdir=None,
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
48
    force_mtime=None):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
49
    """ Export this tree to a new zip file.
50
51
    `dest` will be created holding the contents of this tree; if it
52
    already exists, it will be overwritten".
53
    """
54
    compression = zipfile.ZIP_DEFLATED
6968.2.9 by Jelmer Vernooij
Simplify export module.
55
    with tempfile.SpooledTemporaryFile() as buf:
56
        with closing(zipfile.ZipFile(buf, "w", compression)) as zipf, \
57
             tree.lock_read():
58
            for dp, tp, ie in _export_iter_entries(tree, subdir):
6973.3.4 by Jelmer Vernooij
Don't assume entries returned by trees have a 'file_id' attribute.
59
                file_id = getattr(ie, 'file_id', None)
6968.2.9 by Jelmer Vernooij
Simplify export module.
60
                mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
61
6968.2.9 by Jelmer Vernooij
Simplify export module.
62
                # zipfile.ZipFile switches all paths to forward
63
                # slashes anyway, so just stick with that.
64
                if force_mtime is not None:
65
                    mtime = force_mtime
66
                else:
6973.3.4 by Jelmer Vernooij
Don't assume entries returned by trees have a 'file_id' attribute.
67
                    mtime = tree.get_file_mtime(tp, file_id)
6968.2.9 by Jelmer Vernooij
Simplify export module.
68
                date_time = time.localtime(mtime)[:6]
69
                filename = osutils.pathjoin(root, dp).encode('utf8')
70
                if ie.kind == "file":
71
                    zinfo = zipfile.ZipInfo(
72
                                filename=filename,
73
                                date_time=date_time)
74
                    zinfo.compress_type = compression
75
                    zinfo.external_attr = _FILE_ATTR
76
                    content = tree.get_file_text(tp, file_id)
77
                    zipf.writestr(zinfo, content)
78
                elif ie.kind in ("directory", "tree-reference"):
79
                    # Directories must contain a trailing slash, to indicate
80
                    # to the zip routine that they are really directories and
81
                    # not just empty files.
82
                    zinfo = zipfile.ZipInfo(
83
                                filename=filename + '/',
84
                                date_time=date_time)
85
                    zinfo.compress_type = compression
86
                    zinfo.external_attr = _DIR_ATTR
87
                    zipf.writestr(zinfo, '')
88
                elif ie.kind == "symlink":
89
                    zinfo = zipfile.ZipInfo(
90
                                filename=(filename + '.lnk'),
91
                                date_time=date_time)
92
                    zinfo.compress_type = compression
93
                    zinfo.external_attr = _FILE_ATTR
94
                    zipf.writestr(zinfo, tree.get_symlink_target(tp, file_id))
95
        # Urgh, headers are written last since they include e.g. file size.
96
        # So we have to buffer it all :(
97
        buf.seek(0)
6968.2.10 by Jelmer Vernooij
Review comments.
98
        for chunk in osutils.file_iterator(buf):
6968.2.9 by Jelmer Vernooij
Simplify export module.
99
            yield chunk