/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/upstream_import.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Import upstream source into a branch"""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
19
import errno
 
20
from io import (
 
21
    BytesIO,
 
22
    )
22
23
import os
23
24
import re
24
25
import stat
25
26
import tarfile
26
27
import zipfile
27
28
 
28
 
from . import generate_ids, urlutils
 
29
from . import urlutils
 
30
from .bzr import generate_ids
29
31
from .controldir import ControlDir, is_control_filename
30
32
from .errors import (BzrError, NoSuchFile, BzrCommandError, NotBranchError)
31
33
from .osutils import (pathjoin, isdir, file_iterator, basename,
32
34
                      file_kind, splitpath)
33
 
from .sixish import StringIO
34
35
from .trace import warning
35
 
from .transform import TreeTransform, resolve_conflicts, cook_conflicts
 
36
from .transform import resolve_conflicts, cook_conflicts
36
37
from .transport import get_transport
37
38
from .workingtree import WorkingTree
38
39
 
65
66
            yield ZipInfoWrapper(self.zipfile, info)
66
67
 
67
68
    def extractfile(self, infowrapper):
68
 
        return StringIO(self.zipfile.read(infowrapper.name))
 
69
        return BytesIO(self.zipfile.read(infowrapper.name))
69
70
 
70
71
    def add(self, filename):
71
72
        if isdir(filename):
72
 
            self.zipfile.writestr(filename+'/', '')
 
73
            self.zipfile.writestr(filename + '/', '')
73
74
        else:
74
75
            self.zipfile.write(filename)
75
76
 
101
102
        if mode != 'r':
102
103
            raise AssertionError(
103
104
                'only readonly supported')
104
 
        self.root = os.path.realpath(fileobj.read())
 
105
        self.root = os.path.realpath(fileobj.read().decode('utf-8'))
105
106
 
106
107
    def __repr__(self):
107
108
        return 'DirWrapper(%r)' % self.root
121
122
                    yield v
122
123
 
123
124
    def extractfile(self, member):
124
 
        return open(member.fullpath)
 
125
        return open(member.fullpath, 'rb')
125
126
 
126
127
 
127
128
class FileInfo(object):
214
215
    tar_file = tarfile.open('lala', 'r', tar_input)
215
216
    import_archive(tree, tar_file)
216
217
 
 
218
 
217
219
def import_zip(tree, zip_input):
218
220
    zip_file = ZipFileWrapper(zip_input, 'r')
219
221
    import_archive(tree, zip_file)
220
222
 
 
223
 
221
224
def import_dir(tree, dir_input):
222
225
    dir_file = DirWrapper(dir_input)
223
226
    import_archive(tree, dir_file)
224
227
 
225
228
 
226
229
def import_archive(tree, archive_file):
227
 
    tt = TreeTransform(tree)
228
 
    try:
 
230
    with tree.get_transform() as tt:
229
231
        import_archive_to_transform(tree, archive_file, tt)
230
232
        tt.apply()
231
 
    finally:
232
 
        tt.finalize()
233
233
 
234
234
 
235
235
def import_archive_to_transform(tree, archive_file, tt):
252
252
        # Inverse functionality in bzr uses utf-8.  We could also
253
253
        # interpret relative to fs encoding, which would match native
254
254
        # behaviour better.
255
 
        relative_path = member.name.decode('utf-8')
 
255
        relative_path = member.name
 
256
        if not isinstance(relative_path, str):
 
257
            relative_path = relative_path.decode('utf-8')
256
258
        if prefix is not None:
257
 
            relative_path = relative_path[len(prefix)+1:]
 
259
            relative_path = relative_path[len(prefix) + 1:]
258
260
            relative_path = relative_path.rstrip('/')
259
261
        if relative_path == '':
260
262
            continue
314
316
            tree = branch.controldir.open_workingtree()
315
317
    else:
316
318
        tree = WorkingTree.open_containing('.')[0]
317
 
    tree.lock_write()
318
 
    try:
 
319
    with tree.lock_write():
319
320
        if tree.changes_from(tree.basis_tree()).has_changed():
320
321
            raise BzrCommandError("Working tree has uncommitted changes.")
321
322
 
323
324
            archive, external_compressor = get_archive_type(source)
324
325
        except NotArchiveType:
325
326
            if file_kind(source) == 'directory':
326
 
                s = StringIO(source)
 
327
                s = BytesIO(source.encode('utf-8'))
327
328
                s.seek(0)
328
329
                import_dir(tree, s)
329
330
            else:
336
337
                    tar_input = open_from_url(source)
337
338
                    if external_compressor == 'bz2':
338
339
                        import bz2
339
 
                        tar_input = StringIO(bz2.decompress(tar_input.read()))
 
340
                        tar_input = BytesIO(bz2.decompress(tar_input.read()))
340
341
                    elif external_compressor == 'lzma':
341
342
                        import lzma
342
 
                        tar_input = StringIO(lzma.decompress(tar_input.read()))
 
343
                        tar_input = BytesIO(lzma.decompress(tar_input.read()))
343
344
                except IOError as e:
344
345
                    if e.errno == errno.ENOENT:
345
346
                        raise NoSuchFile(source)
347
348
                    import_tar(tree, tar_input)
348
349
                finally:
349
350
                    tar_input.close()
350
 
    finally:
351
 
        tree.unlock()
352
351
 
353
352
 
354
353
def get_archive_type(path):