~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2007-01-04 15:37:11 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070104153711-gghmtwum1xidifmj
Support deep cbranch hierarcy via appendpath

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
10
11
from bzrlib import generate_ids
11
12
from bzrlib.bzrdir import BzrDir
12
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
13
 
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
14
 
                            file_kind, splitpath)
 
14
from bzrlib.osutils import pathjoin, isdir, file_iterator, basename
15
15
from bzrlib.trace import warning
16
16
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
17
17
from bzrlib.workingtree import WorkingTree
18
 
from bzrlib.plugins.bzrtools.bzrtools import open_from_url
19
18
 
20
19
class ZipFileWrapper(object):
21
20
 
40
39
 
41
40
 
42
41
class ZipInfoWrapper(object):
43
 
 
 
42
    
44
43
    def __init__(self, zipfile, info):
45
44
        self.info = info
46
45
        self.type = None
62
61
        assert mode == 'r', mode
63
62
        self.root = os.path.realpath(fileobj.read())
64
63
 
65
 
    def __repr__(self):
66
 
        return 'DirWrapper(%r)' % self.root
67
 
 
68
64
    def getmembers(self, subdir=None):
69
65
        if subdir is not None:
70
66
            mydir = pathjoin(self.root, subdir)
88
84
    def __init__(self, root, filepath):
89
85
        self.fullpath = pathjoin(root, filepath)
90
86
        self.root = root
91
 
        if filepath != '':
92
 
            self.name = pathjoin(basename(root), filepath)
93
 
        else:
94
 
            print 'root %r' % root
95
 
            self.name = basename(root)
 
87
        self.name = filepath 
96
88
        self.type = None
97
89
        stat = os.lstat(self.fullpath)
98
90
        self.mode = stat.st_mode
99
91
        if self.isdir():
100
92
            self.name += '/'
101
93
 
102
 
    def __repr__(self):
103
 
        return 'FileInfo(%r)' % self.name
104
 
 
105
94
    def isreg(self):
106
95
        return stat.S_ISREG(self.mode)
107
96
 
108
97
    def isdir(self):
109
98
        return stat.S_ISDIR(self.mode)
110
99
 
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):
 
100
        
 
101
def top_directory(path):
120
102
    """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 ''
 
103
    dirname = os.path.dirname(path)
 
104
    last_dirname = dirname
 
105
    while True:
 
106
        dirname = os.path.dirname(dirname)
 
107
        if dirname == '' or dirname == last_dirname:
 
108
            return last_dirname
 
109
        last_dirname = dirname
126
110
 
127
111
 
128
112
def common_directory(names):
129
113
    """Determine a single directory prefix from a list of names"""
130
114
    possible_prefix = None
131
115
    for name in names:
132
 
        name_top = top_path(name)
 
116
        name_top = top_directory(name)
133
117
        if name_top == '':
134
118
            return None
135
119
        if possible_prefix is None:
162
146
            yield member.name
163
147
 
164
148
 
165
 
def should_ignore(relative_path):
166
 
    return top_path(relative_path) == '.bzr'
167
 
 
168
 
 
169
149
def import_tar(tree, tar_input):
170
150
    """Replace the contents of a working directory with tarfile contents.
171
151
    The tarfile may be a gzipped stream.  File ids will be updated.
181
161
    dir_file = DirWrapper(dir_input)
182
162
    import_archive(tree, dir_file)
183
163
 
184
 
 
185
164
def import_archive(tree, archive_file):
 
165
    prefix = common_directory(names_of_files(archive_file))
186
166
    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))
 
167
 
196
168
    removed = set()
197
169
    for path, entry in tree.inventory.iter_entries():
198
170
        if entry.parent_id is None:
201
173
        tt.delete_contents(trans_id)
202
174
        removed.add(path)
203
175
 
204
 
    added = set()
 
176
    added = set() 
205
177
    implied_parents = set()
206
178
    seen = set()
207
179
    for member in archive_file.getmembers():
208
180
        if member.type == 'g':
209
181
            # type 'g' is a header
210
182
            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')
 
183
        relative_path = member.name 
215
184
        if prefix is not None:
216
185
            relative_path = relative_path[len(prefix)+1:]
217
 
            relative_path = relative_path.rstrip('/')
218
186
        if relative_path == '':
219
187
            continue
220
 
        if should_ignore(relative_path):
221
 
            continue
222
188
        add_implied_parents(implied_parents, relative_path)
223
189
        trans_id = tt.trans_id_tree_path(relative_path)
224
190
        added.add(relative_path.rstrip('/'))
229
195
            tt.cancel_creation(trans_id)
230
196
        seen.add(member.name)
231
197
        if member.isreg():
232
 
            tt.create_file(file_iterator(archive_file.extractfile(member)),
 
198
            tt.create_file(file_iterator(archive_file.extractfile(member)), 
233
199
                           trans_id)
234
200
            executable = (member.mode & 0111) != 0
235
201
            tt.set_executability(executable, trans_id)
259
225
 
260
226
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
261
227
        warning(conflict)
 
228
    tt.apply()
262
229
 
263
230
 
264
231
def do_import(source, tree_directory=None):
278
245
        if tree.changes_from(tree.basis_tree()).has_changed():
279
246
            raise BzrCommandError("Working tree has uncommitted changes.")
280
247
 
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')):
 
248
        if (source.endswith('.tar') or source.endswith('.tar.gz') or 
 
249
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
284
250
            try:
285
 
                tar_input = open_from_url(source)
286
251
                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()))
 
252
                    tar_input = BZ2File(source, 'r')
 
253
                    tar_input = StringIO(tar_input.read())
 
254
                else:
 
255
                    tar_input = file(source, 'rb')
292
256
            except IOError, e:
293
257
                if e.errno == errno.ENOENT:
294
258
                    raise NoSuchFile(source)
297
261
            finally:
298
262
                tar_input.close()
299
263
        elif source.endswith('.zip'):
300
 
            import_zip(tree, open_from_url(source))
301
 
        elif file_kind(source) == 'directory':
302
 
            s = StringIO(source)
303
 
            s.seek(0)
304
 
            import_dir(tree, s)
 
264
            import_zip(tree, open(source, 'rb'))
305
265
        else:
306
266
            raise BzrCommandError('Unhandled import source')
307
267
    finally: