1
"""Import upstream source into a branch"""
6
from StringIO import StringIO
11
from bzrlib import generate_ids
12
from bzrlib.bzrdir import BzrDir
13
from bzrlib.errors import NoSuchFile, BzrCommandError, NotBranchError
14
from bzrlib.osutils import (pathjoin, isdir, file_iterator, basename,
16
from bzrlib.trace import warning
17
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
18
from bzrlib.workingtree import WorkingTree
19
from bzrlib.plugins.bzrtools.bzrtools import open_from_url
21
class ZipFileWrapper(object):
23
def __init__(self, fileobj, mode):
24
self.zipfile = zipfile.ZipFile(fileobj, mode)
27
for info in self.zipfile.infolist():
28
yield ZipInfoWrapper(self.zipfile, info)
30
def extractfile(self, infowrapper):
31
return StringIO(self.zipfile.read(infowrapper.name))
33
def add(self, filename):
35
self.zipfile.writestr(filename+'/', '')
37
self.zipfile.write(filename)
43
class ZipInfoWrapper(object):
45
def __init__(self, zipfile, info):
48
self.name = info.filename
49
self.zipfile = zipfile
54
return bool(self.name.endswith('/'))
58
return not self.isdir()
61
class DirWrapper(object):
62
def __init__(self, fileobj, mode='r'):
63
assert mode == 'r', mode
64
self.root = os.path.realpath(fileobj.read())
67
return 'DirWrapper(%r)' % self.root
69
def getmembers(self, subdir=None):
70
if subdir is not None:
71
mydir = pathjoin(self.root, subdir)
74
for child in os.listdir(mydir):
75
if subdir is not None:
76
child = pathjoin(subdir, child)
77
fi = FileInfo(self.root, child)
80
for v in self.getmembers(child):
83
def extractfile(self, member):
84
return open(member.fullpath)
87
class FileInfo(object):
89
def __init__(self, root, filepath):
90
self.fullpath = pathjoin(root, filepath)
93
self.name = pathjoin(basename(root), filepath)
95
print 'root %r' % root
96
self.name = basename(root)
98
stat = os.lstat(self.fullpath)
99
self.mode = stat.st_mode
104
return 'FileInfo(%r)' % self.name
107
return stat.S_ISREG(self.mode)
110
return stat.S_ISDIR(self.mode)
113
if stat.S_ISLNK(self.mode):
114
self.linkname = os.readlink(self.fullpath)
121
"""Return the top directory given in a path."""
122
components = splitpath(path)
123
if len(components) > 0:
129
def common_directory(names):
130
"""Determine a single directory prefix from a list of names"""
131
possible_prefix = None
133
name_top = top_path(name)
136
if possible_prefix is None:
137
possible_prefix = name_top
139
if name_top != possible_prefix:
141
return possible_prefix
144
def do_directory(tt, trans_id, tree, relative_path, path):
145
if isdir(path) and tree.path2id(relative_path) is not None:
146
tt.cancel_deletion(trans_id)
148
tt.create_directory(trans_id)
151
def add_implied_parents(implied_parents, path):
152
"""Update the set of implied parents from a path"""
153
parent = os.path.dirname(path)
154
if parent in implied_parents:
156
implied_parents.add(parent)
157
add_implied_parents(implied_parents, parent)
160
def names_of_files(tar_file):
161
for member in tar_file.getmembers():
162
if member.type != "g":
166
def should_ignore(relative_path):
167
return top_path(relative_path) == '.bzr'
170
def import_tar(tree, tar_input):
171
"""Replace the contents of a working directory with tarfile contents.
172
The tarfile may be a gzipped stream. File ids will be updated.
174
tar_file = tarfile.open('lala', 'r', tar_input)
175
import_archive(tree, tar_file)
177
def import_zip(tree, zip_input):
178
zip_file = ZipFileWrapper(zip_input, 'r')
179
import_archive(tree, zip_file)
181
def import_dir(tree, dir_input):
182
dir_file = DirWrapper(dir_input)
183
import_archive(tree, dir_file)
186
def import_archive(tree, archive_file):
187
tt = TreeTransform(tree)
189
import_archive_to_transform(tree, archive_file, tt)
195
def import_archive_to_transform(tree, archive_file, tt):
196
prefix = common_directory(names_of_files(archive_file))
198
for path, entry in tree.inventory.iter_entries():
199
if entry.parent_id is None:
201
trans_id = tt.trans_id_tree_path(path)
202
tt.delete_contents(trans_id)
206
implied_parents = set()
208
for member in archive_file.getmembers():
209
if member.type == 'g':
210
# type 'g' is a header
212
# Inverse functionality in bzr uses utf-8. We could also
213
# interpret relative to fs encoding, which would match native
215
relative_path = member.name.decode('utf-8')
216
if prefix is not None:
217
relative_path = relative_path[len(prefix)+1:]
218
relative_path = relative_path.rstrip('/')
219
if relative_path == '':
221
if should_ignore(relative_path):
223
add_implied_parents(implied_parents, relative_path)
224
trans_id = tt.trans_id_tree_path(relative_path)
225
added.add(relative_path.rstrip('/'))
226
path = tree.abspath(relative_path)
227
if member.name in seen:
228
if tt.final_kind(trans_id) == 'file':
229
tt.set_executability(None, trans_id)
230
tt.cancel_creation(trans_id)
231
seen.add(member.name)
233
tt.create_file(file_iterator(archive_file.extractfile(member)),
235
executable = (member.mode & 0111) != 0
236
tt.set_executability(executable, trans_id)
238
do_directory(tt, trans_id, tree, relative_path, path)
240
tt.create_symlink(member.linkname, trans_id)
243
if tt.tree_file_id(trans_id) is None:
244
name = basename(member.name.rstrip('/'))
245
file_id = generate_ids.gen_file_id(name)
246
tt.version_file(file_id, trans_id)
248
for relative_path in implied_parents.difference(added):
249
if relative_path == "":
251
trans_id = tt.trans_id_tree_path(relative_path)
252
path = tree.abspath(relative_path)
253
do_directory(tt, trans_id, tree, relative_path, path)
254
if tt.tree_file_id(trans_id) is None:
255
tt.version_file(trans_id, trans_id)
256
added.add(relative_path)
258
for path in removed.difference(added):
259
tt.unversion_file(tt.trans_id_tree_path(path))
261
for conflict in cook_conflicts(resolve_conflicts(tt), tt):
265
def do_import(source, tree_directory=None):
266
"""Implementation of import command. Intended for UI only"""
267
if tree_directory is not None:
269
tree = WorkingTree.open(tree_directory)
270
except NotBranchError:
271
if not os.path.exists(tree_directory):
272
os.mkdir(tree_directory)
273
branch = BzrDir.create_branch_convenience(tree_directory)
274
tree = branch.bzrdir.open_workingtree()
276
tree = WorkingTree.open_containing('.')[0]
279
if tree.changes_from(tree.basis_tree()).has_changed():
280
raise BzrCommandError("Working tree has uncommitted changes.")
282
if (source.endswith('.tar') or source.endswith('.tar.gz') or
283
source.endswith('.tar.bz2')) or source.endswith('.tgz'):
285
tar_input = open_from_url(source)
286
if source.endswith('.bz2'):
287
tar_input = StringIO(tar_input.read().decode('bz2'))
289
if e.errno == errno.ENOENT:
290
raise NoSuchFile(source)
292
import_tar(tree, tar_input)
295
elif source.endswith('.zip'):
296
import_zip(tree, open_from_url(source))
297
elif file_kind(source) == 'directory':
302
raise BzrCommandError('Unhandled import source')