cp-noerror 1.4 KB

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