~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-05-18 05:18:44 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060518051844-f5668ee58f66153e
Correctly extract tarfiles

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
from bzrlib.bzrdir import BzrDir
10
10
from bzrlib.osutils import pathjoin
11
11
from bzrlib.tests import TestCaseInTempDir
 
12
from bzrlib.transform import TreeTransform
12
13
 
13
14
def top_directory(path):
14
15
    dirname = os.path.dirname(path)
37
38
    tar_file = tarfile.open('lala', 'r', tar_input)
38
39
    prefix = common_directory(tar_file.getnames())
39
40
    added = []
40
 
    for name in tar_file.getnames():
41
 
        relative_path = name
 
41
    print sorted(tree.inventory.iter_entries())
 
42
    tt = TreeTransform(tree)
 
43
    for path, entry in tree.inventory.iter_entries():
 
44
        trans_id = tt.trans_id_tree_path(path)
 
45
        tt.delete_contents(trans_id)
 
46
 
 
47
    for member in tar_file.getmembers():
 
48
        relative_path = member.name 
42
49
        if prefix is not None:
43
50
            relative_path = relative_path[len(prefix)+1:]
 
51
        if relative_path == '':
 
52
            continue
 
53
        trans_id = tt.trans_id_tree_path(relative_path)
44
54
        added.append(relative_path)
45
 
        tar_file.extract(name, tree.abspath(relative_path))
46
 
 
 
55
        path = tree.abspath(relative_path)
 
56
        if member.isreg():
 
57
            tt.create_file([tar_file.extractfile(member).read()], trans_id)
 
58
        elif member.isdir():
 
59
            tt.create_directory(trans_id)
 
60
        elif member.issym():
 
61
            tt.create_symlink(member.linkname, trans_id)
 
62
    tt.apply()
47
63
    for path in added:
48
64
        if tree.path2id(path) is None:
49
65
            tree.add(path)
121
137
        import_tar(tree, tar_file)
122
138
        self.assertTrue(tree.path2id('README') is not None) 
123
139
        self.assertTrue(tree.path2id('FEEDME') is not None)
 
140
        self.assertTrue(os.path.isfile(tree.abspath('README')))
 
141
        self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
 
142
        self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
124
143
        
125
144
        tar_file = self.make_tar2()
126
145
        import_tar(tree, tar_file)
127
146
        self.assertTrue(tree.path2id('README') is not None) 
128
 
        self.assertTrue(tree.path2id('FEEDME') is None)
 
147
        self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
129
148
 
130
149
 
131
150
    def test_untar2(self):