##// END OF EJS Templates
largefiles: use try/except/finally
Matt Mackall -
r25079:bee00e0c default
parent child Browse files
Show More
@@ -578,14 +578,13 b' def overridecopy(orig, ui, repo, pats, o'
578 nolfiles = False
578 nolfiles = False
579 installnormalfilesmatchfn(repo[None].manifest())
579 installnormalfilesmatchfn(repo[None].manifest())
580 try:
580 try:
581 try:
581 result = orig(ui, repo, pats, opts, rename)
582 result = orig(ui, repo, pats, opts, rename)
582 except util.Abort, e:
583 except util.Abort, e:
583 if str(e) != _('no files to copy'):
584 if str(e) != _('no files to copy'):
584 raise e
585 raise e
585 else:
586 else:
586 nonormalfiles = True
587 nonormalfiles = True
587 result = 0
588 result = 0
589 finally:
588 finally:
590 restorematchfn()
589 restorematchfn()
591
590
@@ -608,86 +607,85 b' def overridecopy(orig, ui, repo, pats, o'
608 os.makedirs(makestandin(dest))
607 os.makedirs(makestandin(dest))
609
608
610 try:
609 try:
611 try:
610 # When we call orig below it creates the standins but we don't add
612 # When we call orig below it creates the standins but we don't add
611 # them to the dir state until later so lock during that time.
613 # them to the dir state until later so lock during that time.
612 wlock = repo.wlock()
614 wlock = repo.wlock()
615
613
616 manifest = repo[None].manifest()
614 manifest = repo[None].manifest()
617 def overridematch(ctx, pats=[], opts={}, globbed=False,
615 def overridematch(ctx, pats=[], opts={}, globbed=False,
618 default='relpath'):
616 default='relpath'):
619 newpats = []
617 newpats = []
620 # The patterns were previously mangled to add the standin
618 # The patterns were previously mangled to add the standin
621 # directory; we need to remove that now
619 # directory; we need to remove that now
622 for pat in pats:
623 if match_.patkind(pat) is None and lfutil.shortname in pat:
624 newpats.append(pat.replace(lfutil.shortname, ''))
625 else:
626 newpats.append(pat)
627 match = oldmatch(ctx, newpats, opts, globbed, default)
628 m = copy.copy(match)
629 lfile = lambda f: lfutil.standin(f) in manifest
630 m._files = [lfutil.standin(f) for f in m._files if lfile(f)]
631 m._fmap = set(m._files)
632 origmatchfn = m.matchfn
633 m.matchfn = lambda f: (lfutil.isstandin(f) and
634 (f in manifest) and
635 origmatchfn(lfutil.splitstandin(f)) or
636 None)
637 return m
638 oldmatch = installmatchfn(overridematch)
639 listpats = []
640 for pat in pats:
620 for pat in pats:
641 if match_.patkind(pat) is not None:
621 if match_.patkind(pat) is None and lfutil.shortname in pat:
642 listpats.append(pat)
622 newpats.append(pat.replace(lfutil.shortname, ''))
643 else:
623 else:
644 listpats.append(makestandin(pat))
624 newpats.append(pat)
625 match = oldmatch(ctx, newpats, opts, globbed, default)
626 m = copy.copy(match)
627 lfile = lambda f: lfutil.standin(f) in manifest
628 m._files = [lfutil.standin(f) for f in m._files if lfile(f)]
629 m._fmap = set(m._files)
630 origmatchfn = m.matchfn
631 m.matchfn = lambda f: (lfutil.isstandin(f) and
632 (f in manifest) and
633 origmatchfn(lfutil.splitstandin(f)) or
634 None)
635 return m
636 oldmatch = installmatchfn(overridematch)
637 listpats = []
638 for pat in pats:
639 if match_.patkind(pat) is not None:
640 listpats.append(pat)
641 else:
642 listpats.append(makestandin(pat))
645
643
646 try:
644 try:
647 origcopyfile = util.copyfile
645 origcopyfile = util.copyfile
648 copiedfiles = []
646 copiedfiles = []
649 def overridecopyfile(src, dest):
647 def overridecopyfile(src, dest):
650 if (lfutil.shortname in src and
651 dest.startswith(repo.wjoin(lfutil.shortname))):
652 destlfile = dest.replace(lfutil.shortname, '')
653 if not opts['force'] and os.path.exists(destlfile):
654 raise IOError('',
655 _('destination largefile already exists'))
656 copiedfiles.append((src, dest))
657 origcopyfile(src, dest)
658
659 util.copyfile = overridecopyfile
660 result += orig(ui, repo, listpats, opts, rename)
661 finally:
662 util.copyfile = origcopyfile
663
664 lfdirstate = lfutil.openlfdirstate(ui, repo)
665 for (src, dest) in copiedfiles:
666 if (lfutil.shortname in src and
648 if (lfutil.shortname in src and
667 dest.startswith(repo.wjoin(lfutil.shortname))):
649 dest.startswith(repo.wjoin(lfutil.shortname))):
668 srclfile = src.replace(repo.wjoin(lfutil.standin('')), '')
650 destlfile = dest.replace(lfutil.shortname, '')
669 destlfile = dest.replace(repo.wjoin(lfutil.standin('')), '')
651 if not opts['force'] and os.path.exists(destlfile):
670 destlfiledir = os.path.dirname(repo.wjoin(destlfile)) or '.'
652 raise IOError('',
671 if not os.path.isdir(destlfiledir):
653 _('destination largefile already exists'))
672 os.makedirs(destlfiledir)
654 copiedfiles.append((src, dest))
673 if rename:
655 origcopyfile(src, dest)
674 os.rename(repo.wjoin(srclfile), repo.wjoin(destlfile))
656
657 util.copyfile = overridecopyfile
658 result += orig(ui, repo, listpats, opts, rename)
659 finally:
660 util.copyfile = origcopyfile
675
661
676 # The file is gone, but this deletes any empty parent
662 lfdirstate = lfutil.openlfdirstate(ui, repo)
677 # directories as a side-effect.
663 for (src, dest) in copiedfiles:
678 util.unlinkpath(repo.wjoin(srclfile), True)
664 if (lfutil.shortname in src and
679 lfdirstate.remove(srclfile)
665 dest.startswith(repo.wjoin(lfutil.shortname))):
680 else:
666 srclfile = src.replace(repo.wjoin(lfutil.standin('')), '')
681 util.copyfile(repo.wjoin(srclfile),
667 destlfile = dest.replace(repo.wjoin(lfutil.standin('')), '')
682 repo.wjoin(destlfile))
668 destlfiledir = os.path.dirname(repo.wjoin(destlfile)) or '.'
669 if not os.path.isdir(destlfiledir):
670 os.makedirs(destlfiledir)
671 if rename:
672 os.rename(repo.wjoin(srclfile), repo.wjoin(destlfile))
683
673
684 lfdirstate.add(destlfile)
674 # The file is gone, but this deletes any empty parent
685 lfdirstate.write()
675 # directories as a side-effect.
686 except util.Abort, e:
676 util.unlinkpath(repo.wjoin(srclfile), True)
687 if str(e) != _('no files to copy'):
677 lfdirstate.remove(srclfile)
688 raise e
678 else:
689 else:
679 util.copyfile(repo.wjoin(srclfile),
690 nolfiles = True
680 repo.wjoin(destlfile))
681
682 lfdirstate.add(destlfile)
683 lfdirstate.write()
684 except util.Abort, e:
685 if str(e) != _('no files to copy'):
686 raise e
687 else:
688 nolfiles = True
691 finally:
689 finally:
692 restorematchfn()
690 restorematchfn()
693 wlock.release()
691 wlock.release()
@@ -31,17 +31,16 b' def putlfile(repo, proto, sha):'
31 tmpfp = util.atomictempfile(path, createmode=repo.store.createmode)
31 tmpfp = util.atomictempfile(path, createmode=repo.store.createmode)
32
32
33 try:
33 try:
34 try:
34 proto.getfile(tmpfp)
35 proto.getfile(tmpfp)
35 tmpfp._fp.seek(0)
36 tmpfp._fp.seek(0)
36 if sha != lfutil.hexsha1(tmpfp._fp):
37 if sha != lfutil.hexsha1(tmpfp._fp):
37 raise IOError(0, _('largefile contents do not match hash'))
38 raise IOError(0, _('largefile contents do not match hash'))
38 tmpfp.close()
39 tmpfp.close()
39 lfutil.linktousercache(repo, sha)
40 lfutil.linktousercache(repo, sha)
40 except IOError, e:
41 except IOError, e:
41 repo.ui.warn(_('largefiles: failed to put %s into store: %s\n') %
42 repo.ui.warn(_('largefiles: failed to put %s into store: %s\n') %
42 (sha, e.strerror))
43 (sha, e.strerror))
43 return wireproto.pushres(1)
44 return wireproto.pushres(1)
45 finally:
44 finally:
46 tmpfp.discard()
45 tmpfp.discard()
47
46
@@ -36,13 +36,12 b' class remotestore(basestore.basestore):'
36 self.ui.debug('remotestore: sendfile(%s, %s)\n' % (filename, hash))
36 self.ui.debug('remotestore: sendfile(%s, %s)\n' % (filename, hash))
37 fd = None
37 fd = None
38 try:
38 try:
39 try:
39 fd = lfutil.httpsendfile(self.ui, filename)
40 fd = lfutil.httpsendfile(self.ui, filename)
41 except IOError, e:
42 raise util.Abort(
43 _('remotestore: could not open file %s: %s')
44 % (filename, str(e)))
45 return self._put(hash, fd)
40 return self._put(hash, fd)
41 except IOError, e:
42 raise util.Abort(
43 _('remotestore: could not open file %s: %s')
44 % (filename, str(e)))
46 finally:
45 finally:
47 if fd:
46 if fd:
48 fd.close()
47 fd.close()
General Comments 0
You need to be logged in to leave comments. Login now