added method to auto commit generated files

This commit is contained in:
2025-12-31 14:32:21 +13:00
parent ef2f5452c1
commit 28dcbcabc9

View File

@@ -21,9 +21,16 @@ PCB_PDF_FILE_SUFFIX = "_pcb"
SCHEMATIC_OUTPUT_PATH = "docs/" SCHEMATIC_OUTPUT_PATH = "docs/"
SCHEMATIC_FILE_SUFFIX = "_schematic" SCHEMATIC_FILE_SUFFIX = "_schematic"
BOM_OUTPUT_PATH = "docs/"
BOM_REPORT_NAME = "_bom"
TEMP_DRC_REPORT_NAME = "_drc" TEMP_DRC_REPORT_NAME = "_drc"
TEMP_ERC_REPORT_NAME = "_erc" TEMP_ERC_REPORT_NAME = "_erc"
TEMP_BOM_REPORT_NAME = "_bom"
# quiet
STDOUT=subprocess.DEVNULL
# verbose
# STDOUT=subprocess.STDOUT
class OutputReportType(Enum): class OutputReportType(Enum):
JSON = 1 JSON = 1
@@ -35,6 +42,7 @@ class KicadProject:
def __init__(self, path : Path) -> None: def __init__(self, path : Path) -> None:
self.project_path = path.parent self.project_path = path.parent
self.project_name = path.name.removesuffix(".kicad_pro") self.project_name = path.name.removesuffix(".kicad_pro")
self.created_files : list[Path] = []
print(f"{self.project_path=}") print(f"{self.project_path=}")
print(f"{self.project_name=}") print(f"{self.project_name=}")
@@ -46,11 +54,19 @@ class KicadProject:
format_type = report_format.name.lower() format_type = report_format.name.lower()
pcb_file_path = self.project_path / f"{self.project_name}.kicad_sch" pcb_file_path = self.project_path / f"{self.project_name}.kicad_sch"
erc_report_path = Path(TEMP_FILE_PATH) / f"{self.project_name}{TEMP_ERC_REPORT_NAME}.{format_type}" erc_report_path = Path(TEMP_FILE_PATH) / f"{self.project_name}{TEMP_ERC_REPORT_NAME}.{format_type}"
subprocess.call(
retcode = subprocess.call(
f'{KICAD_CLI_PATH} sch erc {pcb_file_path} --output {erc_report_path} --format {format_type}', f'{KICAD_CLI_PATH} sch erc {pcb_file_path} --output {erc_report_path} --format {format_type}',
shell=True, shell=True,
stdout=STDOUT
) )
if (retcode != 0):
print(f"erc check failed return code {retcode}")
exit(1)
self.created_files.append(erc_report_path)
if (return_report): if (return_report):
with open(erc_report_path, "r") as txt: with open(erc_report_path, "r") as txt:
if format_type == OutputReportType.JSON: if format_type == OutputReportType.JSON:
@@ -66,11 +82,18 @@ class KicadProject:
format_type = report_format.name.lower() format_type = report_format.name.lower()
pcb_file_path = self.project_path / f"{self.project_name}.kicad_pcb" pcb_file_path = self.project_path / f"{self.project_name}.kicad_pcb"
drc_report_path = Path(TEMP_FILE_PATH) / f"{self.project_name}{TEMP_DRC_REPORT_NAME}.{format_type}" drc_report_path = Path(TEMP_FILE_PATH) / f"{self.project_name}{TEMP_DRC_REPORT_NAME}.{format_type}"
subprocess.call( retcode = subprocess.call(
f'{KICAD_CLI_PATH} pcb drc {pcb_file_path} --output {drc_report_path} --format {format_type}', f'{KICAD_CLI_PATH} pcb drc {pcb_file_path} --output {drc_report_path} --format {format_type}',
shell=True, shell=True,
stdout=STDOUT
) )
if (retcode != 0):
print(f"drc check failed return code {retcode}")
exit(1)
self.created_files.append(drc_report_path)
if (return_report): if (return_report):
with open(drc_report_path, "r") as txt: with open(drc_report_path, "r") as txt:
if format_type == OutputReportType.JSON: if format_type == OutputReportType.JSON:
@@ -78,13 +101,23 @@ class KicadProject:
if format_type == OutputReportType.RPT: if format_type == OutputReportType.RPT:
return txt.read() return txt.read()
def process_bom(self, return_csv : bool = False) -> None | list[list[str]]: def process_bom(
self,
return_csv : bool = False
) -> None | list[list[str]]:
sch_file_path = self.project_path / f"{self.project_name}.kicad_sch" sch_file_path = self.project_path / f"{self.project_name}.kicad_sch"
bom_output_path = Path(TEMP_FILE_PATH) / f"{self.project_name}{TEMP_DRC_REPORT_NAME}.csv" bom_output_path = Path(BOM_OUTPUT_PATH) / f"{self.project_name}{BOM_REPORT_NAME}.csv"
subprocess.call( retcode = subprocess.call(
f'{KICAD_CLI_PATH} sch export bom {sch_file_path} --output {bom_output_path}', f'{KICAD_CLI_PATH} sch export bom {sch_file_path} --output {bom_output_path}',
shell=True, shell=True,
) stdout=STDOUT
)
if (retcode != 0):
print(f"process_bom failed return code {retcode}")
exit(1)
self.created_files.append(bom_output_path)
if (return_csv): if (return_csv):
with open(bom_output_path, "r") as csvfile: with open(bom_output_path, "r") as csvfile:
bom_csv = csv.reader(csvfile, delimiter=',', quotechar='"') bom_csv = csv.reader(csvfile, delimiter=',', quotechar='"')
@@ -106,44 +139,71 @@ class KicadProject:
""" """
pcb_file_path = self.project_path / f"{self.project_name}.kicad_pcb" pcb_file_path = self.project_path / f"{self.project_name}.kicad_pcb"
render_output_path = Path(PCB_IMAGE_OUTPUT_PATH) / f"{self.project_name}_render.{image_type}" render_output_path = Path(PCB_IMAGE_OUTPUT_PATH) / f"{self.project_name}_render.{image_type}"
subprocess.call( retcode = subprocess.call(
f'{KICAD_CLI_PATH} pcb render {pcb_file_path} --output {render_output_path} --preset {preset} --zoom {zoom} ', f'{KICAD_CLI_PATH} pcb render {pcb_file_path} --output {render_output_path} --preset {preset} --zoom {zoom} ',
shell=True, shell=True,
stdout=STDOUT
) )
if (retcode != 0):
print(f"get_image failed return code {retcode}")
exit(1)
self.created_files.append(render_output_path)
# i am not giving you the pdf to output if you want to do that yourself go ahead # i am not giving you the pdf to output if you want to do that yourself go ahead
def create_schmatic_pdf(self) -> None: def create_schmatic_pdf(self) -> None:
sch_file_path = self.project_path / f"{self.project_name}.kicad_sch" sch_file_path = self.project_path / f"{self.project_name}.kicad_sch"
sch_report_path = Path(SCHEMATIC_OUTPUT_PATH) / f"{self.project_name}{SCHEMATIC_FILE_SUFFIX}.pdf" sch_report_path = Path(SCHEMATIC_OUTPUT_PATH) / f"{self.project_name}{SCHEMATIC_FILE_SUFFIX}.pdf"
subprocess.call( retcode = subprocess.call(
f'{KICAD_CLI_PATH} sch export pdf {sch_file_path} --output {sch_report_path}', f'{KICAD_CLI_PATH} sch export pdf {sch_file_path} --output {sch_report_path}',
shell=True, shell=True,
stdout=STDOUT
) )
if (retcode != 0):
print(f"create_schmatic_pdf failed return code {retcode}")
exit(1)
self.created_files.append(sch_report_path)
def create_pcb_pdf(self, layers : list[str] = ["F.Cu", "B.Cu"]) -> None: def create_pcb_pdf(self, layers : list[str] = ["F.Cu", "B.Cu"]) -> None:
pcb_file_path = self.project_path / f"{self.project_name}.kicad_pcb" pcb_file_path = self.project_path / f"{self.project_name}.kicad_pcb"
pcb_report_path = Path(PCB_PDF_OUTPUT_PATH) / f"{self.project_name}{PCB_PDF_FILE_SUFFIX}.pdf" pcb_report_path = Path(PCB_PDF_OUTPUT_PATH) / f"{self.project_name}{PCB_PDF_FILE_SUFFIX}.pdf"
subprocess.call( retcode = subprocess.call(
f'{KICAD_CLI_PATH} pcb export pdf {pcb_file_path} --output {pcb_report_path} --layers {",".join(layers)}', f'{KICAD_CLI_PATH} pcb export pdf {pcb_file_path} --output {pcb_report_path} --layers {",".join(layers)}',
shell=True, shell=True,
stdout=STDOUT
) )
def main() -> None: if (retcode != 0):
print(f"create_pcb_pdf failed return code {retcode}")
exit(1)
self.created_files.append(pcb_report_path)
def commit_files(files: list[Path], commit_message : str) -> None:
for file in files:
# add & commit, could use the return code however these should never fail
print(f"adding and commiting {file}")
ret_add = subprocess.call(f"git add {file}", shell=True)
ret_commit = subprocess.call(f"git commit -m \"{commit_message}\"", shell=True)
def main() -> None:
# find all kicad project files to operate on # find all kicad project files to operate on
for path in Path(".").rglob('*.kicad_pro'): for path in Path(".").rglob('*.kicad_pro'):
print(path.name)
print(path)
print(type(path.parent))
k = KicadProject(path) k = KicadProject(path)
k.drc_check() k.drc_check()
k.erc_check() k.erc_check()
k.process_bom() k.process_bom()
k.create_schmatic_pdf() k.create_schmatic_pdf()
k.create_pcb_pdf()
k.get_image() k.get_image()
commit_files(k.created_files, "auto commited")
print("hello world");
if __name__ == "__main__": if __name__ == "__main__":
main() try:
main()
exit(0)
except Exception as e:
exit(1)