3
3
from bz2 import BZ2File
6
from shutil import rmtree
7
6
from StringIO import StringIO
9
from unittest import makeSuite
11
from bzrlib import generate_ids
11
12
from bzrlib.bzrdir import BzrDir
12
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
13
from bzrlib.osutils import pathjoin, isdir, file_iterator
14
from bzrlib.tests import TestCaseInTempDir
14
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
15
16
from bzrlib.trace import warning
16
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
17
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)
20
112
def top_directory(path):
21
113
"""Return the top directory given in a path."""
68
162
The tarfile may be a gzipped stream. File ids will be updated.
70
164
tar_file = tarfile.open('lala', 'r', tar_input)
71
prefix = common_directory(names_of_files(tar_file))
165
import_archive(tree, tar_file)
167
def import_zip(tree, zip_input):
168
zip_file = ZipFileWrapper(zip_input, 'r')
169
import_archive(tree, zip_file)
171
def import_dir(tree, dir_input):
172
dir_file = DirWrapper(dir_input)
173
import_archive(tree, dir_file)
175
def import_archive(tree, archive_file):
176
prefix = common_directory(names_of_files(archive_file))
72
177
tt = TreeTransform(tree)
96
202
added.add(relative_path.rstrip('/'))
97
203
path = tree.abspath(relative_path)
98
204
if member.name in seen:
205
if tt.final_kind(trans_id) == 'file':
206
tt.set_executability(None, trans_id)
99
207
tt.cancel_creation(trans_id)
100
208
seen.add(member.name)
101
209
if member.isreg():
102
tt.create_file(file_iterator(tar_file.extractfile(member)),
210
tt.create_file(file_iterator(archive_file.extractfile(member)),
212
executable = (member.mode & 0111) != 0
213
tt.set_executability(executable, trans_id)
104
214
elif member.isdir():
105
215
do_directory(tt, trans_id, tree, relative_path, path)
106
216
elif member.issym():
107
217
tt.create_symlink(member.linkname, trans_id)
220
if tt.tree_file_id(trans_id) is None:
221
name = basename(member.name.rstrip('/'))
222
file_id = generate_ids.gen_file_id(name)
223
tt.version_file(file_id, trans_id)
109
225
for relative_path in implied_parents.difference(added):
110
226
if relative_path == "":
112
228
trans_id = tt.trans_id_tree_path(relative_path)
113
229
path = tree.abspath(relative_path)
114
230
do_directory(tt, trans_id, tree, relative_path, path)
231
if tt.tree_file_id(trans_id) is None:
232
tt.version_file(trans_id, trans_id)
115
233
added.add(relative_path)
235
for path in removed.difference(added):
236
tt.unversion_file(tt.trans_id_tree_path(path))
117
238
for conflict in cook_conflicts(resolve_conflicts(tt), tt):
118
239
warning(conflict)
120
update_ids(tree, added, removed)
123
def update_ids(tree, added, removed):
124
"""Make sure that all present files files have file_ids.
127
new = added.difference(removed)
128
deleted = removed.difference(added)
129
tree.add(sorted(new))
130
tree.remove(sorted(deleted, reverse=True))
133
243
def do_import(source, tree_directory=None):
162
272
import_tar(tree, tar_input)
164
274
tar_input.close()
275
elif source.endswith('.zip'):
276
import_zip(tree, open(source, 'rb'))
277
elif file_kind(source) == 'directory':
282
raise BzrCommandError('Unhandled import source')
168
class TestImport(TestCaseInTempDir):
170
def make_tar(self, mode='w'):
172
tar_file = tarfile.open('project-0.1.tar', mode, result)
173
os.mkdir('project-0.1')
174
tar_file.add('project-0.1')
175
os.mkdir('project-0.1/junk')
176
tar_file.add('project-0.1/junk')
178
f = file('project-0.1/README', 'wb')
181
tar_file.add('project-0.1/README')
183
f = file('project-0.1/FEEDME', 'wb')
186
tar_file.add('project-0.1/FEEDME')
189
rmtree('project-0.1')
195
tar_file = tarfile.open('project-0.2.tar', 'w', result)
196
os.mkdir('project-0.2')
197
tar_file.add('project-0.2')
199
os.mkdir('project-0.2/junk')
200
tar_file.add('project-0.2/junk')
202
f = file('project-0.2/README', 'wb')
205
tar_file.add('project-0.2/README')
208
tar_file = tarfile.open('project-0.2.tar', 'a', result)
209
tar_file.add('project-0.2/README')
211
rmtree('project-0.2')
214
def make_messed_tar(self):
216
tar_file = tarfile.open('project-0.1.tar', 'w', result)
217
os.mkdir('project-0.1')
218
tar_file.add('project-0.1')
220
os.mkdir('project-0.2')
221
tar_file.add('project-0.2')
223
f = file('project-0.1/README', 'wb')
226
tar_file.add('project-0.1/README')
228
rmtree('project-0.1')
232
def test_top_directory(self):
233
self.assertEqual(top_directory('ab/b/c'), 'ab')
234
self.assertEqual(top_directory('/etc'), '/')
236
def test_common_directory(self):
237
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
238
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
240
def test_untar(self):
241
tar_file = self.make_tar()
242
tree = BzrDir.create_standalone_workingtree('tree')
243
import_tar(tree, tar_file)
244
self.assertTrue(tree.path2id('README') is not None)
245
self.assertTrue(tree.path2id('FEEDME') is not None)
246
self.assertTrue(os.path.isfile(tree.abspath('README')))
247
self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
248
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
250
f = file(tree.abspath('junk/food'), 'wb')
251
f.write('I like food\n')
254
tar_file = self.make_tar2()
255
import_tar(tree, tar_file)
256
self.assertTrue(tree.path2id('README') is not None)
257
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
260
def test_untar2(self):
261
tar_file = self.make_messed_tar()
262
tree = BzrDir.create_standalone_workingtree('tree')
263
import_tar(tree, tar_file)
264
self.assertTrue(tree.path2id('project-0.1/README') is not None)
266
def test_untar_gzip(self):
267
tar_file = self.make_tar(mode='w:gz')
268
tree = BzrDir.create_standalone_workingtree('tree')
269
import_tar(tree, tar_file)
270
self.assertTrue(tree.path2id('README') is not None)
274
return makeSuite(TestImport)