split.py 1.9 KB

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