diff --git a/Makefile b/Makefile index 7687fec3..9c62ee24 100644 --- a/Makefile +++ b/Makefile @@ -295,8 +295,8 @@ src/conf_templates.c: tbl tbl: @echo Generating tables @$(PYTHON) tools/create_hal_tbl.py . $(COMPS) - @$(PYTHON) tools/create_config.py conf/template/* > src/conf_templates.c - @$(PYTHON) tools/create_cmd.py $(SOURCES) > inc/commandslist.h + @$(PYTHON) tools/create_config.py src/conf_templates.c conf/template/* + @$(PYTHON) tools/create_cmd.py inc/commandslist.h $(SOURCES) #build f4 bootloader boot: diff --git a/stm32f303/Makefile b/stm32f303/Makefile index 9c884abe..76bd0965 100644 --- a/stm32f303/Makefile +++ b/stm32f303/Makefile @@ -201,7 +201,7 @@ tbl: @echo Generating tables @$(PYTHON) tools/create_hal_tbl.py stm32f303/ $(COMPS) #shared/comps/*.c src/comps/hw/*.c src/comps/*.c - @$(PYTHON) tools/create_cmd.py $(SOURCES) > stm32f303/inc/commandslist.h + @$(PYTHON) tools/create_cmd.py stm32f303/inc/commandslist.h $(SOURCES) # Target: clean project # diff --git a/tools/create_cmd.py b/tools/create_cmd.py index 561b77f0..e19ac664 100755 --- a/tools/create_cmd.py +++ b/tools/create_cmd.py @@ -1,24 +1,29 @@ #!/usr/bin/env python import re import sys +import file_updater cmd = [] -for infile in sys.argv[1:]: +for infile in sys.argv[2:]: with open(infile) as f: for line_number, line in enumerate(f): match = re.search('COMMAND\("(\w*)", *(\w*), *"([^"]*)"\);', line) if match: cmd.append((match.groups(), infile, line_number)) -print ("//generated by " + sys.argv[0] + " DO NOT EDIT") +header = file_updater.FileUpdater(sys.argv[1]) + +header.write("//generated by " + sys.argv[0] + " DO NOT EDIT\n") for (name, ptr, doc), file_name, line_number in cmd: - print ("extern void " + ptr + "(char *); // found in " + file_name + " line: " + str(line_number + 1)) + header.write("extern void " + ptr + "(char *); // found in " + file_name + " line: " + str(line_number + 1) + "\n") -print ("\n") +header.write("\n\n") -print ("cmd_t cmd[] = {") +header.write("cmd_t cmd[] = {\n") for (name, ptr, doc), file_name, line_number in cmd: - print (" {\"" + name + "\", \"" + doc + "\", " + ptr + "}, // found in " + file_name + " line: " + str(line_number + 1)) -print ("};") + header.write(" {\"" + name + "\", \"" + doc + "\", " + ptr + "}, // found in " + file_name + " line: " + str(line_number + 1) + "\n") +header.write("};\n") + +header.close() diff --git a/tools/create_config.py b/tools/create_config.py index 03506fef..b2704aff 100755 --- a/tools/create_config.py +++ b/tools/create_config.py @@ -2,24 +2,29 @@ import re import sys import os +import file_updater config = [] -for infile in sys.argv[1:]: +for infile in sys.argv[2:]: with open(infile) as f: config.append((os.path.splitext(os.path.basename(infile))[0], f.read())) -print ("//generated by " + sys.argv[0] + " DO NOT EDIT\n") -print ("#include \"config.h\"\n") -print ("const uint32_t num_of_config_templates = " + str(len(config)) + ";\n") +code = file_updater.FileUpdater(sys.argv[1]) -print ("config_template_t config_templates[] = {") +code.write("//generated by " + sys.argv[0] + " DO NOT EDIT\n\n") +code.write("#include \"config.h\"\n\n") +code.write("const uint32_t num_of_config_templates = " + str(len(config)) + ";\n\n") + +code.write("config_template_t config_templates[] = {\n") for index, (file_name, content) in enumerate(config): - print ("{") - print (".name = \"" + file_name + "\",") - print (".config = \"\\") + code.write("{\n") + code.write(".name = \"" + file_name + "\",\n") + code.write(".config = \"\\\n") for line in content.splitlines(): - print (line + "\\n\\") - print ("\"\n},\n") -print ("};") + code.write(line + "\\n\\\n") + code.write("\"\n},\n\n") +code.write("};\n") + +code.close() diff --git a/tools/create_hal_tbl.py b/tools/create_hal_tbl.py index 2e79aab6..e31a5996 100755 --- a/tools/create_hal_tbl.py +++ b/tools/create_hal_tbl.py @@ -1,11 +1,12 @@ #!/usr/bin/env python import re import sys +import file_updater comps = [] -header = open(sys.argv[1] + '/inc/hal_tbl.h', 'w') -code = open(sys.argv[1] + '/src/hal_tbl.c', 'w') +header = file_updater.FileUpdater(sys.argv[1] + '/inc/hal_tbl.h') +code = file_updater.FileUpdater(sys.argv[1] + '/src/hal_tbl.c') for infile in sys.argv[2:]: with open(infile) as f: @@ -65,7 +66,7 @@ for comp_name, pins, file_name in comps: header.write("extern const hal_comp_t " + comp_name + "_comp_struct; // found in " + file_name + "\n") -code.write("#include \"hal.h\"\n") +code.write("#include \"hal.h\"\n\n") code.write("//generated by " + sys.argv[0] + " DO NOT EDIT\n\n") code.write("const hal_comp_t * comps[] = {\n") for comp_name, pins, file_name in comps: diff --git a/tools/file_updater.py b/tools/file_updater.py new file mode 100644 index 00000000..d82f103a --- /dev/null +++ b/tools/file_updater.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +import shutil +import tempfile +import filecmp + +def copyIfDifferent(src, dst_path): + dst = None + try: + dst = open(dst_path, 'rw+b') + src.seek(0) + if src.read() == dst.read(): + return + dst.seek(0) + except: + pass + if dst is None: + dst = open(dst_path, 'w+b') + src.seek(0) + shutil.copyfileobj(src, dst) + +class FileUpdater: + def __init__(self, path): + self.filePath = path + self.tempFile = tempfile.TemporaryFile(mode='rw+b') + + def __del__(self): + if not self.tempFile.closed: + self.close() + + def write(self, str): + self.tempFile.write(str) + + def close(self): + copyIfDifferent(self.tempFile, self.filePath) + self.tempFile.close()