17
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
18
18
from bzrlib.workingtree import WorkingTree
19
19
from bzrlib.plugins.bzrtools.bzrtools import open_from_url
20
from bzrlib.plugins.bzrtools import errors
22
21
class ZipFileWrapper(object):
183
182
dir_file = DirWrapper(dir_input)
184
183
import_archive(tree, dir_file)
187
185
def import_archive(tree, archive_file):
186
prefix = common_directory(names_of_files(archive_file))
188
187
tt = TreeTransform(tree)
190
import_archive_to_transform(tree, archive_file, tt)
196
def import_archive_to_transform(tree, archive_file, tt):
197
prefix = common_directory(names_of_files(archive_file))
199
190
for path, entry in tree.inventory.iter_entries():
200
191
if entry.parent_id is None:
210
201
if member.type == 'g':
211
202
# type 'g' is a header
213
# Inverse functionality in bzr uses utf-8. We could also
214
# interpret relative to fs encoding, which would match native
216
relative_path = member.name.decode('utf-8')
204
relative_path = member.name
217
205
if prefix is not None:
218
206
relative_path = relative_path[len(prefix)+1:]
219
207
relative_path = relative_path.rstrip('/')
280
269
if tree.changes_from(tree.basis_tree()).has_changed():
281
270
raise BzrCommandError("Working tree has uncommitted changes.")
284
archive, external_compressor = get_archive_type(source)
285
except errors.NotArchiveType:
286
if file_kind(source) == 'directory':
291
raise BzrCommandError('Unhandled import source')
272
if (source.endswith('.tar') or source.endswith('.tar.gz') or
273
source.endswith('.tar.bz2')) or source.endswith('.tgz'):
275
tar_input = open_from_url(source)
276
if source.endswith('.bz2'):
277
tar_input = StringIO(tar_input.read().decode('bz2'))
279
if e.errno == errno.ENOENT:
280
raise NoSuchFile(source)
282
import_tar(tree, tar_input)
285
elif source.endswith('.zip'):
286
import_zip(tree, open_from_url(source))
287
elif file_kind(source) == 'directory':
294
import_zip(tree, open_from_url(source))
295
elif archive == 'tar':
297
tar_input = open_from_url(source)
298
if external_compressor == 'bz2':
300
tar_input = StringIO(bz2.decompress(tar_input.read()))
301
elif external_compressor == 'lzma':
303
tar_input = StringIO(lzma.decompress(tar_input.read()))
305
if e.errno == errno.ENOENT:
306
raise NoSuchFile(source)
308
import_tar(tree, tar_input)
292
raise BzrCommandError('Unhandled import source')
315
def get_archive_type(path):
316
"""Return the type of archive and compressor indicated by path name.
318
Only external compressors are returned, so zip files are only
319
('zip', None). .tgz is treated as ('tar', 'gz') and '.tar.xz' is treated
322
matches = re.match(r'.*\.(zip|tgz|tar(.(gz|bz2|lzma|xz))?)$', path)
324
raise errors.NotArchiveType(path)
325
external_compressor = None
326
if matches.group(3) is not None:
328
external_compressor = matches.group(3)
329
if external_compressor == 'xz':
330
external_compressor = 'lzma'
331
elif matches.group(1) == 'tgz':
334
archive = matches.group(1)
335
return archive, external_compressor