comparison mercurial/hg.py @ 18441:1f794204abbd

hg: replace DirCleanup class with normal try/finally use
author Augie Fackler <raf@durin42.com>
date Fri, 05 Oct 2012 18:10:56 -0500
parents f3b21beb9802
children fe67107094fd
comparison
equal deleted inserted replaced
18440:35513c59f376 18441:1f794204abbd
289 if not os.path.isdir(dest): 289 if not os.path.isdir(dest):
290 raise util.Abort(_("destination '%s' already exists") % dest) 290 raise util.Abort(_("destination '%s' already exists") % dest)
291 elif os.listdir(dest): 291 elif os.listdir(dest):
292 raise util.Abort(_("destination '%s' is not empty") % dest) 292 raise util.Abort(_("destination '%s' is not empty") % dest)
293 293
294 class DirCleanup(object): 294 srclock = destlock = cleandir = None
295 def __init__(self, dir_):
296 self.rmtree = shutil.rmtree
297 self.dir_ = dir_
298 def close(self):
299 self.dir_ = None
300 def cleanup(self):
301 if self.dir_:
302 self.rmtree(self.dir_, True)
303
304 srclock = destlock = dircleanup = None
305 srcrepo = srcpeer.local() 295 srcrepo = srcpeer.local()
306 try: 296 try:
307 abspath = origsource 297 abspath = origsource
308 if islocal(origsource): 298 if islocal(origsource):
309 abspath = os.path.abspath(util.urllocalpath(origsource)) 299 abspath = os.path.abspath(util.urllocalpath(origsource))
310 300
311 if islocal(dest): 301 if islocal(dest):
312 dircleanup = DirCleanup(dest) 302 cleandir = dest
313 303
314 copy = False 304 copy = False
315 if (srcrepo and srcrepo.cancopy() and islocal(dest) 305 if (srcrepo and srcrepo.cancopy() and islocal(dest)
316 and not phases.hassecret(srcrepo)): 306 and not phases.hassecret(srcrepo)):
317 copy = not pull and not rev 307 copy = not pull and not rev
331 hgdir = os.path.realpath(os.path.join(dest, ".hg")) 321 hgdir = os.path.realpath(os.path.join(dest, ".hg"))
332 if not os.path.exists(dest): 322 if not os.path.exists(dest):
333 os.mkdir(dest) 323 os.mkdir(dest)
334 else: 324 else:
335 # only clean up directories we create ourselves 325 # only clean up directories we create ourselves
336 dircleanup.dir_ = hgdir 326 cleandir = hgdir
337 try: 327 try:
338 destpath = hgdir 328 destpath = hgdir
339 util.makedir(destpath, notindexed=True) 329 util.makedir(destpath, notindexed=True)
340 except OSError, inst: 330 except OSError, inst:
341 if inst.errno == errno.EEXIST: 331 if inst.errno == errno.EEXIST:
342 dircleanup.close() 332 cleandir = None
343 raise util.Abort(_("destination '%s' already exists") 333 raise util.Abort(_("destination '%s' already exists")
344 % dest) 334 % dest)
345 raise 335 raise
346 336
347 destlock = copystore(ui, srcrepo, destpath) 337 destlock = copystore(ui, srcrepo, destpath)
365 try: 355 try:
366 destpeer = peer(srcrepo or ui, peeropts, dest, create=True) 356 destpeer = peer(srcrepo or ui, peeropts, dest, create=True)
367 # only pass ui when no srcrepo 357 # only pass ui when no srcrepo
368 except OSError, inst: 358 except OSError, inst:
369 if inst.errno == errno.EEXIST: 359 if inst.errno == errno.EEXIST:
370 dircleanup.close() 360 cleandir = None
371 raise util.Abort(_("destination '%s' already exists") 361 raise util.Abort(_("destination '%s' already exists")
372 % dest) 362 % dest)
373 raise 363 raise
374 364
375 revs = None 365 revs = None
385 elif srcrepo: 375 elif srcrepo:
386 srcrepo.push(destpeer, revs=revs) 376 srcrepo.push(destpeer, revs=revs)
387 else: 377 else:
388 raise util.Abort(_("clone from remote to remote not supported")) 378 raise util.Abort(_("clone from remote to remote not supported"))
389 379
390 if dircleanup: 380 cleandir = None
391 dircleanup.close()
392 381
393 # clone all bookmarks except divergent ones 382 # clone all bookmarks except divergent ones
394 destrepo = destpeer.local() 383 destrepo = destpeer.local()
395 if destrepo and srcpeer.capable("pushkey"): 384 if destrepo and srcpeer.capable("pushkey"):
396 rb = srcpeer.listkeys('bookmarks') 385 rb = srcpeer.listkeys('bookmarks')
452 bookmarks.setcurrent(destrepo, update) 441 bookmarks.setcurrent(destrepo, update)
453 442
454 return srcpeer, destpeer 443 return srcpeer, destpeer
455 finally: 444 finally:
456 release(srclock, destlock) 445 release(srclock, destlock)
457 if dircleanup is not None: 446 if cleandir is not None:
458 dircleanup.cleanup() 447 shutil.rmtree(cleandir, True)
459 if srcpeer is not None: 448 if srcpeer is not None:
460 srcpeer.close() 449 srcpeer.close()
461 450
462 def _showstats(repo, stats): 451 def _showstats(repo, stats):
463 repo.ui.status(_("%d files updated, %d files merged, " 452 repo.ui.status(_("%d files updated, %d files merged, "