~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-04-12 04:51:37 UTC
  • Revision ID: aaron@aaronbentley.com-20110412045137-ni3eart33v4wnlst
Allow zap --branch --store if no uncommitted changes.

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
 
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.
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('/'))
260
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)