~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-05-18 14:19:48 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060518141948-7c5c473382991df5
Got import command working

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from unittest import makeSuite
8
8
 
9
9
from bzrlib.bzrdir import BzrDir
 
10
from bzrlib.errors import NoSuchFile
10
11
from bzrlib.osutils import pathjoin
11
12
from bzrlib.tests import TestCaseInTempDir
12
13
from bzrlib.transform import TreeTransform
 
14
from bzrlib.workingtree import WorkingTree
 
15
 
13
16
 
14
17
def top_directory(path):
 
18
    """Return the top directory given in a path."""
15
19
    dirname = os.path.dirname(path)
16
20
    last_dirname = dirname
17
21
    while True:
35
39
 
36
40
 
37
41
def import_tar(tree, tar_input):
 
42
    """Replace the contents of a working directory with tarfile contents.
 
43
    The tarfile may be a gzipped or bzipped stream.  File ids will be updated.
 
44
    """
38
45
    tar_file = tarfile.open('lala', 'r', tar_input)
39
46
    prefix = common_directory(tar_file.getnames())
40
47
    added = []
59
66
        elif member.issym():
60
67
            tt.create_symlink(member.linkname, trans_id)
61
68
    tt.apply()
 
69
    update_ids(tree, added)
 
70
 
 
71
 
 
72
def update_ids(tree, added):
 
73
    """Make sure that all present files files have file_ids.
 
74
    """
 
75
    # XXX detect renames
62
76
    for path in added:
63
77
        if tree.path2id(path) is None:
64
78
            tree.add(path)
65
79
 
 
80
 
 
81
def do_import(source):
 
82
    """Implementation of import command.  Intended for UI only"""
 
83
    tree = WorkingTree.open_containing('.')[0]
 
84
    tree.lock_write()
 
85
    try:
 
86
        if (source.endswith('.tar') or source.endswith('.tar.gz') or 
 
87
            source.endswith('.tar.bz2')):
 
88
            try:
 
89
                tar_input = file(source, 'rb')
 
90
            except:
 
91
                if e.errno == errno.ENOENT:
 
92
                    raise NoSuchFile(source)
 
93
            try:
 
94
                import_tar(tree, tar_input)
 
95
            finally:
 
96
                tar_input.close()
 
97
    finally:
 
98
        tree.unlock()
 
99
 
66
100
class TestImport(TestCaseInTempDir):
67
101
 
68
102
    def make_tar(self):
152
186
        import_tar(tree, tar_file)
153
187
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
154
188
 
 
189
 
155
190
def test_suite():
156
191
    return makeSuite(TestImport)