1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
1 |
# Copyright (C) 2005 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
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Export a Tree to a non-versioned directory.
|
|
18 |
"""
|
|
19 |
||
20 |
import os |
|
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
21 |
import stat |
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
22 |
import time |
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
23 |
import zipfile |
24 |
||
2024.2.8
by John Arbash Meinel
paths are always forward slashed for python zipfiles |
25 |
from bzrlib import ( |
26 |
osutils, |
|
27 |
)
|
|
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
28 |
from bzrlib.export import _export_iter_entries |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
29 |
from bzrlib.trace import mutter |
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
30 |
|
31 |
||
32 |
# Windows expects this bit to be set in the 'external_attr' section
|
|
33 |
# Or it won't consider the entry a directory
|
|
34 |
ZIP_DIRECTORY_BIT = (1 << 4) |
|
35 |
||
2024.2.8
by John Arbash Meinel
paths are always forward slashed for python zipfiles |
36 |
_FILE_ATTR = stat.S_IFREG |
37 |
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT |
|
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
38 |
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
39 |
|
3613.2.1
by Robert Collins
Teach export how to export a subdirectory. (Robert Collins) |
40 |
def zip_exporter(tree, dest, root, subdir): |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
41 |
""" Export this tree to a new zip file.
|
42 |
||
43 |
`dest` will be created holding the contents of this tree; if it
|
|
44 |
already exists, it will be overwritten".
|
|
45 |
"""
|
|
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
46 |
mutter('export version %r', tree) |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
47 |
|
48 |
now = time.localtime()[:6] |
|
49 |
||
50 |
compression = zipfile.ZIP_DEFLATED |
|
51 |
zipf = zipfile.ZipFile(dest, "w", compression) |
|
52 |
||
53 |
try: |
|
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
54 |
for dp, ie in _export_iter_entries(tree, subdir): |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
55 |
file_id = ie.file_id |
56 |
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest) |
|
57 |
||
2024.2.8
by John Arbash Meinel
paths are always forward slashed for python zipfiles |
58 |
# zipfile.ZipFile switches all paths to forward
|
59 |
# slashes anyway, so just stick with that.
|
|
60 |
filename = osutils.pathjoin(root, dp).encode('utf8') |
|
2024.2.6
by John Arbash Meinel
In zip files, directories must have trailing slashes |
61 |
if ie.kind == "file": |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
62 |
zinfo = zipfile.ZipInfo( |
2024.2.1
by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile |
63 |
filename=filename, |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
64 |
date_time=now) |
65 |
zinfo.compress_type = compression |
|
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
66 |
zinfo.external_attr = _FILE_ATTR |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
67 |
zipf.writestr(zinfo, tree.get_file_text(file_id)) |
68 |
elif ie.kind == "directory": |
|
2024.2.6
by John Arbash Meinel
In zip files, directories must have trailing slashes |
69 |
# Directories must contain a trailing slash, to indicate
|
70 |
# to the zip routine that they are really directories and
|
|
71 |
# not just empty files.
|
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
72 |
zinfo = zipfile.ZipInfo( |
2024.2.8
by John Arbash Meinel
paths are always forward slashed for python zipfiles |
73 |
filename=filename + '/', |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
74 |
date_time=now) |
75 |
zinfo.compress_type = compression |
|
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
76 |
zinfo.external_attr = _DIR_ATTR |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
77 |
zipf.writestr(zinfo,'') |
78 |
elif ie.kind == "symlink": |
|
79 |
zinfo = zipfile.ZipInfo( |
|
2024.2.1
by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile |
80 |
filename=(filename + '.lnk'), |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
81 |
date_time=now) |
82 |
zinfo.compress_type = compression |
|
2024.2.7
by John Arbash Meinel
Set the external_attr bits so Windows respects our directories |
83 |
zinfo.external_attr = _FILE_ATTR |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
84 |
zipf.writestr(zinfo, ie.symlink_target) |
85 |
||
86 |
zipf.close() |
|
87 |
||
88 |
except UnicodeEncodeError: |
|
89 |
zipf.close() |
|
90 |
os.remove(dest) |
|
91 |
from bzrlib.errors import BzrError |
|
92 |
raise BzrError("Can't export non-ascii filenames to zip") |
|
93 |