~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2005-07-25 14:17:15 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050725141715-d410836f6e4a32c0
Got baz2bzr/annotate working now that ProgressBar is a function

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Import upstream source into a branch"""
2
 
 
3
 
from bz2 import BZ2File
4
 
import errno
5
 
import os
6
 
from StringIO import StringIO
7
 
import stat
8
 
import tarfile
9
 
import zipfile
10
 
 
11
 
from bzrlib import generate_ids
12
 
from bzrlib.bzrdir import BzrDir
13
 
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
14
 
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
15
 
                            file_kind)
16
 
from bzrlib.trace import warning
17
 
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
18
 
from bzrlib.workingtree import WorkingTree
19
 
 
20
 
class ZipFileWrapper(object):
21
 
 
22
 
    def __init__(self, fileobj, mode):
23
 
        self.zipfile = zipfile.ZipFile(fileobj, mode)
24
 
 
25
 
    def getmembers(self):
26
 
        for info in self.zipfile.infolist():
27
 
            yield ZipInfoWrapper(self.zipfile, info)
28
 
 
29
 
    def extractfile(self, infowrapper):
30
 
        return StringIO(self.zipfile.read(infowrapper.name))
31
 
 
32
 
    def add(self, filename):
33
 
        if isdir(filename):
34
 
            self.zipfile.writestr(filename+'/', '')
35
 
        else:
36
 
            self.zipfile.write(filename)
37
 
 
38
 
    def close(self):
39
 
        self.zipfile.close()
40
 
 
41
 
 
42
 
class ZipInfoWrapper(object):
43
 
 
44
 
    def __init__(self, zipfile, info):
45
 
        self.info = info
46
 
        self.type = None
47
 
        self.name = info.filename
48
 
        self.zipfile = zipfile
49
 
        self.mode = 0666
50
 
 
51
 
    def isdir(self):
52
 
        # Really? Eeeew!
53
 
        return bool(self.name.endswith('/'))
54
 
 
55
 
    def isreg(self):
56
 
        # Really? Eeeew!
57
 
        return not self.isdir()
58
 
 
59
 
 
60
 
class DirWrapper(object):
61
 
    def __init__(self, fileobj, mode='r'):
62
 
        assert mode == 'r', mode
63
 
        self.root = os.path.realpath(fileobj.read())
64
 
 
65
 
    def __repr__(self):
66
 
        return 'DirWrapper(%r)' % self.root
67
 
 
68
 
    def getmembers(self, subdir=None):
69
 
        if subdir is not None:
70
 
            mydir = pathjoin(self.root, subdir)
71
 
        else:
72
 
            mydir = self.root
73
 
        for child in os.listdir(mydir):
74
 
            if subdir is not None:
75
 
                child = pathjoin(subdir, child)
76
 
            fi = FileInfo(self.root, child)
77
 
            yield fi
78
 
            if fi.isdir():
79
 
                for v in self.getmembers(child):
80
 
                    yield v
81
 
 
82
 
    def extractfile(self, member):
83
 
        return open(member.fullpath)
84
 
 
85
 
 
86
 
class FileInfo(object):
87
 
 
88
 
    def __init__(self, root, filepath):
89
 
        self.fullpath = pathjoin(root, filepath)
90
 
        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)
96
 
        self.type = None
97
 
        stat = os.lstat(self.fullpath)
98
 
        self.mode = stat.st_mode
99
 
        if self.isdir():
100
 
            self.name += '/'
101
 
 
102
 
    def __repr__(self):
103
 
        return 'FileInfo(%r)' % self.name
104
 
 
105
 
    def isreg(self):
106
 
        return stat.S_ISREG(self.mode)
107
 
 
108
 
    def isdir(self):
109
 
        return stat.S_ISDIR(self.mode)
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_directory(path):
120
 
    """Return the top directory given in a path."""
121
 
    dirname = os.path.dirname(path)
122
 
    last_dirname = dirname
123
 
    while True:
124
 
        dirname = os.path.dirname(dirname)
125
 
        if dirname == '' or dirname == last_dirname:
126
 
            return last_dirname
127
 
        last_dirname = dirname
128
 
 
129
 
 
130
 
def common_directory(names):
131
 
    """Determine a single directory prefix from a list of names"""
132
 
    possible_prefix = None
133
 
    for name in names:
134
 
        name_top = top_directory(name)
135
 
        if name_top == '':
136
 
            return None
137
 
        if possible_prefix is None:
138
 
            possible_prefix = name_top
139
 
        else:
140
 
            if name_top != possible_prefix:
141
 
                return None
142
 
    return possible_prefix
143
 
 
144
 
 
145
 
def do_directory(tt, trans_id, tree, relative_path, path):
146
 
    if isdir(path) and tree.path2id(relative_path) is not None:
147
 
        tt.cancel_deletion(trans_id)
148
 
    else:
149
 
        tt.create_directory(trans_id)
150
 
 
151
 
 
152
 
def add_implied_parents(implied_parents, path):
153
 
    """Update the set of implied parents from a path"""
154
 
    parent = os.path.dirname(path)
155
 
    if parent in implied_parents:
156
 
        return
157
 
    implied_parents.add(parent)
158
 
    add_implied_parents(implied_parents, parent)
159
 
 
160
 
 
161
 
def names_of_files(tar_file):
162
 
    for member in tar_file.getmembers():
163
 
        if member.type != "g":
164
 
            yield member.name
165
 
 
166
 
 
167
 
def import_tar(tree, tar_input):
168
 
    """Replace the contents of a working directory with tarfile contents.
169
 
    The tarfile may be a gzipped stream.  File ids will be updated.
170
 
    """
171
 
    tar_file = tarfile.open('lala', 'r', tar_input)
172
 
    import_archive(tree, tar_file)
173
 
 
174
 
def import_zip(tree, zip_input):
175
 
    zip_file = ZipFileWrapper(zip_input, 'r')
176
 
    import_archive(tree, zip_file)
177
 
 
178
 
def import_dir(tree, dir_input):
179
 
    dir_file = DirWrapper(dir_input)
180
 
    import_archive(tree, dir_file)
181
 
 
182
 
def import_archive(tree, archive_file):
183
 
    prefix = common_directory(names_of_files(archive_file))
184
 
    tt = TreeTransform(tree)
185
 
 
186
 
    removed = set()
187
 
    for path, entry in tree.inventory.iter_entries():
188
 
        if entry.parent_id is None:
189
 
            continue
190
 
        trans_id = tt.trans_id_tree_path(path)
191
 
        tt.delete_contents(trans_id)
192
 
        removed.add(path)
193
 
 
194
 
    added = set()
195
 
    implied_parents = set()
196
 
    seen = set()
197
 
    for member in archive_file.getmembers():
198
 
        if member.type == 'g':
199
 
            # type 'g' is a header
200
 
            continue
201
 
        relative_path = member.name
202
 
        if prefix is not None:
203
 
            relative_path = relative_path[len(prefix)+1:]
204
 
            relative_path = relative_path.rstrip('/')
205
 
        if relative_path == '':
206
 
            continue
207
 
        add_implied_parents(implied_parents, relative_path)
208
 
        trans_id = tt.trans_id_tree_path(relative_path)
209
 
        added.add(relative_path.rstrip('/'))
210
 
        path = tree.abspath(relative_path)
211
 
        if member.name in seen:
212
 
            if tt.final_kind(trans_id) == 'file':
213
 
                tt.set_executability(None, trans_id)
214
 
            tt.cancel_creation(trans_id)
215
 
        seen.add(member.name)
216
 
        if member.isreg():
217
 
            tt.create_file(file_iterator(archive_file.extractfile(member)),
218
 
                           trans_id)
219
 
            executable = (member.mode & 0111) != 0
220
 
            tt.set_executability(executable, trans_id)
221
 
        elif member.isdir():
222
 
            do_directory(tt, trans_id, tree, relative_path, path)
223
 
        elif member.issym():
224
 
            tt.create_symlink(member.linkname, trans_id)
225
 
        else:
226
 
            continue
227
 
        if tt.tree_file_id(trans_id) is None:
228
 
            name = basename(member.name.rstrip('/'))
229
 
            file_id = generate_ids.gen_file_id(name)
230
 
            tt.version_file(file_id, trans_id)
231
 
 
232
 
    for relative_path in implied_parents.difference(added):
233
 
        if relative_path == "":
234
 
            continue
235
 
        trans_id = tt.trans_id_tree_path(relative_path)
236
 
        path = tree.abspath(relative_path)
237
 
        do_directory(tt, trans_id, tree, relative_path, path)
238
 
        if tt.tree_file_id(trans_id) is None:
239
 
            tt.version_file(trans_id, trans_id)
240
 
        added.add(relative_path)
241
 
 
242
 
    for path in removed.difference(added):
243
 
        tt.unversion_file(tt.trans_id_tree_path(path))
244
 
 
245
 
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
246
 
        warning(conflict)
247
 
    tt.apply()
248
 
 
249
 
 
250
 
def do_import(source, tree_directory=None):
251
 
    """Implementation of import command.  Intended for UI only"""
252
 
    if tree_directory is not None:
253
 
        try:
254
 
            tree = WorkingTree.open(tree_directory)
255
 
        except NotBranchError:
256
 
            if not os.path.exists(tree_directory):
257
 
                os.mkdir(tree_directory)
258
 
            branch = BzrDir.create_branch_convenience(tree_directory)
259
 
            tree = branch.bzrdir.open_workingtree()
260
 
    else:
261
 
        tree = WorkingTree.open_containing('.')[0]
262
 
    tree.lock_write()
263
 
    try:
264
 
        if tree.changes_from(tree.basis_tree()).has_changed():
265
 
            raise BzrCommandError("Working tree has uncommitted changes.")
266
 
 
267
 
        if (source.endswith('.tar') or source.endswith('.tar.gz') or
268
 
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
269
 
            try:
270
 
                if source.endswith('.bz2'):
271
 
                    tar_input = BZ2File(source, 'r')
272
 
                    tar_input = StringIO(tar_input.read())
273
 
                else:
274
 
                    tar_input = file(source, 'rb')
275
 
            except IOError, e:
276
 
                if e.errno == errno.ENOENT:
277
 
                    raise NoSuchFile(source)
278
 
            try:
279
 
                import_tar(tree, tar_input)
280
 
            finally:
281
 
                tar_input.close()
282
 
        elif source.endswith('.zip'):
283
 
            import_zip(tree, open(source, 'rb'))
284
 
        elif file_kind(source) == 'directory':
285
 
            s = StringIO(source)
286
 
            s.seek(0)
287
 
            import_dir(tree, s)
288
 
        else:
289
 
            raise BzrCommandError('Unhandled import source')
290
 
    finally:
291
 
        tree.unlock()