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. --- .gitmodules | 3 ++ demo/thirdparty/unlockoslo-alpha-3/.ee/config | 4 ++ demo/thirdparty/unlockoslo-alpha-3/build.ninja | 70 ++++++++++++++++++++++++++ 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 +++ thirdparty/unlockoslo | 1 + 8 files changed, 164 insertions(+), 21 deletions(-) create mode 100644 demo/thirdparty/unlockoslo-alpha-3/.ee/config create mode 100644 demo/thirdparty/unlockoslo-alpha-3/build.ninja create mode 160000 thirdparty/unlockoslo diff --git a/.gitmodules b/.gitmodules index 98d2da4..177168b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "thirdparty/olinuxino"] path = thirdparty/olinuxino url = https://github.com/OLIMEX/OLINUXINO +[submodule "thirdparty/unlockoslo"] + path = thirdparty/unlockoslo + url = https://github.com/jonnor/unlockoslo diff --git a/demo/thirdparty/unlockoslo-alpha-3/.ee/config b/demo/thirdparty/unlockoslo-alpha-3/.ee/config new file mode 100644 index 0000000..c7e2990 --- /dev/null +++ b/demo/thirdparty/unlockoslo-alpha-3/.ee/config @@ -0,0 +1,4 @@ +[kicad-project] +sch = ../../../thirdparty/unlockoslo/hardware/alpha-3/alpha.sch +pcb = ../../../thirdparty/unlockoslo/hardware/alpha-3/alpha.kicad_pcb + diff --git a/demo/thirdparty/unlockoslo-alpha-3/build.ninja b/demo/thirdparty/unlockoslo-alpha-3/build.ninja new file mode 100644 index 0000000..3bf21f6 --- /dev/null +++ b/demo/thirdparty/unlockoslo-alpha-3/build.ninja @@ -0,0 +1,70 @@ +ee = ../../../env/bin/python3 -m ee +report_dir = ee/reports +sch = ../../../thirdparty/unlockoslo/hardware/alpha-3/alpha.sch +sch_files = ../../../thirdparty/unlockoslo/hardware/alpha-3/alpha.sch +pcb = ../../../thirdparty/unlockoslo/hardware/alpha-3/alpha.kicad_pcb + +rule kicad-gerber + description = kicad-gerber + command = $ee kicad-gerber $ + --output-dir $gerber_dir $ + --pcb $pcb +# mkdir -p $( +# (cd $(GERBER_DIR); zip tmp.zip $(foreach GBR,$(GERBERS),$(notdir $(GBR)))) +# mv $(GERBER_DIR)/tmp.zip $@ + +rule kicad-make-bom + description = kicad-make-bom $out + command = $ee kicad-make-bom --sch $sch --out $out $strategy + +rule part-create-distributor-search-list + description = part-create-distributor-search-list distributor: $distributor + command = $ee part-create-distributor-search-list --in $in --out $out + +rule digikey-search-parts + description = digikey-search-parts + command = $ee digikey-search-parts --in $in --out $out + +rule digikey-normalize-facts + description = digikey-normalize-facts + command = $ee digikey-normalize-facts --in $in --out $out + +rule element14-search-parts + description = element14-search-parts + command = $ee element14-search-parts --in $in --out $out + +rule element14-normalize-facts + description = element14-normalize-facts + command = $ee element14-normalize-facts --in $in --out $out + +rule create-order + description = create-order + command = $ee create-order --schematic $schematic --part-db $part_dbs --out $out + +rule import-parts-yaml + description = import-parts-yaml $in + command = $ee import-parts-yaml --in $in --out $out + + +build gerbers: phony prod/gerber.zip +build prod/gerber.zip: kicad-gerber $pcb + gerber_dir = prod + +build ee/sch.xml: kicad-make-bom $sch + strategy = + +# Distributor digikey +build ee/digikey/search-list.xml: part-create-distributor-search-list ee/sch.xml + distributor = digikey + +build ee/digikey/downloaded.xml: digikey-search-parts ee/digikey/search-list.xml + +build ee/digikey/normalized.xml: digikey-normalize-facts ee/digikey/downloaded.xml + +default ee/digikey/normalized.xml + +build ee/order.xml | $report_dir/order.rst: create-order ee/sch.xml ee/digikey/normalized.xml + schematic = ee/sch.xml + part_dbs = ee/digikey/normalized.xml + +default ee/order.xml 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 diff --git a/thirdparty/unlockoslo b/thirdparty/unlockoslo new file mode 160000 index 0000000..b6567a9 --- /dev/null +++ b/thirdparty/unlockoslo @@ -0,0 +1 @@ +Subproject commit b6567a9b17de8bc9ed73b2e0938a0f4c8878c110 -- cgit v1.2.3