copy_docs_files_to_dir.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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('--bin-dir', nargs='*')
  11. parser.add_argument('--build-root', required=True)
  12. parser.add_argument('--dest-dir', required=True)
  13. parser.add_argument('--docs-dir', action='append', nargs=2, dest='docs_dirs', default=None)
  14. parser.add_argument('--existing', choices=('skip', 'overwrite'), default='overwrite')
  15. parser.add_argument('--include-srcs', nargs='*', default=[])
  16. parser.add_argument('--skip-namespace', default=None)
  17. parser.add_argument('--source-root', required=True)
  18. parser.add_argument('--src-dir', action='append', nargs='*', dest='src_dirs', default=None)
  19. parser.add_argument('--srcs', nargs='*', default=[])
  20. return parser.parse_args(pcf.get_args(sys.argv[1:]))
  21. def makedirs(dirname):
  22. try:
  23. os.makedirs(dirname)
  24. except OSError as e:
  25. if e.errno == errno.EEXIST and os.path.isdir(dirname):
  26. pass
  27. else:
  28. raise
  29. def copy_file(src, dst, overwrite=False, orig_path=None):
  30. if os.path.exists(dst) and not overwrite:
  31. return
  32. makedirs(os.path.dirname(dst))
  33. with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
  34. if orig_path and src.endswith('.md'):
  35. out = b''
  36. buf = fsrc.readline()
  37. bom_length = len(codecs.BOM_UTF8)
  38. if buf[:bom_length] == codecs.BOM_UTF8:
  39. out += codecs.BOM_UTF8
  40. buf = buf[bom_length:]
  41. info = 'vcsPath: {}\n'.format(orig_path)
  42. if buf.startswith(b'---') and b'\n' in buf[3:] and buf[3:].rstrip(b'\r\n') == b'':
  43. content = b''
  44. found = False
  45. while True:
  46. line = fsrc.readline()
  47. if len(line) == 0:
  48. break
  49. content += line
  50. if line.startswith(b'---') and line[3:].rstrip(b'\r\n') == b'':
  51. found = True
  52. break
  53. out += buf
  54. if found:
  55. out += info.encode('utf-8')
  56. out += content
  57. else:
  58. out += '---\n{}---\n'.format(info).encode('utf-8')
  59. out += buf
  60. fdst.write(out)
  61. shutil.copyfileobj(fsrc, fdst)
  62. def main():
  63. args = parse_args()
  64. dest_dir = os.path.normpath(args.dest_dir)
  65. makedirs(dest_dir)
  66. source_root = os.path.normpath(args.source_root) + os.path.sep
  67. build_root = os.path.normpath(args.build_root) + os.path.sep
  68. is_overwrite_existing = args.existing == 'overwrite'
  69. if args.docs_dirs:
  70. for item in args.docs_dirs:
  71. assert len(item) == 2
  72. docs_dir, nm = item[0], item[1]
  73. assert not os.path.isabs(docs_dir)
  74. if nm and nm != '.':
  75. assert not os.path.isabs(nm)
  76. dst = os.path.join(dest_dir, nm)
  77. else:
  78. dst = dest_dir
  79. abs_docs_dir = os.path.join(args.source_root, docs_dir)
  80. for root, _, files in os.walk(abs_docs_dir):
  81. for f in files:
  82. if os.path.islink(os.path.join(root, f)):
  83. continue
  84. file_src = os.path.normpath(os.path.join(root, f))
  85. assert file_src.startswith(source_root)
  86. file_dst = os.path.join(dst, os.path.relpath(root, abs_docs_dir), f)
  87. copy_file(
  88. file_src, file_dst, overwrite=is_overwrite_existing, orig_path=file_src[len(source_root) :]
  89. )
  90. if args.src_dirs:
  91. for item in args.src_dirs:
  92. assert len(item) > 1
  93. src_dir, nm = os.path.normpath(item[0]), item[1]
  94. assert os.path.isabs(src_dir)
  95. if nm and nm != '.':
  96. assert not os.path.isabs(nm)
  97. dst = os.path.join(dest_dir, nm)
  98. else:
  99. dst = dest_dir
  100. if src_dir.startswith(source_root):
  101. root = source_root
  102. is_from_source_root = True
  103. else:
  104. assert src_dir.startswith(build_root)
  105. root = build_root
  106. is_from_source_root = False
  107. for f in item[2:]:
  108. file_src = os.path.normpath(f)
  109. assert file_src.startswith(root)
  110. rel_path = file_src[len(root) :] if is_from_source_root else None
  111. file_dst = os.path.join(dst, file_src[len(src_dir) :])
  112. copy_file(file_src, file_dst, overwrite=is_overwrite_existing, orig_path=rel_path)
  113. if args.bin_dir:
  114. assert len(args.bin_dir) > 1
  115. bin_dir, bin_dir_namespace = os.path.normpath(args.bin_dir[0]) + os.path.sep, args.bin_dir[1]
  116. assert bin_dir.startswith(build_root)
  117. if bin_dir_namespace and bin_dir_namespace != '.':
  118. assert not os.path.isabs(bin_dir_namespace)
  119. dst = os.path.join(dest_dir, bin_dir_namespace)
  120. else:
  121. dst = dest_dir
  122. for file_src in args.bin_dir[2:]:
  123. assert os.path.isfile(file_src)
  124. assert file_src.startswith(bin_dir)
  125. file_dst = os.path.join(dst, file_src[len(bin_dir) :])
  126. copy_file(file_src, file_dst, overwrite=is_overwrite_existing, orig_path=None)
  127. for skip_namespace, files in [(args.skip_namespace, args.srcs), (None, args.include_srcs)]:
  128. for src in files:
  129. file_src = os.path.normpath(src)
  130. assert os.path.isfile(file_src), 'File [{}] does not exist...'.format(file_src)
  131. rel_path = file_src
  132. orig_path = None
  133. if file_src.startswith(source_root):
  134. rel_path = file_src[len(source_root) :]
  135. if skip_namespace and rel_path.startswith(skip_namespace):
  136. rel_path = rel_path[len(skip_namespace):]
  137. orig_path = rel_path
  138. elif file_src.startswith(build_root):
  139. rel_path = file_src[len(build_root) :]
  140. else:
  141. raise Exception('Unexpected file path [{}].'.format(file_src))
  142. assert not os.path.isabs(rel_path)
  143. file_dst = os.path.join(args.dest_dir, rel_path)
  144. if file_dst != file_src:
  145. copy_file(file_src, file_dst, is_overwrite_existing, orig_path)
  146. if __name__ == '__main__':
  147. main()