From 3f4e9056e9d3bd66082858aff46afc9f19d1002d Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 5 Feb 2025 17:17:42 +0100 Subject: [PATCH 1/6] Run black code formatter on split.py --- split.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/split.py b/split.py index 4d8b307417..ac774c7da9 100755 --- a/split.py +++ b/split.py @@ -6,12 +6,16 @@ import os import sys -border = '// ----------------------------------------------------------------------------' +border = ( + "// ----------------------------------------------------------------------------" +) args_parser = argparse.ArgumentParser(description=__doc__) args_parser.add_argument( - "-e", "--extension", help="extension of the implementation file (default: cc)", - default="cc" + "-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" @@ -19,9 +23,9 @@ args = args_parser.parse_args() cur_dir = os.path.dirname(sys.argv[0]) -lib_name = 'httplib' -header_name = '/' + lib_name + '.h' -source_name = '/' + lib_name + '.' + args.extension +lib_name = "httplib" +header_name = "/" + lib_name + ".h" +source_name = "/" + lib_name + "." + args.extension # get the input file in_file = cur_dir + header_name # get the output file @@ -49,18 +53,18 @@ 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('namespace httplib {\n') + fc.write("namespace httplib {\n") for line in lines: is_border_line = border in line if is_border_line: in_implementation = not in_implementation elif in_implementation: - fc.write(line.replace('inline ', '')) + fc.write(line.replace("inline ", "")) else: fh.write(line) - fc.write('} // namespace httplib\n') + fc.write("} // namespace httplib\n") print("Wrote {} and {}".format(h_out, cc_out)) else: From 50816d4c0894198df4ce0e94f3145911c69dcee8 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 5 Feb 2025 17:25:22 +0100 Subject: [PATCH 2/6] Follow good practice and add main() to split.py --- split.py | 108 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/split.py b/split.py index ac774c7da9..66a75f2029 100755 --- a/split.py +++ b/split.py @@ -10,62 +10,68 @@ "// ----------------------------------------------------------------------------" ) -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() -cur_dir = os.path.dirname(sys.argv[0]) -lib_name = "httplib" -header_name = "/" + lib_name + ".h" -source_name = "/" + lib_name + "." + args.extension -# get the input file -in_file = cur_dir + header_name -# get the output file -h_out = args.out + header_name -cc_out = args.out + source_name +def main(): + 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() + + cur_dir = os.path.dirname(sys.argv[0]) + lib_name = "httplib" + header_name = "/" + lib_name + ".h" + source_name = "/" + lib_name + "." + args.extension + # get the input file + in_file = cur_dir + header_name + # get the output file + h_out = args.out + header_name + cc_out = args.out + source_name -# if the modification time of the out file is after the in file, -# don't split (as it is already finished) -do_split = True + # if the modification time of the out file is after the in file, + # don't split (as it is already finished) + do_split = True -if os.path.exists(h_out): - in_time = os.path.getmtime(in_file) - out_time = os.path.getmtime(h_out) - do_split = in_time > out_time + if os.path.exists(h_out): + in_time = os.path.getmtime(in_file) + out_time = os.path.getmtime(h_out) + do_split = in_time > out_time -if do_split: - with open(in_file) as f: - lines = f.readlines() + if do_split: + with open(in_file) as f: + lines = f.readlines() - python_version = sys.version_info[0] - if python_version < 3: - os.makedirs(args.out) + python_version = sys.version_info[0] + if python_version < 3: + os.makedirs(args.out) + else: + os.makedirs(args.out, exist_ok=True) + + in_implementation = False + cc_out = args.out + source_name + with open(h_out, "w") as fh, open(cc_out, "w") as fc: + fc.write('#include "httplib.h"\n') + fc.write("namespace httplib {\n") + for line in lines: + is_border_line = border in line + if is_border_line: + in_implementation = not in_implementation + elif in_implementation: + fc.write(line.replace("inline ", "")) + else: + fh.write(line) + fc.write("} // namespace httplib\n") + + print("Wrote {} and {}".format(h_out, cc_out)) else: - os.makedirs(args.out, exist_ok=True) + print("{} and {} are up to date".format(h_out, cc_out)) - in_implementation = False - cc_out = args.out + source_name - with open(h_out, "w") as fh, open(cc_out, "w") as fc: - fc.write('#include "httplib.h"\n') - fc.write("namespace httplib {\n") - for line in lines: - is_border_line = border in line - if is_border_line: - in_implementation = not in_implementation - elif in_implementation: - fc.write(line.replace("inline ", "")) - else: - fh.write(line) - fc.write("} // namespace httplib\n") - print("Wrote {} and {}".format(h_out, cc_out)) -else: - print("{} and {} are up to date".format(h_out, cc_out)) +if __name__ == "__main__": + main() From 947e740c0e7bf5115f4c1b690cb19d50855c624b Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 5 Feb 2025 18:09:55 +0100 Subject: [PATCH 3/6] Generalize split.py Make split.py usable on other httplib files. --- split.py | 85 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/split.py b/split.py index 66a75f2029..3a62e6d507 100755 --- a/split.py +++ b/split.py @@ -2,37 +2,47 @@ """This script splits httplib.h into .h and .cc parts.""" -import argparse import os import sys -border = ( +BORDER = ( "// ----------------------------------------------------------------------------" ) -def main(): - 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() +def walk_dir(file_name, directory): + for root, subdirs, files in os.walk(directory): + if file_name in files: + return os.path.join(root, file_name) + for subdir in subdirs: + return walk_dir(file_name, os.path.join(root, subdir)) + +def locate_file(file_name, search_dirs): cur_dir = os.path.dirname(sys.argv[0]) - lib_name = "httplib" - header_name = "/" + lib_name + ".h" - source_name = "/" + lib_name + "." + args.extension - # get the input file - in_file = cur_dir + header_name - # get the output file - h_out = args.out + header_name - cc_out = args.out + source_name + initial_path = os.path.join(cur_dir, file_name) + + if os.path.isfile(initial_path): + return initial_path + + for directory in search_dirs: + result = walk_dir(file_name, os.path.join(cur_dir, directory)) + 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, # don't split (as it is already finished) @@ -49,17 +59,16 @@ def main(): python_version = sys.version_info[0] if python_version < 3: - os.makedirs(args.out) + os.makedirs(out) else: - os.makedirs(args.out, exist_ok=True) + os.makedirs(out, exist_ok=True) in_implementation = False - cc_out = args.out + source_name 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") for line in lines: - is_border_line = border in line + is_border_line = BORDER in line if is_border_line: in_implementation = not in_implementation elif in_implementation: @@ -73,5 +82,27 @@ def main(): 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__": main() From f965fd9fb01238d264e5ac970b529301e1e9874c Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 5 Feb 2025 18:19:36 +0100 Subject: [PATCH 4/6] Add libraries list option to split.py Overrides the default list of libraries to split. --- split.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/split.py b/split.py index 3a62e6d507..a6374e6f8b 100755 --- a/split.py +++ b/split.py @@ -95,12 +95,19 @@ def main(): args_parser.add_argument( "-o", "--out", help="where to write the files (default: out)", default="out" ) + args_parser.add_argument( + "-l", + "--library", + action="append", + dest="libraries", + help="the libraries to split (default: [httplib])", + ) args = args_parser.parse_args() + default_libraries = ["httplib"] search_dirs = ["example"] - lib_names = ["httplib"] - for lib_name in lib_names: + for lib_name in args.libraries or default_libraries: split(lib_name, search_dirs, args.extension, args.out) From 87feeea267c3186a0286d0ee49a1698a4dfbf206 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 5 Feb 2025 18:33:03 +0100 Subject: [PATCH 5/6] Don't replace "inline namespace" in split.py --- split.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/split.py b/split.py index a6374e6f8b..476f48cb12 100755 --- a/split.py +++ b/split.py @@ -3,11 +3,13 @@ """This script splits httplib.h into .h and .cc parts.""" import os +import re import sys BORDER = ( "// ----------------------------------------------------------------------------" ) +INLINE_RE = re.compile(r"inline(?! namespace) ") def walk_dir(file_name, directory): @@ -72,7 +74,7 @@ def split(lib_name, search_dirs=[], extension="cc", out="out"): if is_border_line: in_implementation = not in_implementation elif in_implementation: - fc.write(line.replace("inline ", "")) + fc.write(INLINE_RE.sub("", line)) else: fh.write(line) fc.write("} // namespace httplib\n") From d8b2894007dc0e92ec01000f84368ef3afcc76bc Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 5 Feb 2025 19:29:43 +0100 Subject: [PATCH 6/6] Add base directory option to split.py Overrides the directory to look for libraries. --- split.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/split.py b/split.py index 476f48cb12..a417472206 100755 --- a/split.py +++ b/split.py @@ -20,8 +20,8 @@ def walk_dir(file_name, directory): return walk_dir(file_name, os.path.join(root, subdir)) -def locate_file(file_name, search_dirs): - cur_dir = os.path.dirname(sys.argv[0]) +def locate_file(file_name, search_dirs, base_directory=None): + cur_dir = base_directory or os.path.dirname(sys.argv[0]) initial_path = os.path.join(cur_dir, file_name) if os.path.isfile(initial_path): @@ -35,10 +35,10 @@ def locate_file(file_name, search_dirs): return None -def split(lib_name, search_dirs=[], extension="cc", out="out"): +def split(lib_name, search_dirs=[], extension="cc", out="out", base_directory=None): header_name = lib_name + ".h" source_name = lib_name + "." + extension - in_file = locate_file(header_name, search_dirs) + in_file = locate_file(header_name, search_dirs, base_directory) if not in_file: print("File not found: {}".format(header_name)) return @@ -104,13 +104,24 @@ def main(): dest="libraries", help="the libraries to split (default: [httplib])", ) + args_parser.add_argument( + "-b", + "--base-directory", + help="where to look for files (default: