22
33"""This script splits httplib.h into .h and .cc parts."""
44
5- import argparse
65import os
76import sys
87
9- border = (
8+ BORDER = (
109 "// ----------------------------------------------------------------------------"
1110)
1211
1312
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 ()
13+ def walk_dir (file_name , directory ):
14+ for root , subdirs , files in os .walk (directory ):
15+ if file_name in files :
16+ return os .path .join (root , file_name )
17+ for subdir in subdirs :
18+ return walk_dir (file_name , os .path .join (root , subdir ))
2619
20+
21+ def locate_file (file_name , search_dirs ):
2722 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
23+ initial_path = os .path .join (cur_dir , file_name )
24+
25+ if os .path .isfile (initial_path ):
26+ return initial_path
27+
28+ for directory in search_dirs :
29+ result = walk_dir (file_name , os .path .join (cur_dir , directory ))
30+ if result :
31+ return result
32+
33+ return None
34+
35+
36+ def split (lib_name , search_dirs = [], extension = "cc" , out = "out" ):
37+ header_name = lib_name + ".h"
38+ source_name = lib_name + "." + extension
39+ in_file = locate_file (header_name , search_dirs )
40+ if not in_file :
41+ print ("File not found: {}" .format (header_name ))
42+ return
43+
44+ h_out = os .path .join (out , header_name )
45+ cc_out = os .path .join (out , source_name )
3646
3747 # if the modification time of the out file is after the in file,
3848 # don't split (as it is already finished)
@@ -49,17 +59,16 @@ def main():
4959
5060 python_version = sys .version_info [0 ]
5161 if python_version < 3 :
52- os .makedirs (args . out )
62+ os .makedirs (out )
5363 else :
54- os .makedirs (args . out , exist_ok = True )
64+ os .makedirs (out , exist_ok = True )
5565
5666 in_implementation = False
57- cc_out = args .out + source_name
5867 with open (h_out , "w" ) as fh , open (cc_out , "w" ) as fc :
59- fc .write ('#include "httplib.h "\n ' )
68+ fc .write ('#include "{} "\n ' . format ( header_name ) )
6069 fc .write ("namespace httplib {\n " )
6170 for line in lines :
62- is_border_line = border in line
71+ is_border_line = BORDER in line
6372 if is_border_line :
6473 in_implementation = not in_implementation
6574 elif in_implementation :
@@ -73,5 +82,27 @@ def main():
7382 print ("{} and {} are up to date" .format (h_out , cc_out ))
7483
7584
85+ def main ():
86+ import argparse
87+
88+ args_parser = argparse .ArgumentParser (description = __doc__ )
89+ args_parser .add_argument (
90+ "-e" ,
91+ "--extension" ,
92+ help = "extension of the implementation file (default: cc)" ,
93+ default = "cc" ,
94+ )
95+ args_parser .add_argument (
96+ "-o" , "--out" , help = "where to write the files (default: out)" , default = "out"
97+ )
98+ args = args_parser .parse_args ()
99+
100+ search_dirs = ["example" ]
101+ lib_names = ["httplib" ]
102+
103+ for lib_name in lib_names :
104+ split (lib_name , search_dirs , args .extension , args .out )
105+
106+
76107if __name__ == "__main__" :
77108 main ()
0 commit comments