~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-05-10 01:53:58 UTC
  • mfrom: (756.1.1 no-deprecated-graph)
  • Revision ID: aaron@aaronbentley.com-20110510015358-y6ft9e47oisysftz
Move deprecated_graph functionality to bzrtools (Jelmer).

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
4
3
import errno
5
4
import os
 
5
import sys
6
6
from StringIO import StringIO
7
7
import stat
8
8
import tarfile
12
12
from bzrlib.bzrdir import BzrDir
13
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
14
14
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
15
 
                            file_kind)
 
15
                            file_kind, splitpath)
16
16
from bzrlib.trace import warning
17
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
18
18
from bzrlib.workingtree import WorkingTree
117
117
            return False
118
118
 
119
119
 
120
 
def top_directory(path):
 
120
def top_path(path):
121
121
    """Return the top directory given in a path."""
122
 
    dirname = os.path.dirname(path)
123
 
    last_dirname = dirname
124
 
    while True:
125
 
        dirname = os.path.dirname(dirname)
126
 
        if dirname == '' or dirname == last_dirname:
127
 
            return last_dirname
128
 
        last_dirname = dirname
 
122
    components = splitpath(path)
 
123
    if len(components) > 0:
 
124
        return components[0]
 
125
    else:
 
126
        return ''
129
127
 
130
128
 
131
129
def common_directory(names):
132
130
    """Determine a single directory prefix from a list of names"""
133
131
    possible_prefix = None
134
132
    for name in names:
135
 
        name_top = top_directory(name)
 
133
        name_top = top_path(name)
136
134
        if name_top == '':
137
135
            return None
138
136
        if possible_prefix is None:
165
163
            yield member.name
166
164
 
167
165
 
 
166
def should_ignore(relative_path):
 
167
    return top_path(relative_path) == '.bzr'
 
168
 
 
169
 
168
170
def import_tar(tree, tar_input):
169
171
    """Replace the contents of a working directory with tarfile contents.
170
172
    The tarfile may be a gzipped stream.  File ids will be updated.
180
182
    dir_file = DirWrapper(dir_input)
181
183
    import_archive(tree, dir_file)
182
184
 
 
185
 
183
186
def import_archive(tree, archive_file):
 
187
    tt = TreeTransform(tree)
 
188
    try:
 
189
        import_archive_to_transform(tree, archive_file, tt)
 
190
        tt.apply()
 
191
    finally:
 
192
        tt.finalize()
 
193
 
 
194
 
 
195
def import_archive_to_transform(tree, archive_file, tt):
184
196
    prefix = common_directory(names_of_files(archive_file))
185
 
    tt = TreeTransform(tree)
186
 
 
187
197
    removed = set()
188
198
    for path, entry in tree.inventory.iter_entries():
189
199
        if entry.parent_id is None:
199
209
        if member.type == 'g':
200
210
            # type 'g' is a header
201
211
            continue
202
 
        relative_path = member.name
 
212
        # Inverse functionality in bzr uses utf-8.  We could also
 
213
        # interpret relative to fs encoding, which would match native
 
214
        # behaviour better.
 
215
        relative_path = member.name.decode('utf-8')
203
216
        if prefix is not None:
204
217
            relative_path = relative_path[len(prefix)+1:]
205
218
            relative_path = relative_path.rstrip('/')
206
219
        if relative_path == '':
207
220
            continue
 
221
        if should_ignore(relative_path):
 
222
            continue
208
223
        add_implied_parents(implied_parents, relative_path)
209
224
        trans_id = tt.trans_id_tree_path(relative_path)
210
225
        added.add(relative_path.rstrip('/'))
245
260
 
246
261
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
247
262
        warning(conflict)
248
 
    tt.apply()
249
263
 
250
264
 
251
265
def do_import(source, tree_directory=None):