Update the split.py file (#2295)

This commit is contained in:
Miko
2025-12-08 19:18:01 -08:00
committed by GitHub
parent 87c2b4e584
commit c3613c6977

108
split.py
View File

@@ -2,66 +2,72 @@
"""This script splits httplib.h into .h and .cc parts.""" """This script splits httplib.h into .h and .cc parts."""
import argparse
import os import os
import sys import sys
from argparse import ArgumentParser, Namespace
from typing import List
border = '// ----------------------------------------------------------------------------'
args_parser = argparse.ArgumentParser(description=__doc__) def main() -> None:
args_parser.add_argument( """Main entry point for the script."""
"-e", "--extension", help="extension of the implementation file (default: cc)", BORDER: str = '// ----------------------------------------------------------------------------'
default="cc"
)
args_parser.add_argument(
"-o", "--out", help="where to write the files (default: out)", default="out"
)
args = args_parser.parse_args()
cur_dir = os.path.dirname(sys.argv[0]) args_parser: ArgumentParser = ArgumentParser(description=__doc__)
lib_name = 'httplib' args_parser.add_argument(
header_name = '/' + lib_name + '.h' "-e", "--extension", help="extension of the implementation file (default: cc)",
source_name = '/' + lib_name + '.' + args.extension default="cc"
# get the input file )
in_file = cur_dir + header_name args_parser.add_argument(
# get the output file "-o", "--out", help="where to write the files (default: out)", default="out"
h_out = args.out + header_name )
cc_out = args.out + source_name args: Namespace = args_parser.parse_args()
# if the modification time of the out file is after the in file, cur_dir: str = os.path.dirname(sys.argv[0])
# don't split (as it is already finished) if not cur_dir:
do_split = True cur_dir = '.'
lib_name: str = 'httplib'
header_name: str = f"/{lib_name}.h"
source_name: str = f"/{lib_name}.{args.extension}"
# get the input file
in_file: str = cur_dir + header_name
# get the output file
h_out: str = args.out + header_name
cc_out: str = args.out + source_name
if os.path.exists(h_out): # if the modification time of the out file is after the in file,
in_time = os.path.getmtime(in_file) # don't split (as it is already finished)
out_time = os.path.getmtime(h_out) do_split: bool = True
do_split = in_time > out_time
if do_split: if os.path.exists(h_out):
with open(in_file) as f: in_time: float = os.path.getmtime(in_file)
lines = f.readlines() out_time: float = os.path.getmtime(h_out)
do_split: bool = in_time > out_time
if do_split:
with open(in_file) as f:
lines: List[str] = f.readlines()
python_version = sys.version_info[0]
if python_version < 3:
os.makedirs(args.out)
else:
os.makedirs(args.out, exist_ok=True) os.makedirs(args.out, exist_ok=True)
in_implementation = False in_implementation: bool = False
cc_out = args.out + source_name cc_out: str = args.out + source_name
with open(h_out, 'w') as fh, open(cc_out, 'w') as fc: with open(h_out, 'w') as fh, open(cc_out, 'w') as fc:
fc.write('#include "httplib.h"\n') fc.write('#include "httplib.h"\n')
fc.write('namespace httplib {\n') fc.write('namespace httplib {\n')
for line in lines: for line in lines:
is_border_line = border in line is_border_line: bool = BORDER in line
if is_border_line: if is_border_line:
in_implementation = not in_implementation in_implementation: bool = not in_implementation
elif in_implementation: elif in_implementation:
fc.write(line.replace('inline ', '')) fc.write(line.replace('inline ', ''))
else: else:
fh.write(line) fh.write(line)
fc.write('} // namespace httplib\n') fc.write('} // namespace httplib\n')
print("Wrote {} and {}".format(h_out, cc_out)) print(f"Wrote {h_out} and {cc_out}")
else: else:
print("{} and {} are up to date".format(h_out, cc_out)) print(f"{h_out} and {cc_out} are up to date")
if __name__ == "__main__":
main()