Skip to content

Commit c3613c6

Browse files
Update the split.py file (#2295)
1 parent 87c2b4e commit c3613c6

File tree

1 file changed

+57
-51
lines changed

1 file changed

+57
-51
lines changed

split.py

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,72 @@
22

33
"""This script splits httplib.h into .h and .cc parts."""
44

5-
import argparse
65
import os
76
import sys
7+
from argparse import ArgumentParser, Namespace
8+
from typing import List
89

9-
border = '// ----------------------------------------------------------------------------'
1010

11-
args_parser = argparse.ArgumentParser(description=__doc__)
12-
args_parser.add_argument(
13-
"-e", "--extension", help="extension of the implementation file (default: cc)",
14-
default="cc"
15-
)
16-
args_parser.add_argument(
17-
"-o", "--out", help="where to write the files (default: out)", default="out"
18-
)
19-
args = args_parser.parse_args()
11+
def main() -> None:
12+
"""Main entry point for the script."""
13+
BORDER: str = '// ----------------------------------------------------------------------------'
2014

21-
cur_dir = os.path.dirname(sys.argv[0])
22-
lib_name = 'httplib'
23-
header_name = '/' + lib_name + '.h'
24-
source_name = '/' + lib_name + '.' + args.extension
25-
# get the input file
26-
in_file = cur_dir + header_name
27-
# get the output file
28-
h_out = args.out + header_name
29-
cc_out = args.out + source_name
15+
args_parser: ArgumentParser = ArgumentParser(description=__doc__)
16+
args_parser.add_argument(
17+
"-e", "--extension", help="extension of the implementation file (default: cc)",
18+
default="cc"
19+
)
20+
args_parser.add_argument(
21+
"-o", "--out", help="where to write the files (default: out)", default="out"
22+
)
23+
args: Namespace = args_parser.parse_args()
3024

31-
# if the modification time of the out file is after the in file,
32-
# don't split (as it is already finished)
33-
do_split = True
25+
cur_dir: str = os.path.dirname(sys.argv[0])
26+
if not cur_dir:
27+
cur_dir = '.'
28+
lib_name: str = 'httplib'
29+
header_name: str = f"/{lib_name}.h"
30+
source_name: str = f"/{lib_name}.{args.extension}"
31+
# get the input file
32+
in_file: str = cur_dir + header_name
33+
# get the output file
34+
h_out: str = args.out + header_name
35+
cc_out: str = args.out + source_name
3436

35-
if os.path.exists(h_out):
36-
in_time = os.path.getmtime(in_file)
37-
out_time = os.path.getmtime(h_out)
38-
do_split = in_time > out_time
37+
# if the modification time of the out file is after the in file,
38+
# don't split (as it is already finished)
39+
do_split: bool = True
3940

40-
if do_split:
41-
with open(in_file) as f:
42-
lines = f.readlines()
41+
if os.path.exists(h_out):
42+
in_time: float = os.path.getmtime(in_file)
43+
out_time: float = os.path.getmtime(h_out)
44+
do_split: bool = in_time > out_time
45+
46+
if do_split:
47+
with open(in_file) as f:
48+
lines: List[str] = f.readlines()
4349

44-
python_version = sys.version_info[0]
45-
if python_version < 3:
46-
os.makedirs(args.out)
47-
else:
4850
os.makedirs(args.out, exist_ok=True)
4951

50-
in_implementation = False
51-
cc_out = args.out + source_name
52-
with open(h_out, 'w') as fh, open(cc_out, 'w') as fc:
53-
fc.write('#include "httplib.h"\n')
54-
fc.write('namespace httplib {\n')
55-
for line in lines:
56-
is_border_line = border in line
57-
if is_border_line:
58-
in_implementation = not in_implementation
59-
elif in_implementation:
60-
fc.write(line.replace('inline ', ''))
61-
else:
62-
fh.write(line)
63-
fc.write('} // namespace httplib\n')
52+
in_implementation: bool = False
53+
cc_out: str = args.out + source_name
54+
with open(h_out, 'w') as fh, open(cc_out, 'w') as fc:
55+
fc.write('#include "httplib.h"\n')
56+
fc.write('namespace httplib {\n')
57+
for line in lines:
58+
is_border_line: bool = BORDER in line
59+
if is_border_line:
60+
in_implementation: bool = not in_implementation
61+
elif in_implementation:
62+
fc.write(line.replace('inline ', ''))
63+
else:
64+
fh.write(line)
65+
fc.write('} // namespace httplib\n')
66+
67+
print(f"Wrote {h_out} and {cc_out}")
68+
else:
69+
print(f"{h_out} and {cc_out} are up to date")
70+
6471

65-
print("Wrote {} and {}".format(h_out, cc_out))
66-
else:
67-
print("{} and {} are up to date".format(h_out, cc_out))
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)