copy_docs_files.py 3.3 KB

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