fs_tools.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from __future__ import print_function
  2. import os
  3. import platform
  4. import sys
  5. import shutil
  6. import errno
  7. import process_command_files as pcf
  8. def link_or_copy(src, dst, trace={}):
  9. if dst not in trace:
  10. trace[dst] = src
  11. try:
  12. if platform.system().lower() == 'windows':
  13. shutil.copy(src, dst)
  14. else:
  15. os.link(src, dst)
  16. except OSError as e:
  17. if e.errno == errno.EEXIST:
  18. if dst in trace:
  19. print(
  20. '[[bad]]link_or_copy: copy collision found - tried to copy {} to {} which was copied earlier from {}[[rst]]'.format(
  21. src, dst, trace[dst]
  22. ),
  23. file=sys.stderr,
  24. )
  25. else:
  26. print('[[bad]]link_or_copy: destination file already exists: {}[[rst]]'.format(dst), file=sys.stderr)
  27. if e.errno == errno.ENOENT:
  28. print('[[bad]]link_or_copy: source file doesn\'t exists: {}[[rst]]'.format(src), file=sys.stderr)
  29. raise
  30. if __name__ == '__main__':
  31. mode = sys.argv[1]
  32. args = pcf.get_args(sys.argv[2:])
  33. if mode == 'copy':
  34. shutil.copy(args[0], args[1])
  35. elif mode == 'copy_tree_no_link':
  36. dst = args[1]
  37. shutil.copytree(
  38. args[0], dst, ignore=lambda dirname, names: [n for n in names if os.path.islink(os.path.join(dirname, n))]
  39. )
  40. elif mode == 'copy_files':
  41. src = args[0]
  42. dst = args[1]
  43. files = open(args[2]).read().strip().split()
  44. for f in files:
  45. s = os.path.join(src, f)
  46. d = os.path.join(dst, f)
  47. if os.path.exists(d):
  48. continue
  49. try:
  50. os.makedirs(os.path.dirname(d))
  51. except OSError:
  52. pass
  53. shutil.copy(s, d)
  54. elif mode == 'copy_all_files':
  55. src = args[0]
  56. dst = args[1]
  57. for root, _, files in os.walk(src):
  58. for f in files:
  59. if os.path.islink(os.path.join(root, f)):
  60. continue
  61. file_dst = os.path.join(dst, os.path.relpath(root, src), f)
  62. if os.path.exists(file_dst):
  63. continue
  64. try:
  65. os.makedirs(os.path.dirname(file_dst))
  66. except OSError:
  67. pass
  68. shutil.copy(os.path.join(root, f), file_dst)
  69. elif mode == 'rename_if_exists':
  70. if os.path.exists(args[0]):
  71. shutil.move(args[0], args[1])
  72. elif mode == 'rename':
  73. targetdir = os.path.dirname(args[1])
  74. if targetdir and not os.path.exists(targetdir):
  75. os.makedirs(os.path.dirname(args[1]))
  76. shutil.move(args[0], args[1])
  77. elif mode == 'remove':
  78. for f in args:
  79. try:
  80. if os.path.isfile(f) or os.path.islink(f):
  81. os.remove(f)
  82. else:
  83. shutil.rmtree(f)
  84. except OSError:
  85. pass
  86. elif mode == 'link_or_copy':
  87. link_or_copy(args[0], args[1])
  88. elif mode == 'link_or_copy_to_dir':
  89. assert len(args) > 1
  90. start = 0
  91. if args[0] == '--no-check':
  92. if args == 2:
  93. sys.exit()
  94. start = 1
  95. dst = args[-1]
  96. for src in args[start:-1]:
  97. link_or_copy(src, os.path.join(dst, os.path.basename(src)))
  98. elif mode == 'cat':
  99. with open(args[0], 'w') as dst:
  100. for input_name in args[1:]:
  101. with open(input_name) as src:
  102. dst.write(src.read())
  103. elif mode == 'md':
  104. try:
  105. os.makedirs(args[0])
  106. except OSError:
  107. pass
  108. else:
  109. raise Exception('unsupported tool %s' % mode)