~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""Import upstream source into a branch"""

import os
from shutil import rmtree
from StringIO import StringIO
import tarfile
from unittest import makeSuite

from bzrlib.bzrdir import BzrDir
from bzrlib.delta import compare_trees
from bzrlib.errors import NoSuchFile, BzrCommandError
from bzrlib.osutils import pathjoin, isdir
from bzrlib.tests import TestCaseInTempDir
from bzrlib.transform import TreeTransform
from bzrlib.workingtree import WorkingTree


def top_directory(path):
    """Return the top directory given in a path."""
    dirname = os.path.dirname(path)
    last_dirname = dirname
    while True:
        dirname = os.path.dirname(dirname)
        if dirname == '' or dirname == last_dirname:
            return last_dirname
        last_dirname = dirname


def common_directory(names):
    """Determine a single directory prefix from a list of names"""
    possible_prefix = None
    for name in names:
        name_top = top_directory(name)
        if possible_prefix is None:
            possible_prefix = name_top
        else:
            if name_top != possible_prefix:
                return None
    return possible_prefix


def import_tar(tree, tar_input):
    """Replace the contents of a working directory with tarfile contents.
    The tarfile may be a gzipped or bzipped stream.  File ids will be updated.
    """
    tar_file = tarfile.open('lala', 'r', tar_input)
    prefix = common_directory(tar_file.getnames())
    added = []
    tt = TreeTransform(tree)
    for path, entry in tree.inventory.iter_entries():
        trans_id = tt.trans_id_tree_path(path)
        tt.delete_contents(trans_id)

    for member in tar_file.getmembers():
        relative_path = member.name 
        if prefix is not None:
            relative_path = relative_path[len(prefix)+1:]
        if relative_path == '':
            continue
        trans_id = tt.trans_id_tree_path(relative_path)
        added.append(relative_path)
        path = tree.abspath(relative_path)
        if member.isreg():
            tt.create_file([tar_file.extractfile(member).read()], trans_id)
        elif member.isdir():
            if isdir(path):
                tt.cancel_deletion(trans_id)
            else:
                tt.create_directory(trans_id)
        elif member.issym():
            tt.create_symlink(member.linkname, trans_id)
    tt.apply()
    update_ids(tree, added)


def update_ids(tree, added):
    """Make sure that all present files files have file_ids.
    """
    # XXX detect renames
    for path in added:
        if tree.path2id(path) is None:
            tree.add(path)


def do_import(source):
    """Implementation of import command.  Intended for UI only"""
    tree = WorkingTree.open_containing('.')[0]
    tree.lock_write()
    try:
        if compare_trees(tree, tree.basis_tree()).has_changed():
            raise BzrCommandError("Working tree has uncommitted changes.")

        if (source.endswith('.tar') or source.endswith('.tar.gz') or 
            source.endswith('.tar.bz2')):
            try:
                tar_input = file(source, 'rb')
            except:
                if e.errno == errno.ENOENT:
                    raise NoSuchFile(source)
            try:
                import_tar(tree, tar_input)
            finally:
                tar_input.close()
    finally:
        tree.unlock()

class TestImport(TestCaseInTempDir):

    def make_tar(self):
        result = StringIO()
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
        os.mkdir('project-0.1')
        tar_file.add('project-0.1')
        os.mkdir('project-0.1/junk')
        tar_file.add('project-0.1/junk')
        
        f = file('project-0.1/README', 'wb')
        f.write('What?')
        f.close()
        tar_file.add('project-0.1/README')

        f = file('project-0.1/FEEDME', 'wb')
        f.write('Hungry!!')
        f.close()
        tar_file.add('project-0.1/FEEDME')

        tar_file.close()
        rmtree('project-0.1')
        result.seek(0)
        return result

    def make_tar2(self):
        result = StringIO()
        tar_file = tarfile.open('project-0.2.tar', 'w', result)
        os.mkdir('project-0.2')
        tar_file.add('project-0.2')
        
        os.mkdir('project-0.2/junk')
        tar_file.add('project-0.2/junk')

        f = file('project-0.2/README', 'wb')
        f.write('Now?')
        f.close()
        tar_file.add('project-0.2/README')

        tar_file.add('project-0.2/README')

        tar_file.close()
        rmtree('project-0.2')
        result.seek(0)
        return result

    def make_messed_tar(self):
        result = StringIO()
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
        os.mkdir('project-0.1')
        tar_file.add('project-0.1')

        os.mkdir('project-0.2')
        tar_file.add('project-0.2')
        
        f = file('project-0.1/README', 'wb')
        f.write('What?')
        f.close()
        tar_file.add('project-0.1/README')
        tar_file.close()
        rmtree('project-0.1')
        result.seek(0)
        return result

    def test_top_directory(self):
        self.assertEqual(top_directory('ab/b/c'), 'ab')
        self.assertEqual(top_directory('/etc'), '/')

    def test_common_directory(self):
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)

    def test_untar(self):
        tar_file = self.make_tar()
        tree = BzrDir.create_standalone_workingtree('tree')
        import_tar(tree, tar_file)
        self.assertTrue(tree.path2id('README') is not None) 
        self.assertTrue(tree.path2id('FEEDME') is not None)
        self.assertTrue(os.path.isfile(tree.abspath('README')))
        self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
        self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
        
        f = file(tree.abspath('junk/food'), 'wb')
        f.write('I like food\n')
        f.close()

        tar_file = self.make_tar2()
        import_tar(tree, tar_file)
        self.assertTrue(tree.path2id('README') is not None) 
        self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))


    def test_untar2(self):
        tar_file = self.make_messed_tar()
        tree = BzrDir.create_standalone_workingtree('tree')
        import_tar(tree, tar_file)
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 


def test_suite():
    return makeSuite(TestImport)