~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2007-06-12 22:09:44 UTC
  • mfrom: (540.1.2 bzrtools-0.17)
  • Revision ID: aaron.bentley@utoronto.ca-20070612220944-5zw4hlzp1ctq6mkl
Merge fixes from 0.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Import upstream source into a branch"""
2
2
 
 
3
from bz2 import BZ2File
3
4
import errno
4
5
import os
5
6
from StringIO import StringIO
11
12
from bzrlib.bzrdir import BzrDir
12
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
13
14
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
14
 
                            file_kind, splitpath)
 
15
                            file_kind)
15
16
from bzrlib.trace import warning
16
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
17
18
from bzrlib.workingtree import WorkingTree
18
 
from bzrlib.plugins.bzrtools.bzrtools import open_from_url
19
19
 
20
20
class ZipFileWrapper(object):
21
21
 
116
116
            return False
117
117
 
118
118
 
119
 
def top_path(path):
 
119
def top_directory(path):
120
120
    """Return the top directory given in a path."""
121
 
    components = splitpath(path)
122
 
    if len(components) > 0:
123
 
        return components[0]
124
 
    else:
125
 
        return ''
 
121
    dirname = os.path.dirname(path)
 
122
    last_dirname = dirname
 
123
    while True:
 
124
        dirname = os.path.dirname(dirname)
 
125
        if dirname == '' or dirname == last_dirname:
 
126
            return last_dirname
 
127
        last_dirname = dirname
126
128
 
127
129
 
128
130
def common_directory(names):
129
131
    """Determine a single directory prefix from a list of names"""
130
132
    possible_prefix = None
131
133
    for name in names:
132
 
        name_top = top_path(name)
 
134
        name_top = top_directory(name)
133
135
        if name_top == '':
134
136
            return None
135
137
        if possible_prefix is None:
162
164
            yield member.name
163
165
 
164
166
 
165
 
def should_ignore(relative_path):
166
 
    return top_path(relative_path) == '.bzr'
167
 
 
168
 
 
169
167
def import_tar(tree, tar_input):
170
168
    """Replace the contents of a working directory with tarfile contents.
171
169
    The tarfile may be a gzipped stream.  File ids will be updated.
181
179
    dir_file = DirWrapper(dir_input)
182
180
    import_archive(tree, dir_file)
183
181
 
184
 
 
185
182
def import_archive(tree, archive_file):
 
183
    prefix = common_directory(names_of_files(archive_file))
186
184
    tt = TreeTransform(tree)
187
 
    try:
188
 
        import_archive_to_transform(tree, archive_file, tt)
189
 
        tt.apply()
190
 
    finally:
191
 
        tt.finalize()
192
 
 
193
 
 
194
 
def import_archive_to_transform(tree, archive_file, tt):
195
 
    prefix = common_directory(names_of_files(archive_file))
 
185
 
196
186
    removed = set()
197
187
    for path, entry in tree.inventory.iter_entries():
198
188
        if entry.parent_id is None:
208
198
        if member.type == 'g':
209
199
            # type 'g' is a header
210
200
            continue
211
 
        # Inverse functionality in bzr uses utf-8.  We could also
212
 
        # interpret relative to fs encoding, which would match native
213
 
        # behaviour better.
214
 
        relative_path = member.name.decode('utf-8')
 
201
        relative_path = member.name
215
202
        if prefix is not None:
216
203
            relative_path = relative_path[len(prefix)+1:]
217
204
            relative_path = relative_path.rstrip('/')
218
205
        if relative_path == '':
219
206
            continue
220
 
        if should_ignore(relative_path):
221
 
            continue
222
207
        add_implied_parents(implied_parents, relative_path)
223
208
        trans_id = tt.trans_id_tree_path(relative_path)
224
209
        added.add(relative_path.rstrip('/'))
259
244
 
260
245
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
261
246
        warning(conflict)
 
247
    tt.apply()
262
248
 
263
249
 
264
250
def do_import(source, tree_directory=None):
279
265
            raise BzrCommandError("Working tree has uncommitted changes.")
280
266
 
281
267
        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')):
 
268
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
284
269
            try:
285
 
                tar_input = open_from_url(source)
286
270
                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()))
 
271
                    tar_input = BZ2File(source, 'r')
 
272
                    tar_input = StringIO(tar_input.read())
 
273
                else:
 
274
                    tar_input = file(source, 'rb')
292
275
            except IOError, e:
293
276
                if e.errno == errno.ENOENT:
294
277
                    raise NoSuchFile(source)
297
280
            finally:
298
281
                tar_input.close()
299
282
        elif source.endswith('.zip'):
300
 
            import_zip(tree, open_from_url(source))
 
283
            import_zip(tree, open(source, 'rb'))
301
284
        elif file_kind(source) == 'directory':
302
285
            s = StringIO(source)
303
286
            s.seek(0)