copy_docs_files_to_dir.py 6.6 KB

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