fs_tools.py 3.3 KB

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