-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_files.py
64 lines (59 loc) · 3.19 KB
/
build_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import subprocess
import os,sys
import contextlib
try:
subprocess.run(['kicad-cli'],capture_output=True)
except FileNotFoundError:
#if kicad-cli not found, run inside of docker
cwd=os.getcwd()
cmd=["docker", "run", "--rm", f"-v{cwd}:{cwd}",f"-w{cwd}","-it","kicad/kicad:8.0.2","python3"]
cmd+=sys.argv
#print(cmd)
proc = subprocess.run(cmd)
sys.exit(proc.returncode)
@contextlib.contextmanager
def pushd(new_dir):
previous_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(previous_dir)
def sh_run(cmd,capture_output=False):
print(f"run:{cmd}")
return subprocess.run(cmd,shell=True,capture_output=capture_output,text=True)
def pcbway_gen(project_name,project_rev):
sh_run(f"sed -i 's/Revision:.*\"/Revision:{project_rev}\"/' {project_name}.kicad_pcb")
sh_run(f"kicad-cli pcb export drill {project_name}.kicad_pcb -o manufacture/")
sh_run(f"kicad-cli pcb export gerbers {project_name}.kicad_pcb --board-plot-params -o manufacture")
with pushd("manufacture"):
sh_run(f"zip {project_name}-gbr.zip *.drl *.gbr *.gbrjob")
#sh_run(f"rm *.drl *.gbr *.pos")
sh_run(f"kicad-cli pcb export pos --side front --use-drill-file-origin {project_name}.kicad_pcb -o manufacture/front.pos")
sh_run(f"kicad-cli pcb export pos --side back --use-drill-file-origin {project_name}.kicad_pcb -o manufacture/back.pos")
sh_run("kicad-cli sch export bom -o manufacture/bom.csv --group-by Value "+
" --fields '${ITEM_NUMBER},Reference,${QUANTITY},Manufacturer,MPN,Value,Footprint,${DNP}'"+
" --labels 'Item #,Designator,Qty,Manufacturer,Mfg Part#,Value,Footprint,dnp'" +
f" {project_name}.kicad_sch")
sh_run(f"kicad-cli sch export pdf -D PROJECT_REV={project_rev} -o manufacture/schematic.pdf {project_name}.kicad_sch")
def jlcpcb(project_name,project_rev):
sh_run(f"sed -i 's/Revision:.*\"/Revision:{project_rev}\"/' {project_name}.kicad_pcb")
sh_run(f"kicad-cli pcb export drill {project_name}.kicad_pcb -o jlcpcb_manufacture/")
sh_run(f"kicad-cli pcb export gerbers {project_name}.kicad_pcb --board-plot-params -o jlcpcb_manufacture")
with pushd("jlcpcb_manufacture"):
sh_run(f"zip {project_name}-gbr.zip *.drl *.gbr *.gbrjob")
#sh_run(f"rm *.drl *.gbr *.pos")
sh_run(f"kicad-cli pcb export pos --format csv --units mm --use-drill-file-origin --side both {project_name}.kicad_pcb -o jlcpcb_manufacture/position.csv")
sh_run("kicad-cli sch export bom -o jlcpcb_manufacture/bom.csv --group-by Value "+
" --fields 'Reference,Footprint,${QUANTITY},Value,Footprint,LCSC Part #'"+
" --labels 'Designator,Footprint,Qty,Value,LCSC Part #'" +
f" {project_name}.kicad_sch")
sh_run(f"kicad-cli sch export pdf -D PROJECT_REV={project_rev} -o jlcpcb_manufacture/schematic.pdf {project_name}.kicad_sch")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("project_name")
args= parser.parse_args()
githash=sh_run("git describe --tags --always --dirty",capture_output=True).stdout.strip()
pcbway_gen(args.project_name,githash)
#jlcpcb(args.project_name,githash)