~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-06-27 23:07:10 UTC
  • Revision ID: aaron@aaronbentley.com-20110627230710-orth0tzf1kwknfen
Better handling of compound tar names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import errno
4
4
import os
 
5
import re
5
6
from StringIO import StringIO
6
7
import stat
7
8
import tarfile
16
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
17
18
from bzrlib.workingtree import WorkingTree
18
19
from bzrlib.plugins.bzrtools.bzrtools import open_from_url
 
20
from bzrlib.plugins.bzrtools import errors
19
21
 
20
22
class ZipFileWrapper(object):
21
23
 
278
280
        if tree.changes_from(tree.basis_tree()).has_changed():
279
281
            raise BzrCommandError("Working tree has uncommitted changes.")
280
282
 
281
 
        if (source.endswith('.tar') or source.endswith('.tar.gz') or
282
 
            source.endswith('.tar.bz2') or source.endswith('.tgz') or
283
 
            source.endswith('.tar.lzma') or source.endswith('.tar.xz')):
284
 
            try:
285
 
                tar_input = open_from_url(source)
286
 
                if source.endswith('.bz2'):
287
 
                    import bz2
288
 
                    tar_input = StringIO(bz2.decompress(tar_input.read()))
289
 
                elif source.endswith('.xz') or source.endswith('.lzma'):
290
 
                    import lzma
291
 
                    tar_input = StringIO(lzma.decompress(tar_input.read()))
292
 
            except IOError, e:
293
 
                if e.errno == errno.ENOENT:
294
 
                    raise NoSuchFile(source)
295
 
            try:
296
 
                import_tar(tree, tar_input)
297
 
            finally:
298
 
                tar_input.close()
299
 
        elif source.endswith('.zip'):
300
 
            import_zip(tree, open_from_url(source))
301
 
        elif file_kind(source) == 'directory':
302
 
            s = StringIO(source)
303
 
            s.seek(0)
304
 
            import_dir(tree, s)
 
283
        try:
 
284
            archive, external_compressor = get_archive_type(source)
 
285
        except errors.NotArchiveType:
 
286
            if file_kind(source) == 'directory':
 
287
                s = StringIO(source)
 
288
                s.seek(0)
 
289
                import_dir(tree, s)
 
290
            else:
 
291
                raise BzrCommandError('Unhandled import source')
305
292
        else:
306
 
            raise BzrCommandError('Unhandled import source')
 
293
            if archive == 'zip':
 
294
                import_zip(tree, open_from_url(source))
 
295
            elif archive == 'tar':
 
296
                try:
 
297
                    tar_input = open_from_url(source)
 
298
                    if external_compressor == 'bz2':
 
299
                        import bz2
 
300
                        tar_input = StringIO(bz2.decompress(tar_input.read()))
 
301
                    elif external_compressor == 'lzma':
 
302
                        import lzma
 
303
                        tar_input = StringIO(lzma.decompress(tar_input.read()))
 
304
                except IOError, e:
 
305
                    if e.errno == errno.ENOENT:
 
306
                        raise NoSuchFile(source)
 
307
                try:
 
308
                    import_tar(tree, tar_input)
 
309
                finally:
 
310
                    tar_input.close()
307
311
    finally:
308
312
        tree.unlock()
 
313
 
 
314
 
 
315
def get_archive_type(path):
 
316
    """Return the type of archive and compressor indicated by path name.
 
317
 
 
318
    Only external compressors are returned, so zip files are only
 
319
    ('zip', None).  .tgz is treated as ('tar', 'gz') and '.xz' is treated as
 
320
    'tar', 'lzma'.
 
321
    """
 
322
    matches = re.match(r'.*\.(zip|tgz|tar(.(gz|bz2|lzma|xz))?)$', path)
 
323
    if not matches:
 
324
        raise errors.NotArchiveType(path)
 
325
    external_compressor = None
 
326
    if matches.group(3) is not None:
 
327
        archive = 'tar'
 
328
        external_compressor = matches.group(3)
 
329
        if external_compressor == 'xz':
 
330
            external_compressor = 'lzma'
 
331
    elif matches.group(1) == 'tgz':
 
332
        return 'tar', 'gz'
 
333
    else:
 
334
        archive = matches.group(1)
 
335
    return archive, external_compressor