~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-07-11 18:56:01 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060711185601-6e7f164148ed616d
Remove <BZRTOOLS> tag from command descriptions

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
from bz2 import BZ2File
4
4
import errno
5
5
import os
 
6
from shutil import rmtree
6
7
from StringIO import StringIO
7
 
import stat
8
8
import tarfile
9
 
import zipfile
 
9
from unittest import makeSuite
10
10
 
11
 
from bzrlib import generate_ids
12
11
from bzrlib.bzrdir import BzrDir
 
12
from bzrlib.delta import compare_trees
13
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
14
 
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
15
 
                            file_kind, splitpath)
 
14
from bzrlib.osutils import pathjoin, isdir, file_iterator
 
15
from bzrlib.tests import TestCaseInTempDir
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
 
 
21
 
class ZipFileWrapper(object):
22
 
 
23
 
    def __init__(self, fileobj, mode):
24
 
        self.zipfile = zipfile.ZipFile(fileobj, mode)
25
 
 
26
 
    def getmembers(self):
27
 
        for info in self.zipfile.infolist():
28
 
            yield ZipInfoWrapper(self.zipfile, info)
29
 
 
30
 
    def extractfile(self, infowrapper):
31
 
        return StringIO(self.zipfile.read(infowrapper.name))
32
 
 
33
 
    def add(self, filename):
34
 
        if isdir(filename):
35
 
            self.zipfile.writestr(filename+'/', '')
36
 
        else:
37
 
            self.zipfile.write(filename)
38
 
 
39
 
    def close(self):
40
 
        self.zipfile.close()
41
 
 
42
 
 
43
 
class ZipInfoWrapper(object):
44
 
 
45
 
    def __init__(self, zipfile, info):
46
 
        self.info = info
47
 
        self.type = None
48
 
        self.name = info.filename
49
 
        self.zipfile = zipfile
50
 
        self.mode = 0666
51
 
 
52
 
    def isdir(self):
53
 
        # Really? Eeeew!
54
 
        return bool(self.name.endswith('/'))
55
 
 
56
 
    def isreg(self):
57
 
        # Really? Eeeew!
58
 
        return not self.isdir()
59
 
 
60
 
 
61
 
class DirWrapper(object):
62
 
    def __init__(self, fileobj, mode='r'):
63
 
        assert mode == 'r', mode
64
 
        self.root = os.path.realpath(fileobj.read())
65
 
 
66
 
    def __repr__(self):
67
 
        return 'DirWrapper(%r)' % self.root
68
 
 
69
 
    def getmembers(self, subdir=None):
70
 
        if subdir is not None:
71
 
            mydir = pathjoin(self.root, subdir)
72
 
        else:
73
 
            mydir = self.root
74
 
        for child in os.listdir(mydir):
75
 
            if subdir is not None:
76
 
                child = pathjoin(subdir, child)
77
 
            fi = FileInfo(self.root, child)
78
 
            yield fi
79
 
            if fi.isdir():
80
 
                for v in self.getmembers(child):
81
 
                    yield v
82
 
 
83
 
    def extractfile(self, member):
84
 
        return open(member.fullpath)
85
 
 
86
 
 
87
 
class FileInfo(object):
88
 
 
89
 
    def __init__(self, root, filepath):
90
 
        self.fullpath = pathjoin(root, filepath)
91
 
        self.root = root
92
 
        if filepath != '':
93
 
            self.name = pathjoin(basename(root), filepath)
94
 
        else:
95
 
            print 'root %r' % root
96
 
            self.name = basename(root)
97
 
        self.type = None
98
 
        stat = os.lstat(self.fullpath)
99
 
        self.mode = stat.st_mode
100
 
        if self.isdir():
101
 
            self.name += '/'
102
 
 
103
 
    def __repr__(self):
104
 
        return 'FileInfo(%r)' % self.name
105
 
 
106
 
    def isreg(self):
107
 
        return stat.S_ISREG(self.mode)
108
 
 
109
 
    def isdir(self):
110
 
        return stat.S_ISDIR(self.mode)
111
 
 
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):
 
19
 
 
20
 
 
21
def top_directory(path):
121
22
    """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 ''
 
23
    dirname = os.path.dirname(path)
 
24
    last_dirname = dirname
 
25
    while True:
 
26
        dirname = os.path.dirname(dirname)
 
27
        if dirname == '' or dirname == last_dirname:
 
28
            return last_dirname
 
29
        last_dirname = dirname
127
30
 
128
31
 
129
32
def common_directory(names):
130
33
    """Determine a single directory prefix from a list of names"""
131
34
    possible_prefix = None
132
35
    for name in names:
133
 
        name_top = top_path(name)
134
 
        if name_top == '':
135
 
            return None
 
36
        name_top = top_directory(name)
136
37
        if possible_prefix is None:
137
38
            possible_prefix = name_top
138
39
        else:
163
64
            yield member.name
164
65
 
165
66
 
166
 
def should_ignore(relative_path):
167
 
    return top_path(relative_path) == '.bzr'
168
 
 
169
 
 
170
67
def import_tar(tree, tar_input):
171
68
    """Replace the contents of a working directory with tarfile contents.
172
69
    The tarfile may be a gzipped stream.  File ids will be updated.
173
70
    """
174
71
    tar_file = tarfile.open('lala', 'r', tar_input)
175
 
    import_archive(tree, tar_file)
176
 
 
177
 
def import_zip(tree, zip_input):
178
 
    zip_file = ZipFileWrapper(zip_input, 'r')
179
 
    import_archive(tree, zip_file)
180
 
 
181
 
def import_dir(tree, dir_input):
182
 
    dir_file = DirWrapper(dir_input)
183
 
    import_archive(tree, dir_file)
184
 
 
185
 
def import_archive(tree, archive_file):
186
 
    prefix = common_directory(names_of_files(archive_file))
 
72
    prefix = common_directory(names_of_files(tar_file))
187
73
    tt = TreeTransform(tree)
188
74
 
189
75
    removed = set()
190
76
    for path, entry in tree.inventory.iter_entries():
191
 
        if entry.parent_id is None:
192
 
            continue
193
77
        trans_id = tt.trans_id_tree_path(path)
194
78
        tt.delete_contents(trans_id)
195
79
        removed.add(path)
196
80
 
197
 
    added = set()
 
81
    added = set() 
198
82
    implied_parents = set()
199
83
    seen = set()
200
 
    for member in archive_file.getmembers():
 
84
    for member in tar_file.getmembers():
201
85
        if member.type == 'g':
202
86
            # type 'g' is a header
203
87
            continue
204
 
        relative_path = member.name
 
88
        relative_path = member.name 
205
89
        if prefix is not None:
206
90
            relative_path = relative_path[len(prefix)+1:]
207
 
            relative_path = relative_path.rstrip('/')
208
91
        if relative_path == '':
209
92
            continue
210
 
        if should_ignore(relative_path):
211
 
            continue
212
93
        add_implied_parents(implied_parents, relative_path)
213
94
        trans_id = tt.trans_id_tree_path(relative_path)
214
95
        added.add(relative_path.rstrip('/'))
215
96
        path = tree.abspath(relative_path)
216
97
        if member.name in seen:
217
 
            if tt.final_kind(trans_id) == 'file':
218
 
                tt.set_executability(None, trans_id)
219
98
            tt.cancel_creation(trans_id)
220
99
        seen.add(member.name)
221
100
        if member.isreg():
222
 
            tt.create_file(file_iterator(archive_file.extractfile(member)),
 
101
            tt.create_file(file_iterator(tar_file.extractfile(member)), 
223
102
                           trans_id)
224
 
            executable = (member.mode & 0111) != 0
225
 
            tt.set_executability(executable, trans_id)
226
103
        elif member.isdir():
227
104
            do_directory(tt, trans_id, tree, relative_path, path)
228
105
        elif member.issym():
229
106
            tt.create_symlink(member.linkname, trans_id)
230
 
        else:
231
 
            continue
232
 
        if tt.tree_file_id(trans_id) is None:
233
 
            name = basename(member.name.rstrip('/'))
234
 
            file_id = generate_ids.gen_file_id(name)
235
 
            tt.version_file(file_id, trans_id)
236
107
 
237
108
    for relative_path in implied_parents.difference(added):
238
109
        if relative_path == "":
240
111
        trans_id = tt.trans_id_tree_path(relative_path)
241
112
        path = tree.abspath(relative_path)
242
113
        do_directory(tt, trans_id, tree, relative_path, path)
243
 
        if tt.tree_file_id(trans_id) is None:
244
 
            tt.version_file(trans_id, trans_id)
245
114
        added.add(relative_path)
246
115
 
247
 
    for path in removed.difference(added):
248
 
        tt.unversion_file(tt.trans_id_tree_path(path))
249
 
 
250
116
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
251
117
        warning(conflict)
252
118
    tt.apply()
 
119
    update_ids(tree, added, removed)
 
120
 
 
121
 
 
122
def update_ids(tree, added, removed):
 
123
    """Make sure that all present files files have file_ids.
 
124
    """
 
125
    # XXX detect renames
 
126
    new = added.difference(removed)
 
127
    deleted = removed.difference(added)
 
128
    tree.add(sorted(new))
 
129
    tree.remove(sorted(deleted, reverse=True))
253
130
 
254
131
 
255
132
def do_import(source, tree_directory=None):
266
143
        tree = WorkingTree.open_containing('.')[0]
267
144
    tree.lock_write()
268
145
    try:
269
 
        if tree.changes_from(tree.basis_tree()).has_changed():
 
146
        if compare_trees(tree, tree.basis_tree()).has_changed():
270
147
            raise BzrCommandError("Working tree has uncommitted changes.")
271
148
 
272
 
        if (source.endswith('.tar') or source.endswith('.tar.gz') or
 
149
        if (source.endswith('.tar') or source.endswith('.tar.gz') or 
273
150
            source.endswith('.tar.bz2')) or source.endswith('.tgz'):
274
151
            try:
275
 
                tar_input = open_from_url(source)
276
152
                if source.endswith('.bz2'):
277
 
                    tar_input = StringIO(tar_input.read().decode('bz2'))
 
153
                    tar_input = BZ2File(source, 'r')
 
154
                    tar_input = StringIO(tar_input.read())
 
155
                else:
 
156
                    tar_input = file(source, 'rb')
278
157
            except IOError, e:
279
158
                if e.errno == errno.ENOENT:
280
159
                    raise NoSuchFile(source)
282
161
                import_tar(tree, tar_input)
283
162
            finally:
284
163
                tar_input.close()
285
 
        elif source.endswith('.zip'):
286
 
            import_zip(tree, open_from_url(source))
287
 
        elif file_kind(source) == 'directory':
288
 
            s = StringIO(source)
289
 
            s.seek(0)
290
 
            import_dir(tree, s)
291
 
        else:
292
 
            raise BzrCommandError('Unhandled import source')
293
164
    finally:
294
165
        tree.unlock()
 
166
 
 
167
class TestImport(TestCaseInTempDir):
 
168
 
 
169
    def make_tar(self, mode='w'):
 
170
        result = StringIO()
 
171
        tar_file = tarfile.open('project-0.1.tar', mode, result)
 
172
        os.mkdir('project-0.1')
 
173
        tar_file.add('project-0.1')
 
174
        os.mkdir('project-0.1/junk')
 
175
        tar_file.add('project-0.1/junk')
 
176
        
 
177
        f = file('project-0.1/README', 'wb')
 
178
        f.write('What?')
 
179
        f.close()
 
180
        tar_file.add('project-0.1/README')
 
181
 
 
182
        f = file('project-0.1/FEEDME', 'wb')
 
183
        f.write('Hungry!!')
 
184
        f.close()
 
185
        tar_file.add('project-0.1/FEEDME')
 
186
 
 
187
        tar_file.close()
 
188
        rmtree('project-0.1')
 
189
        result.seek(0)
 
190
        return result
 
191
 
 
192
    def make_tar2(self):
 
193
        result = StringIO()
 
194
        tar_file = tarfile.open('project-0.2.tar', 'w', result)
 
195
        os.mkdir('project-0.2')
 
196
        tar_file.add('project-0.2')
 
197
        
 
198
        os.mkdir('project-0.2/junk')
 
199
        tar_file.add('project-0.2/junk')
 
200
 
 
201
        f = file('project-0.2/README', 'wb')
 
202
        f.write('Now?')
 
203
        f.close()
 
204
        tar_file.add('project-0.2/README')
 
205
        tar_file.close()
 
206
 
 
207
        tar_file = tarfile.open('project-0.2.tar', 'a', result)
 
208
        tar_file.add('project-0.2/README')
 
209
 
 
210
        rmtree('project-0.2')
 
211
        return result
 
212
 
 
213
    def make_messed_tar(self):
 
214
        result = StringIO()
 
215
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
 
216
        os.mkdir('project-0.1')
 
217
        tar_file.add('project-0.1')
 
218
 
 
219
        os.mkdir('project-0.2')
 
220
        tar_file.add('project-0.2')
 
221
        
 
222
        f = file('project-0.1/README', 'wb')
 
223
        f.write('What?')
 
224
        f.close()
 
225
        tar_file.add('project-0.1/README')
 
226
        tar_file.close()
 
227
        rmtree('project-0.1')
 
228
        result.seek(0)
 
229
        return result
 
230
 
 
231
    def test_top_directory(self):
 
232
        self.assertEqual(top_directory('ab/b/c'), 'ab')
 
233
        self.assertEqual(top_directory('/etc'), '/')
 
234
 
 
235
    def test_common_directory(self):
 
236
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
 
237
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
 
238
 
 
239
    def test_untar(self):
 
240
        tar_file = self.make_tar()
 
241
        tree = BzrDir.create_standalone_workingtree('tree')
 
242
        import_tar(tree, tar_file)
 
243
        self.assertTrue(tree.path2id('README') is not None) 
 
244
        self.assertTrue(tree.path2id('FEEDME') is not None)
 
245
        self.assertTrue(os.path.isfile(tree.abspath('README')))
 
246
        self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
 
247
        self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
 
248
        
 
249
        f = file(tree.abspath('junk/food'), 'wb')
 
250
        f.write('I like food\n')
 
251
        f.close()
 
252
 
 
253
        tar_file = self.make_tar2()
 
254
        import_tar(tree, tar_file)
 
255
        self.assertTrue(tree.path2id('README') is not None) 
 
256
        self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
 
257
 
 
258
 
 
259
    def test_untar2(self):
 
260
        tar_file = self.make_messed_tar()
 
261
        tree = BzrDir.create_standalone_workingtree('tree')
 
262
        import_tar(tree, tar_file)
 
263
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
 
264
 
 
265
    def test_untar_gzip(self):
 
266
        tar_file = self.make_tar(mode='w:gz')
 
267
        tree = BzrDir.create_standalone_workingtree('tree')
 
268
        import_tar(tree, tar_file)
 
269
        self.assertTrue(tree.path2id('README') is not None) 
 
270
 
 
271
 
 
272
def test_suite():
 
273
    return makeSuite(TestImport)