|
10 | 10 | "// ----------------------------------------------------------------------------" |
11 | 11 | ) |
12 | 12 |
|
13 | | -args_parser = argparse.ArgumentParser(description=__doc__) |
14 | | -args_parser.add_argument( |
15 | | - "-e", |
16 | | - "--extension", |
17 | | - 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 = args_parser.parse_args() |
24 | 13 |
|
25 | | -cur_dir = os.path.dirname(sys.argv[0]) |
26 | | -lib_name = "httplib" |
27 | | -header_name = "/" + lib_name + ".h" |
28 | | -source_name = "/" + lib_name + "." + args.extension |
29 | | -# get the input file |
30 | | -in_file = cur_dir + header_name |
31 | | -# get the output file |
32 | | -h_out = args.out + header_name |
33 | | -cc_out = args.out + source_name |
| 14 | +def main(): |
| 15 | + args_parser = argparse.ArgumentParser(description=__doc__) |
| 16 | + args_parser.add_argument( |
| 17 | + "-e", |
| 18 | + "--extension", |
| 19 | + help="extension of the implementation file (default: cc)", |
| 20 | + default="cc", |
| 21 | + ) |
| 22 | + args_parser.add_argument( |
| 23 | + "-o", "--out", help="where to write the files (default: out)", default="out" |
| 24 | + ) |
| 25 | + args = args_parser.parse_args() |
| 26 | + |
| 27 | + cur_dir = os.path.dirname(sys.argv[0]) |
| 28 | + lib_name = "httplib" |
| 29 | + header_name = "/" + lib_name + ".h" |
| 30 | + source_name = "/" + lib_name + "." + args.extension |
| 31 | + # get the input file |
| 32 | + in_file = cur_dir + header_name |
| 33 | + # get the output file |
| 34 | + h_out = args.out + header_name |
| 35 | + cc_out = args.out + source_name |
34 | 36 |
|
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 = True |
| 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 = True |
38 | 40 |
|
39 | | -if os.path.exists(h_out): |
40 | | - in_time = os.path.getmtime(in_file) |
41 | | - out_time = os.path.getmtime(h_out) |
42 | | - do_split = in_time > out_time |
| 41 | + if os.path.exists(h_out): |
| 42 | + in_time = os.path.getmtime(in_file) |
| 43 | + out_time = os.path.getmtime(h_out) |
| 44 | + do_split = in_time > out_time |
43 | 45 |
|
44 | | -if do_split: |
45 | | - with open(in_file) as f: |
46 | | - lines = f.readlines() |
| 46 | + if do_split: |
| 47 | + with open(in_file) as f: |
| 48 | + lines = f.readlines() |
47 | 49 |
|
48 | | - python_version = sys.version_info[0] |
49 | | - if python_version < 3: |
50 | | - os.makedirs(args.out) |
| 50 | + python_version = sys.version_info[0] |
| 51 | + if python_version < 3: |
| 52 | + os.makedirs(args.out) |
| 53 | + else: |
| 54 | + os.makedirs(args.out, exist_ok=True) |
| 55 | + |
| 56 | + in_implementation = False |
| 57 | + cc_out = args.out + source_name |
| 58 | + with open(h_out, "w") as fh, open(cc_out, "w") as fc: |
| 59 | + fc.write('#include "httplib.h"\n') |
| 60 | + fc.write("namespace httplib {\n") |
| 61 | + for line in lines: |
| 62 | + is_border_line = border in line |
| 63 | + if is_border_line: |
| 64 | + in_implementation = not in_implementation |
| 65 | + elif in_implementation: |
| 66 | + fc.write(line.replace("inline ", "")) |
| 67 | + else: |
| 68 | + fh.write(line) |
| 69 | + fc.write("} // namespace httplib\n") |
| 70 | + |
| 71 | + print("Wrote {} and {}".format(h_out, cc_out)) |
51 | 72 | else: |
52 | | - os.makedirs(args.out, exist_ok=True) |
| 73 | + print("{} and {} are up to date".format(h_out, cc_out)) |
53 | 74 |
|
54 | | - in_implementation = False |
55 | | - cc_out = 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 = border in line |
61 | | - if is_border_line: |
62 | | - in_implementation = 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 | 75 |
|
69 | | - print("Wrote {} and {}".format(h_out, cc_out)) |
70 | | -else: |
71 | | - print("{} and {} are up to date".format(h_out, cc_out)) |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments