bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5809.3.2
by Aaron Bentley
Support PreviewTree.has_filename. |
1 |
# Copyright (C) 2005-2011 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.1
by Martin Pool
pep8 cleanup |
17 |
"""Export trees to tarballs, non-controlled directories, zipfiles, etc.
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
18 |
"""
|
19 |
||
6968.2.8
by Jelmer Vernooij
Make export a module. |
20 |
import errno |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
21 |
import os |
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
22 |
import sys |
5718.5.1
by Jelmer Vernooij
per_file_timestamp -> force_mtime. |
23 |
import time |
6006.3.6
by Martin Pool
Deprecate export(filtered=True) |
24 |
|
6968.2.8
by Jelmer Vernooij
Make export a module. |
25 |
from . import ( |
6968.2.6
by Jelmer Vernooij
Move tar/zip to breezy.archive. |
26 |
archive, |
5436.2.1
by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__. |
27 |
errors, |
6968.2.8
by Jelmer Vernooij
Make export a module. |
28 |
osutils, |
5718.5.4
by Jelmer Vernooij
fix timestamp in tgz files. |
29 |
trace, |
5436.2.1
by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__. |
30 |
)
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
31 |
|
5967.6.1
by Martin Pool
pep8 cleanup |
32 |
|
6968.2.9
by Jelmer Vernooij
Simplify export module. |
33 |
def export(tree, dest, format=None, root=None, subdir=None, |
34 |
per_file_timestamps=False, fileobj=None): |
|
35 |
"""Export the given Tree to the specific destination. |
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
36 |
|
37 |
:param tree: A Tree (such as RevisionTree) to export
|
|
6968.2.9
by Jelmer Vernooij
Simplify export module. |
38 |
:param dest: The destination where the files,etc should be put
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
39 |
:param format: The format (dir, zip, etc), if None, it will check the
|
6968.2.9
by Jelmer Vernooij
Simplify export module. |
40 |
extension on dest, looking for a match
|
41 |
:param root: The root location inside the format.
|
|
42 |
It is common practise to have zipfiles and tarballs
|
|
43 |
extract into a subdirectory, rather than into the
|
|
44 |
current working directory.
|
|
45 |
If root is None, the default root will be
|
|
46 |
selected as the destination without its
|
|
47 |
extension.
|
|
3613.2.1
by Robert Collins
Teach export how to export a subdirectory. (Robert Collins) |
48 |
:param subdir: A starting directory within the tree. None means to export
|
49 |
the entire tree, and anything else should specify the relative path to
|
|
50 |
a directory to start exporting from.
|
|
6968.2.9
by Jelmer Vernooij
Simplify export module. |
51 |
:param per_file_timestamps: Whether to use the timestamp stored in the
|
52 |
tree rather than now(). This will do a revision lookup
|
|
53 |
for every file so will be significantly slower.
|
|
5952.1.6
by geoffreyfishing at gmail
Created get_export_generator. |
54 |
:param fileobj: Optional file object to use
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
55 |
"""
|
5952.1.6
by geoffreyfishing at gmail
Created get_export_generator. |
56 |
if format is None and dest is not None: |
6968.2.9
by Jelmer Vernooij
Simplify export module. |
57 |
format = guess_format(dest) |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
58 |
|
59 |
# Most of the exporters will just have to call
|
|
60 |
# this function anyway, so why not do it for them
|
|
61 |
if root is None: |
|
62 |
root = get_root_name(dest) |
|
63 |
||
5718.5.1
by Jelmer Vernooij
per_file_timestamp -> force_mtime. |
64 |
if not per_file_timestamps: |
7240.8.2
by Jelmer Vernooij
Fix tests. |
65 |
force_mtime = time.time() |
7240.8.1
by Jelmer Vernooij
Use tree timestamp when exporting. |
66 |
if getattr(tree, '_repository', None): |
7240.8.2
by Jelmer Vernooij
Fix tests. |
67 |
try: |
68 |
force_mtime = tree._repository.get_revision( |
|
69 |
tree.get_revision_id()).timestamp |
|
70 |
except errors.NoSuchRevision: |
|
71 |
pass
|
|
5718.5.1
by Jelmer Vernooij
per_file_timestamp -> force_mtime. |
72 |
else: |
73 |
force_mtime = None |
|
74 |
||
5718.5.4
by Jelmer Vernooij
fix timestamp in tgz files. |
75 |
trace.mutter('export version %r', tree) |
76 |
||
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
77 |
if format == 'dir': |
6968.2.9
by Jelmer Vernooij
Simplify export module. |
78 |
# TODO(jelmer): If the tree is remote (e.g. HPSS, Git Remote),
|
79 |
# then we should stream a tar file and unpack that on the fly.
|
|
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
80 |
with tree.lock_read(): |
81 |
for unused in dir_exporter_generator(tree, dest, root, subdir, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
82 |
force_mtime): |
6968.2.9
by Jelmer Vernooij
Simplify export module. |
83 |
pass
|
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
84 |
return
|
85 |
||
6754.8.4
by Jelmer Vernooij
Use new context stuff. |
86 |
with tree.lock_read(): |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
87 |
chunks = tree.archive(format, dest, root=root, |
88 |
subdir=subdir, force_mtime=force_mtime) |
|
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
89 |
if dest == '-': |
90 |
for chunk in chunks: |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
91 |
getattr(sys.stdout, 'buffer', sys.stdout).write(chunk) |
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
92 |
elif fileobj is not None: |
93 |
for chunk in chunks: |
|
94 |
fileobj.write(chunk) |
|
95 |
else: |
|
96 |
with open(dest, 'wb') as f: |
|
97 |
for chunk in chunks: |
|
6973.11.1
by Jelmer Vernooij
Fix export_pot. |
98 |
f.write(chunk) |
6968.2.9
by Jelmer Vernooij
Simplify export module. |
99 |
|
100 |
||
101 |
def guess_format(filename, default='dir'): |
|
102 |
"""Guess the export format based on a file name. |
|
103 |
||
104 |
:param filename: Filename to guess from
|
|
105 |
:param default: Default format to fall back to
|
|
106 |
:return: format name
|
|
5952.1.6
by geoffreyfishing at gmail
Created get_export_generator. |
107 |
"""
|
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
108 |
format = archive.format_registry.get_format_from_filename(filename) |
109 |
if format is None: |
|
6968.2.9
by Jelmer Vernooij
Simplify export module. |
110 |
format = default |
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
111 |
return format |
112 |
||
113 |
||
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
114 |
def get_root_name(dest): |
115 |
"""Get just the root name for an export. |
|
116 |
||
117 |
"""
|
|
118 |
global _exporter_extensions |
|
5718.5.18
by Jelmer Vernooij
Don't export to '-', but rather to ''. |
119 |
if dest == '-': |
120 |
# Exporting to -/foo doesn't make sense so use relative paths.
|
|
121 |
return '' |
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
122 |
dest = os.path.basename(dest) |
6968.2.7
by Jelmer Vernooij
Add breezy.archive. |
123 |
for ext in archive.format_registry.extensions: |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
124 |
if dest.endswith(ext): |
125 |
return dest[:-len(ext)] |
|
1185.31.13
by John Arbash Meinel
Updated the test to also test zip exports. Fixed some small bugs exposed by test suite. |
126 |
return dest |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
127 |
|
128 |
||
5718.5.8
by Jelmer Vernooij
add skip_special option. |
129 |
def _export_iter_entries(tree, subdir, skip_special=True): |
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
130 |
"""Iter the entries for tree suitable for exporting. |
131 |
||
132 |
:param tree: A tree object.
|
|
4988.10.2
by michal
bzr export won't fail on symlink exporting |
133 |
:param subdir: None or the path of an entry to start exporting from.
|
5718.5.8
by Jelmer Vernooij
add skip_special option. |
134 |
:param skip_special: Whether to skip .bzr files.
|
6331.2.1
by Jelmer Vernooij
Consistently pass tree path when exporting. |
135 |
:return: iterator over tuples with final path, tree path and inventory
|
136 |
entry for each entry to export
|
|
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
137 |
"""
|
5809.3.1
by Aaron Bentley
Switch to iter_entries_by_dir. |
138 |
if subdir == '': |
139 |
subdir = None |
|
140 |
if subdir is not None: |
|
141 |
subdir = subdir.rstrip('/') |
|
142 |
entries = tree.iter_entries_by_dir() |
|
143 |
for path, entry in entries: |
|
6928.1.2
by Jelmer Vernooij
Fix path specification. |
144 |
if path == '': |
145 |
continue
|
|
146 |
||
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
147 |
# The .bzr* namespace is reserved for "magic" files like
|
148 |
# .bzrignore and .bzrrules - do not export these
|
|
5809.3.1
by Aaron Bentley
Switch to iter_entries_by_dir. |
149 |
if skip_special and path.startswith(".bzr"): |
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
150 |
continue
|
5809.3.1
by Aaron Bentley
Switch to iter_entries_by_dir. |
151 |
if path == subdir: |
152 |
if entry.kind == 'directory': |
|
153 |
continue
|
|
154 |
final_path = entry.name |
|
155 |
elif subdir is not None: |
|
156 |
if path.startswith(subdir + '/'): |
|
157 |
final_path = path[len(subdir) + 1:] |
|
158 |
else: |
|
4010.2.1
by James Westby
Handle files that are not present in the tree when exporting (#174539) |
159 |
continue
|
160 |
else: |
|
5809.3.1
by Aaron Bentley
Switch to iter_entries_by_dir. |
161 |
final_path = path |
162 |
if not tree.has_filename(path): |
|
163 |
continue
|
|
5967.6.1
by Martin Pool
pep8 cleanup |
164 |
|
6331.2.1
by Jelmer Vernooij
Consistently pass tree path when exporting. |
165 |
yield final_path, path, entry |
6968.2.8
by Jelmer Vernooij
Make export a module. |
166 |
|
167 |
||
168 |
def dir_exporter_generator(tree, dest, root, subdir=None, |
|
169 |
force_mtime=None, fileobj=None): |
|
170 |
"""Return a generator that exports this tree to a new directory. |
|
171 |
||
172 |
`dest` should either not exist or should be empty. If it does not exist it
|
|
173 |
will be created holding the contents of this tree.
|
|
174 |
||
175 |
:note: If the export fails, the destination directory will be
|
|
176 |
left in an incompletely exported state: export is not transactional.
|
|
177 |
"""
|
|
178 |
try: |
|
179 |
os.mkdir(dest) |
|
180 |
except OSError as e: |
|
181 |
if e.errno == errno.EEXIST: |
|
182 |
# check if directory empty
|
|
183 |
if os.listdir(dest) != []: |
|
184 |
raise errors.BzrError( |
|
185 |
"Can't export tree to non-empty directory.") |
|
186 |
else: |
|
187 |
raise
|
|
188 |
# Iterate everything, building up the files we will want to export, and
|
|
189 |
# creating the directories and symlinks that we need.
|
|
190 |
# This tracks (file_id, (destination_path, executable))
|
|
191 |
# This matches the api that tree.iter_files_bytes() wants
|
|
192 |
# Note in the case of revision trees, this does trigger a double inventory
|
|
193 |
# lookup, hopefully it isn't too expensive.
|
|
194 |
to_fetch = [] |
|
195 |
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. |
196 |
file_id = getattr(ie, 'file_id', None) |
6968.2.8
by Jelmer Vernooij
Make export a module. |
197 |
fullpath = osutils.pathjoin(dest, dp) |
198 |
if ie.kind == "file": |
|
6973.3.4
by Jelmer Vernooij
Don't assume entries returned by trees have a 'file_id' attribute. |
199 |
to_fetch.append((tp, (dp, tp, file_id))) |
6968.2.8
by Jelmer Vernooij
Make export a module. |
200 |
elif ie.kind in ("directory", "tree-reference"): |
201 |
os.mkdir(fullpath) |
|
202 |
elif ie.kind == "symlink": |
|
203 |
try: |
|
7141.7.1
by Jelmer Vernooij
Get rid of file_ids in most of Tree. |
204 |
symlink_target = tree.get_symlink_target(tp) |
6968.2.8
by Jelmer Vernooij
Make export a module. |
205 |
os.symlink(symlink_target, fullpath) |
206 |
except OSError as e: |
|
207 |
raise errors.BzrError( |
|
208 |
"Failed to create symlink %r -> %r, error: %s" |
|
209 |
% (fullpath, symlink_target, e)) |
|
210 |
else: |
|
211 |
raise errors.BzrError("don't know how to export {%s} of kind %r" % |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
212 |
(tp, ie.kind)) |
6968.2.8
by Jelmer Vernooij
Make export a module. |
213 |
|
214 |
yield
|
|
215 |
# The data returned here can be in any order, but we've already created all
|
|
216 |
# the directories
|
|
217 |
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0) |
|
218 |
for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch): |
|
219 |
fullpath = osutils.pathjoin(dest, relpath) |
|
220 |
# We set the mode and let the umask sort out the file info
|
|
221 |
mode = 0o666 |
|
7141.7.1
by Jelmer Vernooij
Get rid of file_ids in most of Tree. |
222 |
if tree.is_executable(treepath): |
6968.2.8
by Jelmer Vernooij
Make export a module. |
223 |
mode = 0o777 |
224 |
with os.fdopen(os.open(fullpath, flags, mode), 'wb') as out: |
|
225 |
out.writelines(chunks) |
|
226 |
if force_mtime is not None: |
|
227 |
mtime = force_mtime |
|
228 |
else: |
|
7141.7.1
by Jelmer Vernooij
Get rid of file_ids in most of Tree. |
229 |
mtime = tree.get_file_mtime(treepath) |
6968.2.8
by Jelmer Vernooij
Make export a module. |
230 |
os.utime(fullpath, (mtime, mtime)) |
231 |
||
232 |
yield
|