1
"""Import upstream source into a branch"""
4
from shutil import rmtree
5
from StringIO import StringIO
7
from unittest import makeSuite
9
from bzrlib.bzrdir import BzrDir
10
from bzrlib.osutils import pathjoin
11
from bzrlib.tests import TestCaseInTempDir
13
def top_directory(path):
14
dirname = os.path.dirname(path)
15
last_dirname = dirname
17
dirname = os.path.dirname(dirname)
18
if dirname == '' or dirname == last_dirname:
20
last_dirname = dirname
23
def common_directory(names):
24
"""Determine a single directory prefix from a list of names"""
25
possible_prefix = None
27
name_top = top_directory(name)
28
if possible_prefix is None:
29
possible_prefix = name_top
31
if name_top != possible_prefix:
33
return possible_prefix
36
def import_tar(tree, tar_input):
37
tar_file = tarfile.open('lala', 'r', tar_input)
38
prefix = common_directory(tar_file.getnames())
40
for name in tar_file.getnames():
42
if prefix is not None:
43
relative_path = relative_path[len(prefix)+1:]
44
added.append(relative_path)
45
tar_file.extract(name, tree.abspath(relative_path))
48
if tree.path2id(path) is None:
51
class TestImport(TestCaseInTempDir):
55
tar_file = tarfile.open('project-0.1.tar', 'w', result)
56
os.mkdir('project-0.1')
57
tar_file.add('project-0.1')
59
f = file('project-0.1/README', 'wb')
62
tar_file.add('project-0.1/README')
64
f = file('project-0.1/FEEDME', 'wb')
67
tar_file.add('project-0.1/FEEDME')
76
tar_file = tarfile.open('project-0.2.tar', 'w', result)
77
os.mkdir('project-0.2')
78
tar_file.add('project-0.2')
80
f = file('project-0.2/README', 'wb')
83
tar_file.add('project-0.2/README')
85
tar_file.add('project-0.2/README')
92
def make_messed_tar(self):
94
tar_file = tarfile.open('project-0.1.tar', 'w', result)
95
os.mkdir('project-0.1')
96
tar_file.add('project-0.1')
98
os.mkdir('project-0.2')
99
tar_file.add('project-0.2')
101
f = file('project-0.1/README', 'wb')
104
tar_file.add('project-0.1/README')
106
rmtree('project-0.1')
110
def test_top_directory(self):
111
self.assertEqual(top_directory('ab/b/c'), 'ab')
112
self.assertEqual(top_directory('/etc'), '/')
114
def test_common_directory(self):
115
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
116
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
118
def test_untar(self):
119
tar_file = self.make_tar()
120
tree = BzrDir.create_standalone_workingtree('tree')
121
import_tar(tree, tar_file)
122
self.assertTrue(tree.path2id('README') is not None)
123
self.assertTrue(tree.path2id('FEEDME') is not None)
125
tar_file = self.make_tar2()
126
import_tar(tree, tar_file)
127
self.assertTrue(tree.path2id('README') is not None)
128
self.assertTrue(tree.path2id('FEEDME') is None)
131
def test_untar2(self):
132
tar_file = self.make_messed_tar()
133
tree = BzrDir.create_standalone_workingtree('tree')
134
import_tar(tree, tar_file)
135
self.assertTrue(tree.path2id('project-0.1/README') is not None)
138
return makeSuite(TestImport)