aboutsummaryrefslogtreecommitdiff
path: root/src/ee/tools/ninja.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-03-04 00:06:32 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-03-04 00:06:32 +0100
commit5a6ab34565fa3d783337a68e07e3500361c8fcf5 (patch)
treec0d4fd9f6986351a543838fabc3dacf8d9ad5fb1 /src/ee/tools/ninja.py
parent650b4ad3774c4cc454a02073808d981f6873036d (diff)
downloadee-python-5a6ab34565fa3d783337a68e07e3500361c8fcf5.tar.gz
ee-python-5a6ab34565fa3d783337a68e07e3500361c8fcf5.tar.bz2
ee-python-5a6ab34565fa3d783337a68e07e3500361c8fcf5.tar.xz
ee-python-5a6ab34565fa3d783337a68e07e3500361c8fcf5.zip
o PartDB: don't die if the directory exist *and* is empty. Ninja creates
empty parent directories. o PartDB: escape some path names. o new comamnd: "ninja". Generates a build.ninja from a sch. Not done!
Diffstat (limited to 'src/ee/tools/ninja.py')
-rw-r--r--src/ee/tools/ninja.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/ee/tools/ninja.py b/src/ee/tools/ninja.py
new file mode 100644
index 0000000..cc090a0
--- /dev/null
+++ b/src/ee/tools/ninja.py
@@ -0,0 +1,80 @@
+import argparse
+import sys
+from pathlib import Path
+from typing import List, Union, Optional
+
+from jinja2 import Environment, PackageLoader, select_autoescape
+
+from ee.kicad import read_schematics
+
+
+def ninja_path_filter(s: Union[str, List[str]]) -> str:
+ if isinstance(s, str):
+ return s. \
+ replace("$", "$$"). \
+ replace(" ", "$ ")
+ elif isinstance(s, list):
+
+ if len(s) == 1:
+ return ninja_path_filter(s[0])
+
+ return "$\n" + " $\n".join([" " + ninja_path_filter(path) for path in s])
+ else:
+ raise Exception("Unsupported argument type: {}".format(type(s)))
+
+
+def parent_dir_filter(s: str) -> str:
+ return str(Path(s).parent)
+
+
+def generate(sch_path: Path, kicad_bom_strategy: Optional[str]):
+ def _create_env():
+ e = Environment(
+ loader=PackageLoader(__name__, "templates"),
+ autoescape=select_autoescape(["html", "xml"]),
+ keep_trailing_newline=True,
+ )
+ e.filters["ninja_path"] = ninja_path_filter
+ e.filters["parent_dir"] = parent_dir_filter
+ return e
+
+ gerber_zip = "prod/gerber.zip"
+
+ sch = read_schematics(str(sch_path))
+
+ sch_files = sorted([s.path for s in sch.schematics])
+
+ params = {}
+ import os.path
+ params["ee"] = "{} -m ee".format(os.path.relpath(sys.executable, Path(".")))
+ params["sch"] = sch_path
+ params["sch_files"] = sch_files
+
+ params["kicad_bom_strategy"] = kicad_bom_strategy
+
+ params["pcb"] = str(sch_path).replace(".sch", ".kicad_pcb")
+
+ if gerber_zip is not None:
+ params["gerber_zip"] = gerber_zip
+
+ build_ninja = Path("build.ninja")
+
+ with build_ninja.open("w") as f:
+ env = _create_env()
+ template = env.get_template("build.ninja.j2")
+ f.write(template.render(**params))
+
+
+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()
+
+generate(args.sch, args.kicad_bom_strategy)