split.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. """This script splits httplib.h into .h and .cc parts."""
  3. import os
  4. import sys
  5. from argparse import ArgumentParser, Namespace
  6. from typing import List
  7. def main() -> None:
  8. """Main entry point for the script."""
  9. BORDER: str = '// ----------------------------------------------------------------------------'
  10. args_parser: ArgumentParser = ArgumentParser(description=__doc__)
  11. args_parser.add_argument(
  12. "-e", "--extension", help="extension of the implementation file (default: cc)",
  13. default="cc"
  14. )
  15. args_parser.add_argument(
  16. "-o", "--out", help="where to write the files (default: out)", default="out"
  17. )
  18. args: Namespace = args_parser.parse_args()
  19. cur_dir: str = os.path.dirname(sys.argv[0])
  20. if not cur_dir:
  21. cur_dir = '.'
  22. lib_name: str = 'httplib'
  23. header_name: str = f"/{lib_name}.h"
  24. source_name: str = f"/{lib_name}.{args.extension}"
  25. # get the input file
  26. in_file: str = cur_dir + header_name
  27. # get the output file
  28. h_out: str = args.out + header_name
  29. cc_out: str = args.out + source_name
  30. # if the modification time of the out file is after the in file,
  31. # don't split (as it is already finished)
  32. do_split: bool = True
  33. if os.path.exists(h_out):
  34. in_time: float = os.path.getmtime(in_file)
  35. out_time: float = os.path.getmtime(h_out)
  36. do_split: bool = in_time > out_time
  37. if do_split:
  38. with open(in_file) as f:
  39. lines: List[str] = f.readlines()
  40. os.makedirs(args.out, exist_ok=True)
  41. in_implementation: bool = False
  42. cc_out: str = args.out + source_name
  43. with open(h_out, 'w') as fh, open(cc_out, 'w') as fc:
  44. fc.write('#include "httplib.h"\n')
  45. fc.write('namespace httplib {\n')
  46. for line in lines:
  47. is_border_line: bool = BORDER in line
  48. if is_border_line:
  49. in_implementation: bool = not in_implementation
  50. elif in_implementation:
  51. fc.write(line.replace('inline ', ''))
  52. else:
  53. fh.write(line)
  54. fc.write('} // namespace httplib\n')
  55. print(f"Wrote {h_out} and {cc_out}")
  56. else:
  57. print(f"{h_out} and {cc_out} are up to date")
  58. if __name__ == "__main__":
  59. main()