Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 24439:2ddfac2f163e
util: add progress callback support to copyfiles
author | Augie Fackler <augie@google.com> |
---|---|
date | Thu, 19 Mar 2015 10:24:22 -0400 |
parents | de14c3972c2f |
children | 98744856b7d3 |
comparison
equal
deleted
inserted
replaced
24438:5b85a5bc5bbb | 24439:2ddfac2f163e |
---|---|
737 shutil.copyfile(src, dest) | 737 shutil.copyfile(src, dest) |
738 shutil.copymode(src, dest) | 738 shutil.copymode(src, dest) |
739 except shutil.Error, inst: | 739 except shutil.Error, inst: |
740 raise Abort(str(inst)) | 740 raise Abort(str(inst)) |
741 | 741 |
742 def copyfiles(src, dst, hardlink=None): | 742 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): |
743 """Copy a directory tree using hardlinks if possible""" | 743 """Copy a directory tree using hardlinks if possible.""" |
744 num = 0 | |
744 | 745 |
745 if hardlink is None: | 746 if hardlink is None: |
746 hardlink = (os.stat(src).st_dev == | 747 hardlink = (os.stat(src).st_dev == |
747 os.stat(os.path.dirname(dst)).st_dev) | 748 os.stat(os.path.dirname(dst)).st_dev) |
748 | 749 if hardlink: |
749 num = 0 | 750 topic = _('linking') |
751 else: | |
752 topic = _('copying') | |
753 | |
750 if os.path.isdir(src): | 754 if os.path.isdir(src): |
751 os.mkdir(dst) | 755 os.mkdir(dst) |
752 for name, kind in osutil.listdir(src): | 756 for name, kind in osutil.listdir(src): |
753 srcname = os.path.join(src, name) | 757 srcname = os.path.join(src, name) |
754 dstname = os.path.join(dst, name) | 758 dstname = os.path.join(dst, name) |
755 hardlink, n = copyfiles(srcname, dstname, hardlink) | 759 def nprog(t, pos): |
760 if pos is not None: | |
761 return progress(t, pos + num) | |
762 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog) | |
756 num += n | 763 num += n |
757 else: | 764 else: |
758 if hardlink: | 765 if hardlink: |
759 try: | 766 try: |
760 oslink(src, dst) | 767 oslink(src, dst) |
762 hardlink = False | 769 hardlink = False |
763 shutil.copy(src, dst) | 770 shutil.copy(src, dst) |
764 else: | 771 else: |
765 shutil.copy(src, dst) | 772 shutil.copy(src, dst) |
766 num += 1 | 773 num += 1 |
774 progress(topic, num) | |
775 progress(topic, None) | |
767 | 776 |
768 return hardlink, num | 777 return hardlink, num |
769 | 778 |
770 _winreservednames = '''con prn aux nul | 779 _winreservednames = '''con prn aux nul |
771 com1 com2 com3 com4 com5 com6 com7 com8 com9 | 780 com1 com2 com3 com4 com5 com6 com7 com8 com9 |