From edafc47d0b3c20638b29f45adbd3362a0345a4a0 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Tue, 9 Jan 2024 10:53:55 -0300 Subject: [PATCH] [KiRi] Check we have the SCH and PCB in the repo - At least one commit --- kibot/out_any_diff.py | 30 ++++++++++++++++++++++++++++++ kibot/out_diff.py | 25 +++---------------------- kibot/out_kiri.py | 15 +++++++++++---- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/kibot/out_any_diff.py b/kibot/out_any_diff.py index d609f859..459d55cf 100644 --- a/kibot/out_any_diff.py +++ b/kibot/out_any_diff.py @@ -3,6 +3,7 @@ # Copyright (c) 2022-2024 Instituto Nacional de TecnologĂ­a Industrial # License: GPL-3.0 # Project: KiBot (formerly KiPlot) +import os from tempfile import NamedTemporaryFile from .gs import GS from .kiplot import run_command @@ -66,6 +67,35 @@ class AnyDiffOptions(VariantOptions): logger.debug('Removing temporal checkout at '+name) self.run_git(['worktree', 'remove', '--force', name]) + def write_empty_file(self, name, create_tmp=False): + to_remove = [name] + base, ext = os.path.splitext(name) + kind = 'PCB' if ext == '.kicad_pcb' else 'schematic' + if create_tmp: + # Use a temporary file + with NamedTemporaryFile(mode='w', suffix=ext, delete=False) as f: + name = f.name + base = os.path.splitext(name)[0] + logger.debug('Creating empty '+kind+': '+name) + with open(name, 'w') as f: + if ext == '.kicad_sch': + f.write("(kicad_sch (version 20211123) (generator eeschema))\n") + elif ext == '.sch': + f.write("EESchema Schematic File Version 4\nEELAYER 30 0\nEELAYER END\n$Descr A4 11693 8268\n" + "$EndDescr\n$EndSCHEMATC\n") + elif ext == '.kicad_pcb': + f.write("(kicad_pcb (version 20171130) (host pcbnew 5.1.5))\n") + else: # pragma: no cover + raise AssertionError('Unknown extension') + if ext == '.sch': + lib_name = base+'-cache.lib' + if not os.path.isfile(lib_name): + logger.debug('Creating dummy cache lib: '+lib_name) + with open(lib_name, 'w') as f: + f.write("EESchema-LIBRARY Version 2.4\n#\n#End Library\n") + to_remove.append(lib_name) + return to_remove + def save_layers_incl(self, layers): self._solved_layers = layers logger.debug('Including layers:') diff --git a/kibot/out_diff.py b/kibot/out_diff.py index a2cae6f1..17572262 100644 --- a/kibot/out_diff.py +++ b/kibot/out_diff.py @@ -25,7 +25,7 @@ import os import re from shutil import rmtree, copy2 from subprocess import CalledProcessError -from tempfile import mkdtemp, NamedTemporaryFile +from tempfile import mkdtemp from .error import KiPlotConfigurationError from .gs import GS from .kiplot import load_any_sch, run_command, config_output, get_output_dir, run_output @@ -149,10 +149,7 @@ class DiffOptions(AnyDiffOptions): if not os.path.isfile(name): if self.always_fail_if_missing: raise KiPlotConfigurationError('Missing file to compare: `{}`'.format(name)) - with NamedTemporaryFile(mode='w', suffix='.kicad_pcb', delete=False) as f: - f.write("(kicad_pcb (version 20171130) (host pcbnew 5.1.5))\n") - name = f.name - self._to_remove.append(name) + self._to_remove.extend(self.write_empty_file(name, create_tmp=True)) hash = self.get_digest(name) self.add_to_cache(name, hash) return hash @@ -167,23 +164,7 @@ class DiffOptions(AnyDiffOptions): if not os.path.isfile(name): if self.always_fail_if_missing: raise KiPlotConfigurationError('Missing file to compare: `{}`'.format(name)) - ext = os.path.splitext(name)[1] - with NamedTemporaryFile(mode='w', suffix=ext, delete=False) as f: - logger.debug('Creating empty schematic: '+f.name) - if ext == '.kicad_sch': - f.write("(kicad_sch (version 20211123) (generator eeschema))\n") - else: - f.write("EESchema Schematic File Version 4\nEELAYER 30 0\nEELAYER END\n$Descr A4 11693 8268\n" - "$EndDescr\n$EndSCHEMATC\n") - name = f.name - self._to_remove.append(name) - if ext != '.kicad_sch': - lib_name = os.path.splitext(name)[0]+'-cache.lib' - if not os.path.isfile(lib_name): - logger.debug('Creating dummy cache lib: '+lib_name) - with open(lib_name, 'w') as f: - f.write("EESchema-LIBRARY Version 2.4\n#\n#End Library\n") - self._to_remove.append(lib_name) + self._to_remove.extend(self.write_empty_file(name, create_tmp=True)) # Schematics can have sub-sheets sch = load_any_sch(name, os.path.splitext(os.path.basename(name))[0]) files = sch.get_files() diff --git a/kibot/out_kiri.py b/kibot/out_kiri.py index fe3e3669..021b9eb9 100644 --- a/kibot/out_kiri.py +++ b/kibot/out_kiri.py @@ -99,8 +99,9 @@ class KiRiOptions(AnyDiffOptions): return True def do_cache(self, name, tmp_wd, hash): - name_copy = self.run_git(['ls-files', '--full-name', name]) - name_copy = os.path.join(tmp_wd, name_copy) + name_copy = os.path.join(tmp_wd, name) + if not os.path.isfile(name_copy): + self.write_empty_file(name_copy) logger.debug('- Using temporal copy: '+name_copy) self.add_to_cache(name_copy, hash[:7]) return name_copy @@ -242,7 +243,13 @@ class KiRiOptions(AnyDiffOptions): GS.check_sch() sch_files = GS.sch.get_files() self.repo_dir = GS.sch_dir + self.sch_rel_name = self.run_git(['ls-files', '--full-name', GS.sch_file]) + if not self.sch_rel_name: + raise KiPlotConfigurationError("The schematic must be committed") GS.check_pcb() + self.pcb_rel_name = self.run_git(['ls-files', '--full-name', GS.pcb_file]) + if not self.pcb_rel_name: + raise KiPlotConfigurationError("The PCB must be committed") # Get a list of hashes where we have changes self._max_commits = ['-n', str(self.max_commits)] if self.max_commits else [] cmd = ['log', "--date=format:%Y-%m-%d %H:%M:%S", '--pretty=format:%H | %ad | %an | %s'] @@ -280,9 +287,9 @@ class KiRiOptions(AnyDiffOptions): self.run_git(['worktree', 'add', git_tmp_wd, hash]) self.run_git(['submodule', 'update', '--init', '--recursive'], cwd=git_tmp_wd) # Generate SVGs for the schematic - name_sch = self.do_cache(GS.sch_file, git_tmp_wd, hash) + name_sch = self.do_cache(self.sch_rel_name, git_tmp_wd, hash) # Generate SVGs for the PCB - self.do_cache(GS.pcb_file, git_tmp_wd, hash) + self.do_cache(self.pcb_rel_name, git_tmp_wd, hash) # List of layers self.save_pcb_layers(hash) # Schematic hierarchy