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
|
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., 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 |
|
21 |
from bzrlib.trace import mutter |
|
22 |
import zipfile |
|
23 |
||
24 |
def zip_exporter(tree, dest, root): |
|
25 |
""" Export this tree to a new zip file.
|
|
26 |
||
27 |
`dest` will be created holding the contents of this tree; if it
|
|
28 |
already exists, it will be overwritten".
|
|
29 |
"""
|
|
30 |
import time |
|
31 |
||
32 |
now = time.localtime()[:6] |
|
33 |
mutter('export version %r', tree) |
|
34 |
||
35 |
compression = zipfile.ZIP_DEFLATED |
|
36 |
zipf = zipfile.ZipFile(dest, "w", compression) |
|
37 |
||
38 |
inv = tree.inventory |
|
39 |
||
40 |
try: |
|
41 |
for dp, ie in inv.iter_entries(): |
|
42 |
||
43 |
file_id = ie.file_id |
|
44 |
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest) |
|
45 |
||
46 |
if ie.kind == "file": |
|
47 |
zinfo = zipfile.ZipInfo( |
|
48 |
filename=str(os.path.join(root, dp)), |
|
49 |
date_time=now) |
|
50 |
zinfo.compress_type = compression |
|
51 |
zipf.writestr(zinfo, tree.get_file_text(file_id)) |
|
52 |
elif ie.kind == "directory": |
|
53 |
zinfo = zipfile.ZipInfo( |
|
54 |
filename=str(os.path.join(root, dp)+os.sep), |
|
55 |
date_time=now) |
|
56 |
zinfo.compress_type = compression |
|
57 |
zipf.writestr(zinfo,'') |
|
58 |
elif ie.kind == "symlink": |
|
59 |
zinfo = zipfile.ZipInfo( |
|
60 |
filename=str(os.path.join(root, dp+".lnk")), |
|
61 |
date_time=now) |
|
62 |
zinfo.compress_type = compression |
|
63 |
zipf.writestr(zinfo, ie.symlink_target) |
|
64 |
||
65 |
zipf.close() |
|
66 |
||
67 |
except UnicodeEncodeError: |
|
68 |
zipf.close() |
|
69 |
os.remove(dest) |
|
70 |
from bzrlib.errors import BzrError |
|
71 |
raise BzrError("Can't export non-ascii filenames to zip") |
|
72 |