~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2007-03-26 16:26:09 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070326162609-7ale5l110bjkfa0g
Add branch parameter to show-paths

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
 
40
40
 
41
41
 
42
42
class ZipInfoWrapper(object):
43
 
 
 
43
    
44
44
    def __init__(self, zipfile, info):
45
45
        self.info = info
46
46
        self.type = None
108
108
    def isdir(self):
109
109
        return stat.S_ISDIR(self.mode)
110
110
 
111
 
    def issym(self):
112
 
        if stat.S_ISLNK(self.mode):
113
 
            self.linkname = os.readlink(self.fullpath)
114
 
            return True
115
 
        else:
116
 
            return False
117
 
 
118
 
 
119
 
def top_path(path):
 
111
        
 
112
def top_directory(path):
120
113
    """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 ''
 
114
    dirname = os.path.dirname(path)
 
115
    last_dirname = dirname
 
116
    while True:
 
117
        dirname = os.path.dirname(dirname)
 
118
        if dirname == '' or dirname == last_dirname:
 
119
            return last_dirname
 
120
        last_dirname = dirname
126
121
 
127
122
 
128
123
def common_directory(names):
129
124
    """Determine a single directory prefix from a list of names"""
130
125
    possible_prefix = None
131
126
    for name in names:
132
 
        name_top = top_path(name)
 
127
        name_top = top_directory(name)
133
128
        if name_top == '':
134
129
            return None
135
130
        if possible_prefix is None:
162
157
            yield member.name
163
158
 
164
159
 
165
 
def should_ignore(relative_path):
166
 
    return top_path(relative_path) == '.bzr'
167
 
 
168
 
 
169
160
def import_tar(tree, tar_input):
170
161
    """Replace the contents of a working directory with tarfile contents.
171
162
    The tarfile may be a gzipped stream.  File ids will be updated.
181
172
    dir_file = DirWrapper(dir_input)
182
173
    import_archive(tree, dir_file)
183
174
 
184
 
 
185
175
def import_archive(tree, archive_file):
 
176
    prefix = common_directory(names_of_files(archive_file))
186
177
    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))
 
178
 
196
179
    removed = set()
197
180
    for path, entry in tree.inventory.iter_entries():
198
181
        if entry.parent_id is None:
201
184
        tt.delete_contents(trans_id)
202
185
        removed.add(path)
203
186
 
204
 
    added = set()
 
187
    added = set() 
205
188
    implied_parents = set()
206
189
    seen = set()
207
190
    for member in archive_file.getmembers():
208
191
        if member.type == 'g':
209
192
            # type 'g' is a header
210
193
            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')
 
194
        relative_path = member.name 
215
195
        if prefix is not None:
216
196
            relative_path = relative_path[len(prefix)+1:]
217
197
            relative_path = relative_path.rstrip('/')
218
198
        if relative_path == '':
219
199
            continue
220
 
        if should_ignore(relative_path):
221
 
            continue
222
200
        add_implied_parents(implied_parents, relative_path)
223
201
        trans_id = tt.trans_id_tree_path(relative_path)
224
202
        added.add(relative_path.rstrip('/'))
229
207
            tt.cancel_creation(trans_id)
230
208
        seen.add(member.name)
231
209
        if member.isreg():
232
 
            tt.create_file(file_iterator(archive_file.extractfile(member)),
 
210
            tt.create_file(file_iterator(archive_file.extractfile(member)), 
233
211
                           trans_id)
234
212
            executable = (member.mode & 0111) != 0
235
213
            tt.set_executability(executable, trans_id)
259
237
 
260
238
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
261
239
        warning(conflict)
 
240
    tt.apply()
262
241
 
263
242
 
264
243
def do_import(source, tree_directory=None):
278
257
        if tree.changes_from(tree.basis_tree()).has_changed():
279
258
            raise BzrCommandError("Working tree has uncommitted changes.")
280
259
 
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')):
 
260
        if (source.endswith('.tar') or source.endswith('.tar.gz') or 
 
261
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
284
262
            try:
285
 
                tar_input = open_from_url(source)
286
263
                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()))
 
264
                    tar_input = BZ2File(source, 'r')
 
265
                    tar_input = StringIO(tar_input.read())
 
266
                else:
 
267
                    tar_input = file(source, 'rb')
292
268
            except IOError, e:
293
269
                if e.errno == errno.ENOENT:
294
270
                    raise NoSuchFile(source)
297
273
            finally:
298
274
                tar_input.close()
299
275
        elif source.endswith('.zip'):
300
 
            import_zip(tree, open_from_url(source))
 
276
            import_zip(tree, open(source, 'rb'))
301
277
        elif file_kind(source) == 'directory':
302
278
            s = StringIO(source)
303
279
            s.seek(0)