3
3
from bz2 import BZ2File
6
from shutil import rmtree
6
7
from StringIO import StringIO
9
from unittest import makeSuite
11
from bzrlib import generate_ids
12
11
from bzrlib.bzrdir import BzrDir
12
from bzrlib.delta import compare_trees
13
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
14
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
14
from bzrlib.osutils import pathjoin, isdir, file_iterator
15
from bzrlib.tests import TestCaseInTempDir
16
16
from bzrlib.trace import warning
17
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
18
18
from bzrlib.workingtree import WorkingTree
20
class ZipFileWrapper(object):
22
def __init__(self, fileobj, mode):
23
self.zipfile = zipfile.ZipFile(fileobj, mode)
26
for info in self.zipfile.infolist():
27
yield ZipInfoWrapper(self.zipfile, info)
29
def extractfile(self, infowrapper):
30
return StringIO(self.zipfile.read(infowrapper.name))
32
def add(self, filename):
34
self.zipfile.writestr(filename+'/', '')
36
self.zipfile.write(filename)
42
class ZipInfoWrapper(object):
44
def __init__(self, zipfile, info):
47
self.name = info.filename
48
self.zipfile = zipfile
53
return bool(self.name.endswith('/'))
57
return not self.isdir()
60
class DirWrapper(object):
61
def __init__(self, fileobj, mode='r'):
62
assert mode == 'r', mode
63
self.root = os.path.realpath(fileobj.read())
66
return 'DirWrapper(%r)' % self.root
68
def getmembers(self, subdir=None):
69
if subdir is not None:
70
mydir = pathjoin(self.root, subdir)
73
for child in os.listdir(mydir):
74
if subdir is not None:
75
child = pathjoin(subdir, child)
76
fi = FileInfo(self.root, child)
79
for v in self.getmembers(child):
82
def extractfile(self, member):
83
return open(member.fullpath)
86
class FileInfo(object):
88
def __init__(self, root, filepath):
89
self.fullpath = pathjoin(root, filepath)
92
self.name = pathjoin(basename(root), filepath)
94
print 'root %r' % root
95
self.name = basename(root)
97
stat = os.lstat(self.fullpath)
98
self.mode = stat.st_mode
103
return 'FileInfo(%r)' % self.name
106
return stat.S_ISREG(self.mode)
109
return stat.S_ISDIR(self.mode)
112
if stat.S_ISLNK(self.mode):
113
self.linkname = os.readlink(self.fullpath)
119
21
def top_directory(path):
120
22
"""Return the top directory given in a path."""
169
69
The tarfile may be a gzipped stream. File ids will be updated.
171
71
tar_file = tarfile.open('lala', 'r', tar_input)
172
import_archive(tree, tar_file)
174
def import_zip(tree, zip_input):
175
zip_file = ZipFileWrapper(zip_input, 'r')
176
import_archive(tree, zip_file)
178
def import_dir(tree, dir_input):
179
dir_file = DirWrapper(dir_input)
180
import_archive(tree, dir_file)
182
def import_archive(tree, archive_file):
183
prefix = common_directory(names_of_files(archive_file))
72
prefix = common_directory(names_of_files(tar_file))
184
73
tt = TreeTransform(tree)
187
76
for path, entry in tree.inventory.iter_entries():
188
if entry.parent_id is None:
190
77
trans_id = tt.trans_id_tree_path(path)
191
78
tt.delete_contents(trans_id)
195
82
implied_parents = set()
197
for member in archive_file.getmembers():
84
for member in tar_file.getmembers():
198
85
if member.type == 'g':
199
86
# type 'g' is a header
201
relative_path = member.name
88
relative_path = member.name
202
89
if prefix is not None:
203
90
relative_path = relative_path[len(prefix)+1:]
204
relative_path = relative_path.rstrip('/')
205
91
if relative_path == '':
207
93
add_implied_parents(implied_parents, relative_path)
209
95
added.add(relative_path.rstrip('/'))
210
96
path = tree.abspath(relative_path)
211
97
if member.name in seen:
212
if tt.final_kind(trans_id) == 'file':
213
tt.set_executability(None, trans_id)
214
98
tt.cancel_creation(trans_id)
215
99
seen.add(member.name)
216
100
if member.isreg():
217
tt.create_file(file_iterator(archive_file.extractfile(member)),
101
tt.create_file(file_iterator(tar_file.extractfile(member)),
219
executable = (member.mode & 0111) != 0
220
tt.set_executability(executable, trans_id)
221
103
elif member.isdir():
222
104
do_directory(tt, trans_id, tree, relative_path, path)
223
105
elif member.issym():
224
106
tt.create_symlink(member.linkname, trans_id)
227
if tt.tree_file_id(trans_id) is None:
228
name = basename(member.name.rstrip('/'))
229
file_id = generate_ids.gen_file_id(name)
230
tt.version_file(file_id, trans_id)
232
108
for relative_path in implied_parents.difference(added):
233
109
if relative_path == "":
235
111
trans_id = tt.trans_id_tree_path(relative_path)
236
112
path = tree.abspath(relative_path)
237
113
do_directory(tt, trans_id, tree, relative_path, path)
238
if tt.tree_file_id(trans_id) is None:
239
tt.version_file(trans_id, trans_id)
240
114
added.add(relative_path)
242
for path in removed.difference(added):
243
tt.unversion_file(tt.trans_id_tree_path(path))
245
116
for conflict in cook_conflicts(resolve_conflicts(tt), tt):
246
117
warning(conflict)
119
update_ids(tree, added, removed)
122
def update_ids(tree, added, removed):
123
"""Make sure that all present files files have file_ids.
126
new = added.difference(removed)
127
deleted = removed.difference(added)
128
tree.add(sorted(new))
129
tree.remove(sorted(deleted, reverse=True))
250
132
def do_import(source, tree_directory=None):
279
161
import_tar(tree, tar_input)
281
163
tar_input.close()
282
elif source.endswith('.zip'):
283
import_zip(tree, open(source, 'rb'))
284
elif file_kind(source) == 'directory':
289
raise BzrCommandError('Unhandled import source')
167
class TestImport(TestCaseInTempDir):
169
def make_tar(self, mode='w'):
171
tar_file = tarfile.open('project-0.1.tar', mode, result)
172
os.mkdir('project-0.1')
173
tar_file.add('project-0.1')
174
os.mkdir('project-0.1/junk')
175
tar_file.add('project-0.1/junk')
177
f = file('project-0.1/README', 'wb')
180
tar_file.add('project-0.1/README')
182
f = file('project-0.1/FEEDME', 'wb')
185
tar_file.add('project-0.1/FEEDME')
188
rmtree('project-0.1')
194
tar_file = tarfile.open('project-0.2.tar', 'w', result)
195
os.mkdir('project-0.2')
196
tar_file.add('project-0.2')
198
os.mkdir('project-0.2/junk')
199
tar_file.add('project-0.2/junk')
201
f = file('project-0.2/README', 'wb')
204
tar_file.add('project-0.2/README')
207
tar_file = tarfile.open('project-0.2.tar', 'a', result)
208
tar_file.add('project-0.2/README')
210
rmtree('project-0.2')
213
def make_messed_tar(self):
215
tar_file = tarfile.open('project-0.1.tar', 'w', result)
216
os.mkdir('project-0.1')
217
tar_file.add('project-0.1')
219
os.mkdir('project-0.2')
220
tar_file.add('project-0.2')
222
f = file('project-0.1/README', 'wb')
225
tar_file.add('project-0.1/README')
227
rmtree('project-0.1')
231
def test_top_directory(self):
232
self.assertEqual(top_directory('ab/b/c'), 'ab')
233
self.assertEqual(top_directory('/etc'), '/')
235
def test_common_directory(self):
236
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
237
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
239
def test_untar(self):
240
tar_file = self.make_tar()
241
tree = BzrDir.create_standalone_workingtree('tree')
242
import_tar(tree, tar_file)
243
self.assertTrue(tree.path2id('README') is not None)
244
self.assertTrue(tree.path2id('FEEDME') is not None)
245
self.assertTrue(os.path.isfile(tree.abspath('README')))
246
self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
247
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
249
f = file(tree.abspath('junk/food'), 'wb')
250
f.write('I like food\n')
253
tar_file = self.make_tar2()
254
import_tar(tree, tar_file)
255
self.assertTrue(tree.path2id('README') is not None)
256
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
259
def test_untar2(self):
260
tar_file = self.make_messed_tar()
261
tree = BzrDir.create_standalone_workingtree('tree')
262
import_tar(tree, tar_file)
263
self.assertTrue(tree.path2id('project-0.1/README') is not None)
265
def test_untar_gzip(self):
266
tar_file = self.make_tar(mode='w:gz')
267
tree = BzrDir.create_standalone_workingtree('tree')
268
import_tar(tree, tar_file)
269
self.assertTrue(tree.path2id('README') is not None)
273
return makeSuite(TestImport)