copy_docs_files.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import argparse
  2. import codecs
  3. import errno
  4. import os
  5. import sys
  6. # Explicitly enable local imports
  7. # Don't forget to add imported scripts to inputs of the calling command!
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  9. import process_command_files as pcf
  10. import shutil
  11. def parse_args():
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument('--build-root', required=True)
  14. parser.add_argument('--dst-dir', required=True)
  15. parser.add_argument('--existing', choices=('skip', 'overwrite'), default='overwrite')
  16. parser.add_argument('--source-root', required=True)
  17. parser.add_argument('--src-dir', required=None)
  18. parser.add_argument('files', nargs='*')
  19. return parser.parse_args(pcf.get_args(sys.argv[1:]))
  20. def makedirs(dirname):
  21. try:
  22. os.makedirs(dirname)
  23. except OSError as e:
  24. if e.errno == errno.EEXIST and os.path.isdir(dirname):
  25. pass
  26. else:
  27. raise
  28. def copy_file(src, dst, overwrite=False, orig_path=None, generated=False):
  29. if os.path.exists(dst) and not overwrite:
  30. return
  31. makedirs(os.path.dirname(dst))
  32. with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
  33. if (orig_path or generated) and src.endswith('.md'):
  34. out = b''
  35. buf = fsrc.readline()
  36. bom_length = len(codecs.BOM_UTF8)
  37. if buf[:bom_length] == codecs.BOM_UTF8:
  38. out += codecs.BOM_UTF8
  39. buf = buf[bom_length:]
  40. info = 'generated: true\n' if generated else 'vcsPath: {}\n'.format(orig_path)
  41. if buf.startswith(b'---') and b'\n' in buf[3:] and buf[3:].rstrip(b'\r\n') == b'':
  42. content = b''
  43. found = False
  44. while True:
  45. line = fsrc.readline()
  46. if len(line) == 0:
  47. break
  48. content += line
  49. if line.startswith(b'---') and line[3:].rstrip(b'\r\n') == b'':
  50. found = True
  51. break
  52. out += buf
  53. if found:
  54. out += info.encode('utf-8')
  55. out += content
  56. else:
  57. out += '---\n{}---\n'.format(info).encode('utf-8')
  58. out += buf
  59. fdst.write(out)
  60. shutil.copyfileobj(fsrc, fdst)
  61. def main():
  62. args = parse_args()
  63. source_root = os.path.normpath(args.source_root) + os.path.sep
  64. build_root = os.path.normpath(args.build_root) + os.path.sep
  65. dst_dir = os.path.normpath(args.dst_dir)
  66. assert dst_dir.startswith(build_root)
  67. makedirs(dst_dir)
  68. src_dir = os.path.normpath(args.src_dir) + os.path.sep
  69. if src_dir.startswith(source_root):
  70. root = source_root
  71. is_from_source_root = True
  72. elif src_dir.startswith(build_root):
  73. root = build_root
  74. is_from_source_root = False
  75. else:
  76. assert False, 'src_dir [{}] should start with [{}] or [{}]'.format(src_dir, source_root, build_root)
  77. is_overwrite_existing = args.existing == 'overwrite'
  78. for f in [os.path.normpath(f) for f in args.files]:
  79. src_file = os.path.join(src_dir, f)
  80. dst_file = os.path.join(dst_dir, f)
  81. if src_file == dst_file:
  82. continue
  83. rel_path = src_file[len(root) :] if is_from_source_root else None
  84. copy_file(src_file, dst_file, overwrite=is_overwrite_existing, orig_path=rel_path)
  85. if __name__ == '__main__':
  86. main()