/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/zip_exporter.py

  • Committer: Martin Pool
  • Date: 2009-12-14 06:06:59 UTC
  • mfrom: (4889 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4891.
  • Revision ID: mbp@sourcefrog.net-20091214060659-1ucv8hpnky0cbnaj
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Export a Tree to a non-versioned directory.
18
18
"""
19
19
 
20
20
import os
21
21
import stat
 
22
import time
22
23
import zipfile
23
24
 
24
25
from bzrlib import (
25
26
    osutils,
26
27
    )
 
28
from bzrlib.export import _export_iter_entries
 
29
from bzrlib.filters import (
 
30
    ContentFilterContext,
 
31
    filtered_output_bytes,
 
32
    )
27
33
from bzrlib.trace import mutter
28
34
 
29
35
 
30
36
# Windows expects this bit to be set in the 'external_attr' section
31
37
# Or it won't consider the entry a directory
32
38
ZIP_DIRECTORY_BIT = (1 << 4)
 
39
FILE_PERMISSIONS = (0644 << 16)
33
40
 
34
 
_FILE_ATTR = stat.S_IFREG
 
41
_FILE_ATTR = stat.S_IFREG | FILE_PERMISSIONS
35
42
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
36
43
 
37
44
 
38
 
def zip_exporter(tree, dest, root):
 
45
def zip_exporter(tree, dest, root, subdir, filtered=False):
39
46
    """ Export this tree to a new zip file.
40
47
 
41
48
    `dest` will be created holding the contents of this tree; if it
42
49
    already exists, it will be overwritten".
43
50
    """
44
 
    import time
 
51
    mutter('export version %r', tree)
45
52
 
46
53
    now = time.localtime()[:6]
47
 
    mutter('export version %r', tree)
48
54
 
49
55
    compression = zipfile.ZIP_DEFLATED
50
56
    zipf = zipfile.ZipFile(dest, "w", compression)
51
57
 
52
 
    inv = tree.inventory
53
 
 
54
58
    try:
55
 
        entries = inv.iter_entries()
56
 
        entries.next() # skip root
57
 
        for dp, ie in entries:
58
 
            # .bzrignore has no meaning outside of a working tree
59
 
            # so do not export it
60
 
            if dp == ".bzrignore":
61
 
                continue
62
 
 
 
59
        for dp, ie in _export_iter_entries(tree, subdir):
63
60
            file_id = ie.file_id
64
61
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
65
62
 
72
69
                            date_time=now)
73
70
                zinfo.compress_type = compression
74
71
                zinfo.external_attr = _FILE_ATTR
75
 
                zipf.writestr(zinfo, tree.get_file_text(file_id))
 
72
                if filtered:
 
73
                    chunks = tree.get_file_lines(file_id)
 
74
                    filters = tree._content_filter_stack(dp)
 
75
                    context = ContentFilterContext(dp, tree, ie)
 
76
                    contents = filtered_output_bytes(chunks, filters, context)
 
77
                    content = ''.join(contents)
 
78
                else:
 
79
                    content = tree.get_file_text(file_id)
 
80
                zipf.writestr(zinfo, content)
76
81
            elif ie.kind == "directory":
77
82
                # Directories must contain a trailing slash, to indicate
78
83
                # to the zip routine that they are really directories and
98
103
        os.remove(dest)
99
104
        from bzrlib.errors import BzrError
100
105
        raise BzrError("Can't export non-ascii filenames to zip")
101