~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-05-17 22:02:34 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060517220234-51a588052d24b7d0
Start work on import plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Import upstream source into a branch"""
 
2
 
 
3
import os
 
4
from shutil import rmtree
 
5
from StringIO import StringIO
 
6
import tarfile
 
7
from unittest import makeSuite
 
8
 
 
9
from bzrlib.bzrdir import BzrDir
 
10
from bzrlib.osutils import pathjoin
 
11
from bzrlib.tests import TestCaseInTempDir
 
12
 
 
13
def top_directory(path):
 
14
    dirname = os.path.dirname(path)
 
15
    last_dirname = dirname
 
16
    while True:
 
17
        dirname = os.path.dirname(dirname)
 
18
        if dirname == '' or dirname == last_dirname:
 
19
            return last_dirname
 
20
        last_dirname = dirname
 
21
 
 
22
 
 
23
def common_directory(names):
 
24
    """Determine a single directory prefix from a list of names"""
 
25
    possible_prefix = None
 
26
    for name in names:
 
27
        name_top = top_directory(name)
 
28
        if possible_prefix is None:
 
29
            possible_prefix = name_top
 
30
        else:
 
31
            if name_top != possible_prefix:
 
32
                return None
 
33
    return possible_prefix
 
34
 
 
35
 
 
36
def import_tar(tree, tar_input):
 
37
    tar_file = tarfile.open('lala', 'r', tar_input)
 
38
    prefix = common_directory(tar_file.getnames())
 
39
    added = []
 
40
    for name in tar_file.getnames():
 
41
        relative_path = name
 
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))
 
46
 
 
47
    for path in added:
 
48
        if tree.path2id(path) is None:
 
49
            tree.add(path)
 
50
 
 
51
class TestImport(TestCaseInTempDir):
 
52
 
 
53
    def make_tar(self):
 
54
        result = StringIO()
 
55
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
 
56
        os.mkdir('project-0.1')
 
57
        tar_file.add('project-0.1')
 
58
        
 
59
        f = file('project-0.1/README', 'wb')
 
60
        f.write('What?')
 
61
        f.close()
 
62
        tar_file.add('project-0.1/README')
 
63
 
 
64
        f = file('project-0.1/FEEDME', 'wb')
 
65
        f.write('Hungry!!')
 
66
        f.close()
 
67
        tar_file.add('project-0.1/FEEDME')
 
68
 
 
69
        tar_file.close()
 
70
        rmtree('project-0.1')
 
71
        result.seek(0)
 
72
        return result
 
73
 
 
74
    def make_tar2(self):
 
75
        result = StringIO()
 
76
        tar_file = tarfile.open('project-0.2.tar', 'w', result)
 
77
        os.mkdir('project-0.2')
 
78
        tar_file.add('project-0.2')
 
79
        
 
80
        f = file('project-0.2/README', 'wb')
 
81
        f.write('Now?')
 
82
        f.close()
 
83
        tar_file.add('project-0.2/README')
 
84
 
 
85
        tar_file.add('project-0.2/README')
 
86
 
 
87
        tar_file.close()
 
88
        rmtree('project-0.2')
 
89
        result.seek(0)
 
90
        return result
 
91
 
 
92
    def make_messed_tar(self):
 
93
        result = StringIO()
 
94
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
 
95
        os.mkdir('project-0.1')
 
96
        tar_file.add('project-0.1')
 
97
 
 
98
        os.mkdir('project-0.2')
 
99
        tar_file.add('project-0.2')
 
100
        
 
101
        f = file('project-0.1/README', 'wb')
 
102
        f.write('What?')
 
103
        f.close()
 
104
        tar_file.add('project-0.1/README')
 
105
        tar_file.close()
 
106
        rmtree('project-0.1')
 
107
        result.seek(0)
 
108
        return result
 
109
 
 
110
    def test_top_directory(self):
 
111
        self.assertEqual(top_directory('ab/b/c'), 'ab')
 
112
        self.assertEqual(top_directory('/etc'), '/')
 
113
 
 
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)
 
117
 
 
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)
 
124
        
 
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)
 
129
 
 
130
 
 
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) 
 
136
 
 
137
def test_suite():
 
138
    return makeSuite(TestImport)