172
172
ancestors.reverse()
175
def get_remaining_revisions(output_dir, version):
178
if os.path.exists(output_dir):
179
# We are starting from an existing directory, figure out what
180
# the current version is
181
branch = bzrlib.Branch(output_dir)
182
last_patch = branch.last_patch()
184
last_patch = arch_revision(last_patch)
185
except NotArchRevision:
187
"Directory \"%s\" already exists, and the last revision is not"
188
" an Arch revision (%s)" % (output_dir, last_patch))
190
version = last_patch.version
191
elif version is None:
192
raise UserError("No version specified, and directory does not exist.")
194
ancestors = version_ancestry(version)
197
for i in range(len(ancestors)):
198
if ancestors[i] == last_patch:
201
raise UserError("Directory \"%s\" already exists, and the last "
202
"revision (%s) is not in the ancestry of %s" %
203
(output_dir, last_patch, version))
204
# Strip off all of the ancestors which are already present
205
# And get a directory starting with the latest ancestor
206
latest_ancestor = ancestors[i]
207
old_revno = bzrlib.Branch(output_dir).revno()
208
ancestors = ancestors[i+1:]
209
return ancestors, old_revno
176
211
def import_version(output_dir, version, fancy=True, fast=False, verbose=False,
177
212
dry_run=False, max_count=None, skip_symlinks=False):
188
223
>>> teardown_environ(q)
225
ancestors, old_revno = get_remaining_revisions(output_dir, version)
226
if len(ancestors) == 0:
227
print '* Tree is up-to-date with %s' % last_patch
190
230
progress_bar = ProgressBar()
191
231
tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
192
232
dir=os.path.dirname(output_dir))
195
235
print "not fancy"
196
for result in iter_import_version(output_dir, version, tempdir,
236
for result in iter_import_version(output_dir, ancestors, tempdir,
197
237
fast=fast, verbose=verbose, dry_run=dry_run,
198
238
max_count=max_count, skip_symlinks=skip_symlinks):
200
240
progress_bar(result)
202
242
sys.stdout.write('.')
245
print '**Dry run, not modifying output_dir'
247
if os.path.exists(output_dir):
248
# Move the bzr control directory back, and update the working tree
249
tmp_bzr_dir = os.path.join(tempdir, '.bzr')
251
bzr_dir = os.path.join(output_dir, '.bzr')
252
new_bzr_dir = os.path.join(tempdir, "rd", '.bzr')
254
os.rename(bzr_dir, tmp_bzr_dir) # Move the original bzr out of the way
255
os.rename(new_bzr_dir, bzr_dir)
257
# bzrlib.merge that exists in mainline does not have a this_dir component,
258
# so we have to work in the local directory
262
bzrlib.merge.merge(('.', -1), ('.', old_revno))
266
# If something failed, move back the original bzr directory
267
os.rename(bzr_dir, new_bzr_dir)
268
os.rename(tmp_bzr_dir, bzr_dir)
271
os.rename(revdir, output_dir)
205
275
clear_progress_bar()
251
321
except pybaz.errors.NamespaceError, e:
252
322
raise NotArchRevision(revision_id)
254
def iter_import_version(output_dir, version, tempdir, fast=False,
324
def iter_import_version(output_dir, ancestors, tempdir, fast=False,
255
325
verbose=False, dry_run=False, max_count=None,
256
326
skip_symlinks=False):
258
ancestors = version_ancestry(version)
260
if os.path.exists(output_dir):
261
# We are starting from an existing directory, figure out what
262
# the current version is
263
branch = bzrlib.Branch(output_dir)
264
last_patch = branch.last_patch()
266
last_patch = arch_revision(last_patch)
267
except NotArchRevision:
269
"Directory \"%s\" already exists, and the last patch is not"
270
" an Arch patch (%s)" % (output_dir, last_patch))
273
for i in range(len(ancestors)):
274
if ancestors[i] == last_patch:
277
raise UserError("Directory \"%s\" already exists, and the last patch (%s) is not in the revision history" % (output_dir, last_patch))
278
# Strip off all of the ancestors which are already present
279
# And get a directory starting with the latest ancestor
280
latest_ancestor = ancestors[i]
281
old_revno = bzrlib.Branch(output_dir).revno()
282
ancestors = ancestors[i+1:]
284
if len(ancestors) == 0:
285
print '* Tree is up-to-date with %s' % last_patch
288
revdir = os.path.join(tempdir, "rd")
289
baz_inv, log = get_revision(revdir, latest_ancestor,
290
skip_symlinks=skip_symlinks)
291
bzr_dir = os.path.join(output_dir, '.bzr')
292
new_bzr_dir = os.path.join(revdir, '.bzr')
293
# This would be much faster with a simple os.rename(), but if we fail,
294
# we have corrupted the original .bzr directory.
295
# Is that a big problem, as we can just back out the last revisions in
296
# .bzr/revision_history
297
# I don't really know
298
shutil.copytree(bzr_dir, new_bzr_dir)
299
# Now revdir should have a tree with the latest .bzr, and the correct
300
# version of the baz tree
302
329
# Uncomment this for testing, it basically just has baz2bzr only update
303
330
# 5 patches at a time
330
357
revdir = os.path.join(tempdir, "rd")
331
358
baz_inv, log = get_revision(revdir, revision,
332
359
skip_symlinks=skip_symlinks)
333
branch = bzrlib.Branch(revdir, init=True)
360
if os.path.exists(output_dir):
361
bzr_dir = os.path.join(output_dir, '.bzr')
362
new_bzr_dir = os.path.join(tempdir, "rd", '.bzr')
363
# This would be much faster with a simple os.rename(), but if
364
# we fail, we have corrupted the original .bzr directory. Is
365
# that a big problem, as we can just back out the last
366
# revisions in .bzr/revision_history I don't really know
367
shutil.copytree(bzr_dir, new_bzr_dir)
368
# Now revdir should have a tree with the latest .bzr, and the
369
# next revision of the baz tree
370
branch = bzrlib.Branch(revdir)
372
branch = bzrlib.Branch(revdir, init=True)
335
374
old = os.path.join(revdir, ".bzr")
336
375
new = os.path.join(tempdir, ".bzr")
353
392
yield Progress("revisions", len(ancestors), len(ancestors))
354
393
unlink_unversioned(branch, revdir)
356
print '**Dry run, not modifying output_dir'
358
if os.path.exists(output_dir):
359
# Move the bzr control directory back, and update the working tree
360
tmp_bzr_dir = os.path.join(tempdir, '.bzr')
361
os.rename(bzr_dir, tmp_bzr_dir) # Move the original bzr out of the way
362
os.rename(new_bzr_dir, bzr_dir)
364
# bzrlib.merge that exists in mainline does not have a this_dir component,
365
# so we have to work in the local directory
369
bzrlib.merge.merge(('.', -1), ('.', old_revno))
373
# If something failed, move back the original bzr directory
374
os.rename(bzr_dir, new_bzr_dir)
375
os.rename(tmp_bzr_dir, bzr_dir)
378
os.rename(revdir, output_dir)
380
395
def unlink_unversioned(branch, revdir):
381
396
for unversioned in branch.working_tree().extras():
453
468
:param args: The arguments to this script. Essentially sys.argv[1:]
456
parser = optparse.OptionParser(usage='%prog [options] VERSION OUTDIR'
471
parser = optparse.OptionParser(usage='%prog [options] [VERSION] OUTDIR'
457
472
'\n VERSION is the arch version to import.'
458
473
'\n OUTDIR can be an existing directory to be updated'
459
474
'\n or a new directory which will be created from scratch.')
512
output_dir = os.path.realpath(args[1])
513
version = pybaz.Version(args[0])
515
output_dir = os.path.realpath(args[0])
498
518
print 'Invalid number of arguments, try --help for more info'
502
output_dir = os.path.realpath(args[1])
503
import_version(output_dir, pybaz.Version(args[0]),
504
verbose=opts.verbose, fast=opts.fast,
505
fancy=opts.fancy, dry_run=opts.dry_run,
506
max_count=opts.max_count, skip_symlinks=opts.skip_symlinks)
511
except KeyboardInterrupt:
523
import_version(output_dir, version,
524
verbose=opts.verbose, fast=opts.fast,
525
fancy=opts.fancy, dry_run=opts.dry_run,
526
max_count=opts.max_count, skip_symlinks=opts.skip_symlinks)
531
except KeyboardInterrupt:
516
536
if __name__ == '__main__':