~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-04-26 22:54:07 UTC
  • Revision ID: aaron@aaronbentley.com-20110426225407-3vhag292qui1n98f
Eschew failIfExists/failUnlessExists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
19
from bzrlib.plugins.bzrtools.bzrtools import open_from_url
19
20
 
20
21
class ZipFileWrapper(object):
21
22
 
40
41
 
41
42
 
42
43
class ZipInfoWrapper(object):
43
 
    
 
44
 
44
45
    def __init__(self, zipfile, info):
45
46
        self.info = info
46
47
        self.type = None
108
109
    def isdir(self):
109
110
        return stat.S_ISDIR(self.mode)
110
111
 
111
 
        
112
 
def top_directory(path):
 
112
    def issym(self):
 
113
        if stat.S_ISLNK(self.mode):
 
114
            self.linkname = os.readlink(self.fullpath)
 
115
            return True
 
116
        else:
 
117
            return False
 
118
 
 
119
 
 
120
def top_path(path):
113
121
    """Return the top directory given in a path."""
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
 
122
    components = splitpath(path)
 
123
    if len(components) > 0:
 
124
        return components[0]
 
125
    else:
 
126
        return ''
121
127
 
122
128
 
123
129
def common_directory(names):
124
130
    """Determine a single directory prefix from a list of names"""
125
131
    possible_prefix = None
126
132
    for name in names:
127
 
        name_top = top_directory(name)
 
133
        name_top = top_path(name)
128
134
        if name_top == '':
129
135
            return None
130
136
        if possible_prefix is None:
157
163
            yield member.name
158
164
 
159
165
 
 
166
def should_ignore(relative_path):
 
167
    return top_path(relative_path) == '.bzr'
 
168
 
 
169
 
160
170
def import_tar(tree, tar_input):
161
171
    """Replace the contents of a working directory with tarfile contents.
162
172
    The tarfile may be a gzipped stream.  File ids will be updated.
184
194
        tt.delete_contents(trans_id)
185
195
        removed.add(path)
186
196
 
187
 
    added = set() 
 
197
    added = set()
188
198
    implied_parents = set()
189
199
    seen = set()
190
200
    for member in archive_file.getmembers():
191
201
        if member.type == 'g':
192
202
            # type 'g' is a header
193
203
            continue
194
 
        relative_path = member.name 
 
204
        relative_path = member.name
195
205
        if prefix is not None:
196
206
            relative_path = relative_path[len(prefix)+1:]
197
207
            relative_path = relative_path.rstrip('/')
198
208
        if relative_path == '':
199
209
            continue
 
210
        if should_ignore(relative_path):
 
211
            continue
200
212
        add_implied_parents(implied_parents, relative_path)
201
213
        trans_id = tt.trans_id_tree_path(relative_path)
202
214
        added.add(relative_path.rstrip('/'))
207
219
            tt.cancel_creation(trans_id)
208
220
        seen.add(member.name)
209
221
        if member.isreg():
210
 
            tt.create_file(file_iterator(archive_file.extractfile(member)), 
 
222
            tt.create_file(file_iterator(archive_file.extractfile(member)),
211
223
                           trans_id)
212
224
            executable = (member.mode & 0111) != 0
213
225
            tt.set_executability(executable, trans_id)
257
269
        if tree.changes_from(tree.basis_tree()).has_changed():
258
270
            raise BzrCommandError("Working tree has uncommitted changes.")
259
271
 
260
 
        if (source.endswith('.tar') or source.endswith('.tar.gz') or 
 
272
        if (source.endswith('.tar') or source.endswith('.tar.gz') or
261
273
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
262
274
            try:
 
275
                tar_input = open_from_url(source)
263
276
                if source.endswith('.bz2'):
264
 
                    tar_input = BZ2File(source, 'r')
265
 
                    tar_input = StringIO(tar_input.read())
266
 
                else:
267
 
                    tar_input = file(source, 'rb')
 
277
                    tar_input = StringIO(tar_input.read().decode('bz2'))
268
278
            except IOError, e:
269
279
                if e.errno == errno.ENOENT:
270
280
                    raise NoSuchFile(source)
273
283
            finally:
274
284
                tar_input.close()
275
285
        elif source.endswith('.zip'):
276
 
            import_zip(tree, open(source, 'rb'))
 
286
            import_zip(tree, open_from_url(source))
277
287
        elif file_kind(source) == 'directory':
278
288
            s = StringIO(source)
279
289
            s.seek(0)