Generalize split.py
Make split.py usable on other httplib files.
This commit is contained in:
parent
50816d4c08
commit
947e740c0e
85
split.py
85
split.py
@ -2,37 +2,47 @@
|
|||||||
|
|
||||||
"""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
|
||||||
|
|
||||||
border = (
|
BORDER = (
|
||||||
"// ----------------------------------------------------------------------------"
|
"// ----------------------------------------------------------------------------"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def walk_dir(file_name, directory):
|
||||||
args_parser = argparse.ArgumentParser(description=__doc__)
|
for root, subdirs, files in os.walk(directory):
|
||||||
args_parser.add_argument(
|
if file_name in files:
|
||||||
"-e",
|
return os.path.join(root, file_name)
|
||||||
"--extension",
|
for subdir in subdirs:
|
||||||
help="extension of the implementation file (default: cc)",
|
return walk_dir(file_name, os.path.join(root, subdir))
|
||||||
default="cc",
|
|
||||||
)
|
|
||||||
args_parser.add_argument(
|
|
||||||
"-o", "--out", help="where to write the files (default: out)", default="out"
|
|
||||||
)
|
|
||||||
args = args_parser.parse_args()
|
|
||||||
|
|
||||||
|
|
||||||
|
def locate_file(file_name, search_dirs):
|
||||||
cur_dir = os.path.dirname(sys.argv[0])
|
cur_dir = os.path.dirname(sys.argv[0])
|
||||||
lib_name = "httplib"
|
initial_path = os.path.join(cur_dir, file_name)
|
||||||
header_name = "/" + lib_name + ".h"
|
|
||||||
source_name = "/" + lib_name + "." + args.extension
|
if os.path.isfile(initial_path):
|
||||||
# get the input file
|
return initial_path
|
||||||
in_file = cur_dir + header_name
|
|
||||||
# get the output file
|
for directory in search_dirs:
|
||||||
h_out = args.out + header_name
|
result = walk_dir(file_name, os.path.join(cur_dir, directory))
|
||||||
cc_out = args.out + source_name
|
if result:
|
||||||
|
return result
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def split(lib_name, search_dirs=[], extension="cc", out="out"):
|
||||||
|
header_name = lib_name + ".h"
|
||||||
|
source_name = lib_name + "." + extension
|
||||||
|
in_file = locate_file(header_name, search_dirs)
|
||||||
|
if not in_file:
|
||||||
|
print("File not found: {}".format(header_name))
|
||||||
|
return
|
||||||
|
|
||||||
|
h_out = os.path.join(out, header_name)
|
||||||
|
cc_out = os.path.join(out, source_name)
|
||||||
|
|
||||||
# if the modification time of the out file is after the in file,
|
# if the modification time of the out file is after the in file,
|
||||||
# don't split (as it is already finished)
|
# don't split (as it is already finished)
|
||||||
@ -49,17 +59,16 @@ def main():
|
|||||||
|
|
||||||
python_version = sys.version_info[0]
|
python_version = sys.version_info[0]
|
||||||
if python_version < 3:
|
if python_version < 3:
|
||||||
os.makedirs(args.out)
|
os.makedirs(out)
|
||||||
else:
|
else:
|
||||||
os.makedirs(args.out, exist_ok=True)
|
os.makedirs(out, exist_ok=True)
|
||||||
|
|
||||||
in_implementation = False
|
in_implementation = False
|
||||||
cc_out = 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 "{}"\n'.format(header_name))
|
||||||
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 = BORDER in line
|
||||||
if is_border_line:
|
if is_border_line:
|
||||||
in_implementation = not in_implementation
|
in_implementation = not in_implementation
|
||||||
elif in_implementation:
|
elif in_implementation:
|
||||||
@ -73,5 +82,27 @@ def main():
|
|||||||
print("{} and {} are up to date".format(h_out, cc_out))
|
print("{} and {} are up to date".format(h_out, cc_out))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
args_parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
args_parser.add_argument(
|
||||||
|
"-e",
|
||||||
|
"--extension",
|
||||||
|
help="extension of the implementation file (default: cc)",
|
||||||
|
default="cc",
|
||||||
|
)
|
||||||
|
args_parser.add_argument(
|
||||||
|
"-o", "--out", help="where to write the files (default: out)", default="out"
|
||||||
|
)
|
||||||
|
args = args_parser.parse_args()
|
||||||
|
|
||||||
|
search_dirs = ["example"]
|
||||||
|
lib_names = ["httplib"]
|
||||||
|
|
||||||
|
for lib_name in lib_names:
|
||||||
|
split(lib_name, search_dirs, args.extension, args.out)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user