cp-noerror 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. #
  3. # Allow copying of $1 to $2 but if files in $1 disappear during the copy operation,
  4. # don't error.
  5. #
  6. import sys
  7. import os
  8. import shutil
  9. def copytree(src, dst, symlinks=False, ignore=None):
  10. """Based on shutil.copytree"""
  11. names = os.listdir(src)
  12. try:
  13. os.makedirs(dst)
  14. except OSError:
  15. # Already exists
  16. pass
  17. errors = []
  18. for name in names:
  19. srcname = os.path.join(src, name)
  20. dstname = os.path.join(dst, name)
  21. try:
  22. d = dstname
  23. if os.path.isdir(dstname):
  24. d = os.path.join(dstname, os.path.basename(srcname))
  25. if os.path.exists(d):
  26. continue
  27. try:
  28. os.link(srcname, dstname)
  29. except OSError:
  30. shutil.copy2(srcname, dstname)
  31. # catch the Error from the recursive copytree so that we can
  32. # continue with other files
  33. except shutil.Error, err:
  34. errors.extend(err.args[0])
  35. except EnvironmentError, why:
  36. errors.append((srcname, dstname, str(why)))
  37. try:
  38. shutil.copystat(src, dst)
  39. except OSError, why:
  40. errors.extend((src, dst, str(why)))
  41. if errors:
  42. raise shutil.Error, errors
  43. try:
  44. copytree(sys.argv[1], sys.argv[2])
  45. except shutil.Error:
  46. pass