1
"""Import upstream source into a branch"""
3
from bz2 import BZ2File
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
15
from bzrlib.trace import warning
16
from bzrlib.transform import TreeTransform, resolve_conflicts, cook_conflicts
17
from bzrlib.workingtree import WorkingTree
19
class ZipFileWrapper(object):
21
def __init__(self, fileobj, mode):
22
self.zipfile = zipfile.ZipFile(fileobj, mode)
25
for info in self.zipfile.infolist():
26
yield ZipInfoWrapper(self.zipfile, info)
28
def extractfile(self, infowrapper):
29
return StringIO(self.zipfile.read(infowrapper.name))
31
def add(self, filename):
33
self.zipfile.writestr(filename+'/', '')
35
self.zipfile.write(filename)
41
class ZipInfoWrapper(object):
43
def __init__(self, zipfile, info):
46
self.name = info.filename
47
self.zipfile = zipfile
52
return bool(self.name.endswith('/'))
56
return not self.isdir()
59
class DirWrapper(object):
60
def __init__(self, fileobj, mode='r'):
61
assert mode == 'r', mode
62
self.root = os.path.realpath(fileobj.read())
65
return 'DirWrapper(%r)' % self.root
67
def getmembers(self, subdir=None):
68
if subdir is not None:
69
mydir = pathjoin(self.root, subdir)
72
for child in os.listdir(mydir):
73
if subdir is not None:
74
child = pathjoin(subdir, child)
75
fi = FileInfo(self.root, child)
78
for v in self.getmembers(child):
81
def extractfile(self, member):
82
return open(member.fullpath)
85
class FileInfo(object):
87
def __init__(self, root, filepath):
88
self.fullpath = pathjoin(root, filepath)
92
stat = os.lstat(self.fullpath)
93
self.mode = stat.st_mode
98
return 'FileInfo(%r)' % self.name
101
return stat.S_ISREG(self.mode)
104
return stat.S_ISDIR(self.mode)
107
def top_directory(path):
108
"""Return the top directory given in a path."""
109
dirname = os.path.dirname(path)
110
last_dirname = dirname
112
dirname = os.path.dirname(dirname)
113
if dirname == '' or dirname == last_dirname:
115
last_dirname = dirname
118
def common_directory(names):
119
"""Determine a single directory prefix from a list of names"""
120
possible_prefix = None
122
name_top = top_directory(name)
125
if possible_prefix is None:
126
possible_prefix = name_top
128
if name_top != possible_prefix:
130
return possible_prefix
133
def do_directory(tt, trans_id, tree, relative_path, path):
134
if isdir(path) and tree.path2id(relative_path) is not None:
135
tt.cancel_deletion(trans_id)
137
tt.create_directory(trans_id)
140
def add_implied_parents(implied_parents, path):
141
"""Update the set of implied parents from a path"""
142
parent = os.path.dirname(path)
143
if parent in implied_parents:
145
implied_parents.add(parent)
146
add_implied_parents(implied_parents, parent)
149
def names_of_files(tar_file):
150
for member in tar_file.getmembers():
151
if member.type != "g":
155
def import_tar(tree, tar_input):
156
"""Replace the contents of a working directory with tarfile contents.
157
The tarfile may be a gzipped stream. File ids will be updated.
159
tar_file = tarfile.open('lala', 'r', tar_input)
160
import_archive(tree, tar_file)
162
def import_zip(tree, zip_input):
163
zip_file = ZipFileWrapper(zip_input, 'r')
164
import_archive(tree, zip_file)
166
def import_dir(tree, dir_input):
167
dir_file = DirWrapper(dir_input)
168
import_archive(tree, dir_file)
170
def import_archive(tree, archive_file):
171
prefix = common_directory(names_of_files(archive_file))
172
tt = TreeTransform(tree)
175
for path, entry in tree.inventory.iter_entries():
176
if entry.parent_id is None:
178
trans_id = tt.trans_id_tree_path(path)
179
tt.delete_contents(trans_id)
183
implied_parents = set()
185
for member in archive_file.getmembers():
186
if member.type == 'g':
187
# type 'g' is a header
189
relative_path = member.name
190
if prefix is not None:
191
relative_path = relative_path[len(prefix)+1:]
192
if relative_path == '':
194
add_implied_parents(implied_parents, relative_path)
195
trans_id = tt.trans_id_tree_path(relative_path)
196
added.add(relative_path.rstrip('/'))
197
path = tree.abspath(relative_path)
198
if member.name in seen:
199
if tt.final_kind(trans_id) == 'file':
200
tt.set_executability(None, trans_id)
201
tt.cancel_creation(trans_id)
202
seen.add(member.name)
204
tt.create_file(file_iterator(archive_file.extractfile(member)),
206
executable = (member.mode & 0111) != 0
207
tt.set_executability(executable, trans_id)
209
do_directory(tt, trans_id, tree, relative_path, path)
211
tt.create_symlink(member.linkname, trans_id)
214
if tt.tree_file_id(trans_id) is None:
215
name = basename(member.name.rstrip('/'))
216
file_id = generate_ids.gen_file_id(name)
217
tt.version_file(file_id, trans_id)
219
for relative_path in implied_parents.difference(added):
220
if relative_path == "":
222
trans_id = tt.trans_id_tree_path(relative_path)
223
path = tree.abspath(relative_path)
224
do_directory(tt, trans_id, tree, relative_path, path)
225
if tt.tree_file_id(trans_id) is None:
226
tt.version_file(trans_id, trans_id)
227
added.add(relative_path)
229
for path in removed.difference(added):
230
tt.unversion_file(tt.trans_id_tree_path(path))
232
for conflict in cook_conflicts(resolve_conflicts(tt), tt):
237
def do_import(source, tree_directory=None):
238
"""Implementation of import command. Intended for UI only"""
239
if tree_directory is not None:
241
tree = WorkingTree.open(tree_directory)
242
except NotBranchError:
243
if not os.path.exists(tree_directory):
244
os.mkdir(tree_directory)
245
branch = BzrDir.create_branch_convenience(tree_directory)
246
tree = branch.bzrdir.open_workingtree()
248
tree = WorkingTree.open_containing('.')[0]
251
if tree.changes_from(tree.basis_tree()).has_changed():
252
raise BzrCommandError("Working tree has uncommitted changes.")
254
if (source.endswith('.tar') or source.endswith('.tar.gz') or
255
source.endswith('.tar.bz2')) or source.endswith('.tgz'):
257
if source.endswith('.bz2'):
258
tar_input = BZ2File(source, 'r')
259
tar_input = StringIO(tar_input.read())
261
tar_input = file(source, 'rb')
263
if e.errno == errno.ENOENT:
264
raise NoSuchFile(source)
266
import_tar(tree, tar_input)
269
elif source.endswith('.zip'):
270
import_zip(tree, open(source, 'rb'))
272
raise BzrCommandError('Unhandled import source')