|
2 | 2 |
|
3 | 3 | """This script splits httplib.h into .h and .cc parts.""" |
4 | 4 |
|
5 | | -import argparse |
6 | 5 | import os |
7 | 6 | import sys |
| 7 | +from argparse import ArgumentParser, Namespace |
| 8 | +from typing import List |
8 | 9 |
|
9 | | -border = '// ----------------------------------------------------------------------------' |
10 | 10 |
|
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 = '// ----------------------------------------------------------------------------' |
20 | 14 |
|
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() |
30 | 24 |
|
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 | + lib_name: str = 'httplib' |
| 27 | + header_name: str = f"/{lib_name}.h" |
| 28 | + source_name: str = f"/{lib_name}.{args.extension}" |
| 29 | + # get the input file |
| 30 | + in_file: str = cur_dir + header_name |
| 31 | + # get the output file |
| 32 | + h_out: str = args.out + header_name |
| 33 | + cc_out: str = args.out + source_name |
34 | 34 |
|
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 |
| 35 | + # if the modification time of the out file is after the in file, |
| 36 | + # don't split (as it is already finished) |
| 37 | + do_split: bool = True |
39 | 38 |
|
40 | | -if do_split: |
41 | | - with open(in_file) as f: |
42 | | - lines = f.readlines() |
| 39 | + if os.path.exists(h_out): |
| 40 | + in_time: float = os.path.getmtime(in_file) |
| 41 | + out_time: float = os.path.getmtime(h_out) |
| 42 | + do_split: bool = in_time > out_time |
43 | 43 |
|
44 | | - python_version = sys.version_info[0] |
45 | | - if python_version < 3: |
46 | | - os.makedirs(args.out) |
| 44 | + if do_split: |
| 45 | + with open(in_file) as f: |
| 46 | + lines: List[str] = f.readlines() |
| 47 | + |
| 48 | + python_version: int = sys.version_info[0] |
| 49 | + if python_version < 3: |
| 50 | + os.makedirs(args.out) |
| 51 | + else: |
| 52 | + os.makedirs(args.out, exist_ok=True) |
| 53 | + |
| 54 | + in_implementation: bool = False |
| 55 | + cc_out: str = args.out + source_name |
| 56 | + with open(h_out, 'w') as fh, open(cc_out, 'w') as fc: |
| 57 | + fc.write('#include "httplib.h"\n') |
| 58 | + fc.write('namespace httplib {\n') |
| 59 | + for line in lines: |
| 60 | + is_border_line: bool = BORDER in line |
| 61 | + if is_border_line: |
| 62 | + in_implementation: bool = not in_implementation |
| 63 | + elif in_implementation: |
| 64 | + fc.write(line.replace('inline ', '')) |
| 65 | + else: |
| 66 | + fh.write(line) |
| 67 | + fc.write('} // namespace httplib\n') |
| 68 | + |
| 69 | + print(f"Wrote {h_out} and {cc_out}") |
47 | 70 | else: |
48 | | - os.makedirs(args.out, exist_ok=True) |
| 71 | + print(f"{h_out} and {cc_out} are up to date") |
49 | 72 |
|
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') |
64 | 73 |
|
65 | | - print("Wrote {} and {}".format(h_out, cc_out)) |
66 | | -else: |
67 | | - print("{} and {} are up to date".format(h_out, cc_out)) |
| 74 | +if __name__ == "__main__": |
| 75 | + main() |
0 commit comments