From 52401b170d8f1c9deaa153acca76e7d6060a06df Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 25 Mar 2019 15:46:08 +0100 Subject: New command: init. Looks for kicad schematic and pcb files, creates .ee/config. ninja tool: o Use project's config to check for sch and pcb files. o Use some more conditionals in build.ninja.j2. unlockoslo: Adding demo project. --- src/ee/project/__init__.py | 13 ++++++++++ src/ee/tools/init.py | 47 +++++++++++++++++++++++++++++++++-- src/ee/tools/ninja.py | 41 ++++++++++++++++-------------- src/ee/tools/templates/build.ninja.j2 | 6 +++++ 4 files changed, 86 insertions(+), 21 deletions(-) (limited to 'src/ee') diff --git a/src/ee/project/__init__.py b/src/ee/project/__init__.py index 5943a7c..6851256 100644 --- a/src/ee/project/__init__.py +++ b/src/ee/project/__init__.py @@ -1,6 +1,8 @@ import configparser from pathlib import Path +from ee.tools import mk_parents + def load_config(project_dir: Path) -> configparser.ConfigParser: config = configparser.ConfigParser() @@ -18,12 +20,23 @@ def load_config(project_dir: Path) -> configparser.ConfigParser: class Project(object): def __init__(self, project_dir: Path, cfg: configparser.ConfigParser): self.report_dir = project_dir / "ee" / "reports" + self.public_dir = project_dir / "ee" self.project_dir = project_dir self._cfg = cfg + @property + def cfg(self): + return self._cfg + @classmethod def load(cls): project_dir = Path(".") cfg = load_config(project_dir) return Project(project_dir, cfg) + + def save(self): + path = self.project_dir / ".ee" / "config" + mk_parents(path) + with (path).open("w") as f: + self._cfg.write(f) diff --git a/src/ee/tools/init.py b/src/ee/tools/init.py index a3e0472..81bdda3 100644 --- a/src/ee/tools/init.py +++ b/src/ee/tools/init.py @@ -1,10 +1,53 @@ import argparse from pathlib import Path +from typing import List + +from ee.project import Project +import configparser + + +def init_kicad_project(basedir: Path, cfg): + print("basedir={}".format(basedir)) + pro_files: List[Path] = [f for f in basedir.iterdir() if f.name.endswith(".pro")] + + if len(pro_files) == 0: + return + + if len(pro_files) == 1: + pro_file = pro_files[0] + sch_file: Path = pro_file.parent / (pro_file.name[0:-4] + ".sch") + pcb_file: Path = pro_file.parent / (pro_file.name[0:-4] + ".kicad_pcb") + + cfg.add_section("kicad-project") + + if sch_file.is_file(): + print("Found KiCAD project and schematic") + cfg["kicad-project"]["sch"] = str(sch_file) + + if pcb_file.is_file(): + cfg["kicad-project"]["pcb"] = str(pcb_file) + else: + print("Found more than one kicad project file.") + + +def init(project_dir: Path, basedir: Path): + cfg = configparser.ConfigParser() + project = Project(project_dir, cfg) + + init_kicad_project(basedir, cfg) + + print("Saving project. Now run 'ee ninja' to generate Ninja build file") + project.save() -from ee.project import init parser = argparse.ArgumentParser() +parser.add_argument("--basedir", + metavar="DIR") + args = parser.parse_args() -init(Path(".")) +if not args.basedir: + args.basedir = "." + +init(Path("."), Path(args.basedir)) diff --git a/src/ee/tools/ninja.py b/src/ee/tools/ninja.py index f04b174..a5bbe5b 100644 --- a/src/ee/tools/ninja.py +++ b/src/ee/tools/ninja.py @@ -40,7 +40,7 @@ def noext_filter(s: Union[str, Path]) -> str: return os.path.splitext(os.path.basename(str(s)))[0] -def generate(project: Project, sch_path: Path, kicad_bom_strategy: Optional[str]): +def generate(project: Project, kicad_bom_strategy: Optional[str]): def _create_env(): e = Environment( loader=PackageLoader(__name__, "templates"), @@ -53,22 +53,31 @@ def generate(project: Project, sch_path: Path, kicad_bom_strategy: Optional[str] e.filters["noext"] = noext_filter return e - gerber_zip = "prod/gerber.zip" - - sch = read_schematics(str(sch_path)) - - sch_files = sorted([s.path for s in sch.schematics]) - part_dbs = [] params = { "ee": "{} -m ee".format(os.path.relpath(sys.executable, Path("."))), "project": project, - "sch": sch_path, - "sch_files": sch_files, "kicad_bom_strategy": kicad_bom_strategy, - "pcb": str(sch_path).replace(".sch", ".kicad_pcb"), "part_dbs": part_dbs, } + gerber_zip = None + + if project.cfg.has_section("kicad-project"): + kp = project.cfg["kicad-project"] + if "sch" in kp: + sch_path = Path(project.cfg["kicad-project"]["sch"]) + sch = read_schematics(str(sch_path)) + + sch_files = sorted([s.path for s in sch.schematics]) + + params["sch"] = sch_path + params["sch_files"] = sch_files + params["kicad_bom_strategy"] = kicad_bom_strategy + + if "pcb" in kp: + params["pcb"] = Path(project.cfg["kicad-project"]["pcb"]) + gerber_zip = "prod/gerber.zip" + # TODO: read from config distributors = ["digikey"] params["distributors"] = distributors @@ -77,10 +86,9 @@ def generate(project: Project, sch_path: Path, kicad_bom_strategy: Optional[str] params["gerber_zip"] = gerber_zip # ee_dir = sch_path.parent / "ee" - ee_dir = Path(".") - build_ninja = ee_dir / "build.ninja" + build_ninja = project.project_dir / "build.ninja" - parts_yaml_files = [path for path in ee_dir.iterdir() if str(path).endswith("-parts.yaml")] + parts_yaml_files = [path for path in project.project_dir.iterdir() if str(path).endswith("-parts.yaml")] params["parts_yaml_files"] = parts_yaml_files # Local part databases first @@ -95,15 +103,10 @@ def generate(project: Project, sch_path: Path, kicad_bom_strategy: Optional[str] parser = argparse.ArgumentParser() -parser.add_argument("--sch", - required=True, - metavar="FILE") - parser.add_argument("--kicad-bom-strategy", required=False, metavar="PY CALLABLE") args = parser.parse_args() -project = Project.load() -generate(project, Path(args.sch), args.kicad_bom_strategy) +generate(Project.load(), args.kicad_bom_strategy) diff --git a/src/ee/tools/templates/build.ninja.j2 b/src/ee/tools/templates/build.ninja.j2 index 514e177..080ac65 100644 --- a/src/ee/tools/templates/build.ninja.j2 +++ b/src/ee/tools/templates/build.ninja.j2 @@ -1,8 +1,12 @@ ee = {{ ee }} report_dir = {{ project.report_dir }} +{%- if sch is defined %} sch = {{ sch | ninja_path }} sch_files = {{ sch_files | ninja_path }} +{%- endif %} +{%- if pcb is defined %} pcb = {{ pcb | ninja_path }} +{%- endif %} rule kicad-gerber description = kicad-gerber @@ -51,8 +55,10 @@ build {{ gerber_zip }}: kicad-gerber $pcb gerber_dir = {{ gerber_zip | parent_dir }} {%- endif %} +{% if sch is defined -%} build ee/sch.xml: kicad-make-bom $sch strategy ={{ " --strategy " + kicad_bom_strategy if kicad_bom_strategy else "" }} +{% endif -%} {% for d in distributors %} # Distributor {{ d }} build ee/{{ d }}/search-list.xml: part-create-distributor-search-list ee/sch.xml -- cgit v1.2.3