~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Charlie Shepherd
  • Date: 2007-04-04 18:12:00 UTC
  • mto: This revision was merged to the branch mainline in revision 538.
  • Revision ID: masterdriverz@gentoo.org-20070404181200-wqiwytdor9srux2v
Remove all trailing whitespace

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, splitpath)
 
15
                            file_kind)
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
20
19
 
21
20
class ZipFileWrapper(object):
22
21
 
109
108
    def isdir(self):
110
109
        return stat.S_ISDIR(self.mode)
111
110
 
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):
 
111
 
 
112
def top_directory(path):
121
113
    """Return the top directory given in a path."""
122
 
    components = splitpath(path)
123
 
    if len(components) > 0:
124
 
        return components[0]
125
 
    else:
126
 
        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
127
121
 
128
122
 
129
123
def common_directory(names):
130
124
    """Determine a single directory prefix from a list of names"""
131
125
    possible_prefix = None
132
126
    for name in names:
133
 
        name_top = top_path(name)
 
127
        name_top = top_directory(name)
134
128
        if name_top == '':
135
129
            return None
136
130
        if possible_prefix is None:
163
157
            yield member.name
164
158
 
165
159
 
166
 
def should_ignore(relative_path):
167
 
    return top_path(relative_path) == '.bzr'
168
 
 
169
 
 
170
160
def import_tar(tree, tar_input):
171
161
    """Replace the contents of a working directory with tarfile contents.
172
162
    The tarfile may be a gzipped stream.  File ids will be updated.
207
197
            relative_path = relative_path.rstrip('/')
208
198
        if relative_path == '':
209
199
            continue
210
 
        if should_ignore(relative_path):
211
 
            continue
212
200
        add_implied_parents(implied_parents, relative_path)
213
201
        trans_id = tt.trans_id_tree_path(relative_path)
214
202
        added.add(relative_path.rstrip('/'))
272
260
        if (source.endswith('.tar') or source.endswith('.tar.gz') or
273
261
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
274
262
            try:
275
 
                tar_input = open_from_url(source)
276
263
                if source.endswith('.bz2'):
277
 
                    tar_input = StringIO(tar_input.read().decode('bz2'))
 
264
                    tar_input = BZ2File(source, 'r')
 
265
                    tar_input = StringIO(tar_input.read())
 
266
                else:
 
267
                    tar_input = file(source, 'rb')
278
268
            except IOError, e:
279
269
                if e.errno == errno.ENOENT:
280
270
                    raise NoSuchFile(source)
283
273
            finally:
284
274
                tar_input.close()
285
275
        elif source.endswith('.zip'):
286
 
            import_zip(tree, open_from_url(source))
 
276
            import_zip(tree, open(source, 'rb'))
287
277
        elif file_kind(source) == 'directory':
288
278
            s = StringIO(source)
289
279
            s.seek(0)