cp-noerror 1.5 KB

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