pymoku-sdk 0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pymoku/sdk/__init__.py +61 -0
- pymoku/sdk/barfile.py +81 -0
- pymoku/sdk/checkpoints.py +145 -0
- pymoku/sdk/cocotb.py +39 -0
- pymoku/sdk/keys.py +89 -0
- pymoku/sdk/main.py +285 -0
- pymoku/sdk/manifests.py +128 -0
- pymoku/sdk/support/constraints/slot_default.xdc +2 -0
- pymoku/sdk/support/parts.def +10 -0
- pymoku/sdk/support/python/api_main.py +61 -0
- pymoku/sdk/support/sim/amba.py +732 -0
- pymoku/sdk/support/sim/blackbox_models.py +223 -0
- pymoku/sdk/support/sim/cocotb_test.py +58 -0
- pymoku/sdk/support/sim/spiregs.py +70 -0
- pymoku/sdk/support/sim/startup.py +110 -0
- pymoku/sdk/support/tcl/bitstream.tcl +11 -0
- pymoku/sdk/support/tcl/reports.tcl +7 -0
- pymoku/sdk/support/tcl/route.tcl +17 -0
- pymoku/sdk/support/tcl/shell-zynq.tcl +9 -0
- pymoku/sdk/support/tcl/shell-zynqmp.tcl +6 -0
- pymoku/sdk/support/tcl/synth.tcl +48 -0
- pymoku/sdk/support/vhdl/MemoryMap.vhd +222 -0
- pymoku/sdk/support/vhdl/MokuSupport.vhd +1102 -0
- pymoku/sdk/templates/TestTop.vhd +64 -0
- pymoku/sdk/templates/Top.vhd +75 -0
- pymoku/sdk/templates/api.py +11 -0
- pymoku/sdk/templates/applet.py +7 -0
- pymoku/sdk/templates/gitignore +6 -0
- pymoku/sdk/templates/module.py +7 -0
- pymoku/sdk/templates/parts.def +70 -0
- pymoku/sdk/templates/parts_platform.def +19 -0
- pymoku/sdk/templates/plugin.py +27 -0
- pymoku/sdk/templates/test_module.py +30 -0
- pymoku/sdk/vivado.py +667 -0
- pymoku_sdk-0.3.dist-info/METADATA +138 -0
- pymoku_sdk-0.3.dist-info/RECORD +40 -0
- pymoku_sdk-0.3.dist-info/WHEEL +5 -0
- pymoku_sdk-0.3.dist-info/entry_points.txt +2 -0
- pymoku_sdk-0.3.dist-info/licenses/LICENSE +22 -0
- pymoku_sdk-0.3.dist-info/top_level.txt +1 -0
pymoku/sdk/__init__.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
try:
|
|
2
|
+
import setuptools_scm
|
|
3
|
+
SCM_VERSION = setuptools_scm.get_version()
|
|
4
|
+
except ImportError:
|
|
5
|
+
SCM_VERSION = ''
|
|
6
|
+
|
|
7
|
+
FIRMWARE_VERSION = 616
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
from .vivado import * # noqa
|
|
11
|
+
from .manifests import * # noqa
|
|
12
|
+
from .barfile import * # noqa
|
|
13
|
+
from .cocotb import * # noqa
|
|
14
|
+
from .checkpoints import * # noqa
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def generate_slot_bitstreams(platform, synth,
|
|
18
|
+
constraints='support-slot-constraints',
|
|
19
|
+
route_tcl='support-slot-route-tcl',
|
|
20
|
+
bitstream_tcl='support-slot-bitstream-tcl',
|
|
21
|
+
reports_tcl='support-slot-reports-tcl'):
|
|
22
|
+
"""
|
|
23
|
+
There is a distinction between arbitrary RMs and Slot RMs, in that Slot RMs are
|
|
24
|
+
expected to be implemented within the group of Slots defined as "Instruments",
|
|
25
|
+
whereas any RM can be implemented within any Slot as long as it meets the
|
|
26
|
+
interface.
|
|
27
|
+
|
|
28
|
+
This API simply provides a shortcut to the common case of "Instruments".
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
routes = [RMRoute(synth, shell, route_tcl=route_tcl, constraints=constraints) for shell in platform.slot_shells()]
|
|
32
|
+
rms = [RMInstance(route, bitstream_tcl=bitstream_tcl, reports_tcl=reports_tcl) for route in routes]
|
|
33
|
+
return rms
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def generate_slot_manifests(platform, synth, manifest_id,
|
|
37
|
+
constraints='support-slot-constraints',
|
|
38
|
+
route_tcl='support-slot-route-tcl',
|
|
39
|
+
bitstream_tcl='support-slot-bitstream-tcl',
|
|
40
|
+
reports_tcl='support-slot-reports-tcl'):
|
|
41
|
+
rms = generate_slot_bitstreams(platform, synth,
|
|
42
|
+
constraints=constraints,
|
|
43
|
+
route_tcl=route_tcl,
|
|
44
|
+
bitstream_tcl=bitstream_tcl,
|
|
45
|
+
reports_tcl=reports_tcl)
|
|
46
|
+
manifests = [Manifest(manifest_id, rm) for rm in rms]
|
|
47
|
+
return manifests
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def include_support():
|
|
51
|
+
import pymoku.sdk # noqa
|
|
52
|
+
import shutil # noqa
|
|
53
|
+
from pymoku.parts import include, ConfigGlobals # noqa
|
|
54
|
+
from importlib import resources # noqa
|
|
55
|
+
|
|
56
|
+
support_path = ConfigGlobals.build_path / 'support'
|
|
57
|
+
if not support_path.exists():
|
|
58
|
+
with resources.path(pymoku.sdk, 'support') as support_res:
|
|
59
|
+
print(support_res, support_path)
|
|
60
|
+
shutil.copytree(support_res, support_path)
|
|
61
|
+
include(support_path, prefix='support')
|
pymoku/sdk/barfile.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
import tarfile
|
|
3
|
+
import json
|
|
4
|
+
import io
|
|
5
|
+
import hashlib
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from pymoku.parts import BuildStep, SourceFile
|
|
8
|
+
from pymoku.sdk import SCM_VERSION, log, keys
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
KEY_DIR = Path.home() / '.config/pymoku/keys'
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BarFile(BuildStep):
|
|
15
|
+
VERSION = 0
|
|
16
|
+
|
|
17
|
+
def __init__(self, barfile, *manifests):
|
|
18
|
+
super().__init__(name=barfile)
|
|
19
|
+
|
|
20
|
+
# list of manifests or list of lists of manifests
|
|
21
|
+
try:
|
|
22
|
+
self.manifests = list(itertools.chain.from_iterable(manifests))
|
|
23
|
+
except TypeError:
|
|
24
|
+
self.manifests = manifests
|
|
25
|
+
|
|
26
|
+
self.barfile = self.build_path / barfile
|
|
27
|
+
for m in self.manifests:
|
|
28
|
+
self.depends_on(m.bitstreams())
|
|
29
|
+
self.creates(SourceFile(self.barfile))
|
|
30
|
+
self._bs_arcnames = {}
|
|
31
|
+
|
|
32
|
+
def arcname_for(self, bitstream):
|
|
33
|
+
# generate a unique filename for bitstreams in the archive
|
|
34
|
+
if bitstream not in self._bs_arcnames:
|
|
35
|
+
self._bs_arcnames[bitstream] = f'{len(self._bs_arcnames):03d}'
|
|
36
|
+
return self._bs_arcnames[bitstream]
|
|
37
|
+
|
|
38
|
+
def config_state(self):
|
|
39
|
+
# Manifests aren't build steps, and we don't know when they change
|
|
40
|
+
# until their own dependencies are built.
|
|
41
|
+
sha = hashlib.sha256(SCM_VERSION.encode())
|
|
42
|
+
sha.update(json.dumps(dict(keys.list_public_keys())).encode())
|
|
43
|
+
return sha.digest()
|
|
44
|
+
|
|
45
|
+
async def do_step(self):
|
|
46
|
+
manifests = []
|
|
47
|
+
for m in self.manifests:
|
|
48
|
+
manifests.extend(m(self.arcname_for))
|
|
49
|
+
|
|
50
|
+
self.update_progress(total=2 + len(self._bs_arcnames))
|
|
51
|
+
|
|
52
|
+
manifest_bytes = self.create_manifest([m for m, bs in manifests])
|
|
53
|
+
self.update_progress(advance=1)
|
|
54
|
+
|
|
55
|
+
with tarfile.open(self.barfile, mode='w') as tf:
|
|
56
|
+
m_tinfo = tarfile.TarInfo(name='MANIFEST')
|
|
57
|
+
m_tinfo.size = len(manifest_bytes)
|
|
58
|
+
tf.addfile(m_tinfo, io.BytesIO(manifest_bytes))
|
|
59
|
+
|
|
60
|
+
# sign with all the keys TODO maybe specify which ones?
|
|
61
|
+
for keyname, private_key in keys.list_private_keys():
|
|
62
|
+
sig_bytes = private_key.sign(manifest_bytes)
|
|
63
|
+
sig_tinfo = tarfile.TarInfo(name=f'MANIFEST.{keyname}.sig')
|
|
64
|
+
sig_tinfo.size = len(sig_bytes)
|
|
65
|
+
tf.addfile(sig_tinfo, io.BytesIO(sig_bytes))
|
|
66
|
+
|
|
67
|
+
for bs, fname in self._bs_arcnames.items():
|
|
68
|
+
bs_info = tf.gettarinfo(bs.path, arcname=fname)
|
|
69
|
+
with open(bs.path, 'rb') as f:
|
|
70
|
+
tf.addfile(bs_info, fileobj=f)
|
|
71
|
+
self.update_progress(advance=1)
|
|
72
|
+
|
|
73
|
+
# 32 MB is the upload limit through the web interface, but also probably
|
|
74
|
+
# a reasonable limit in general
|
|
75
|
+
size = self.barfile.stat().st_size
|
|
76
|
+
if size > 1024 * 2**15:
|
|
77
|
+
log.warning(f'Barfile {self.barfile} exceeds 32MB ({size:d})')
|
|
78
|
+
|
|
79
|
+
def create_manifest(self, items):
|
|
80
|
+
manifest = dict(version=self.VERSION, items=items)
|
|
81
|
+
return json.dumps(manifest, indent=2).encode()
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import re
|
|
3
|
+
import hashlib
|
|
4
|
+
import tarfile
|
|
5
|
+
import json
|
|
6
|
+
import logging
|
|
7
|
+
import asyncio
|
|
8
|
+
import concurrent.futures
|
|
9
|
+
from functools import cache
|
|
10
|
+
from pymoku.sdk import BuildStep
|
|
11
|
+
from pymoku.plugins import PluginCache
|
|
12
|
+
from pymoku.parts import SourceFile
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
_cache = PluginCache()
|
|
16
|
+
log = logging.getLogger("pymokusdk")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@cache
|
|
20
|
+
def get_platform(platform_name):
|
|
21
|
+
""" Find a Platform checkpoint
|
|
22
|
+
"""
|
|
23
|
+
manifests = _cache.checkpoint_manifests(_id=f'pymoku:{platform_name}')
|
|
24
|
+
|
|
25
|
+
if not manifests:
|
|
26
|
+
_cache.store_manifests(_cache.cloud_manifests(_type='checkpoint'))
|
|
27
|
+
manifests = _cache.checkpoint_manifests(_id=f'pymoku:{platform_name}')
|
|
28
|
+
|
|
29
|
+
if not manifests:
|
|
30
|
+
log.error(f"No checkpoints found for {platform_name}")
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
log.debug(f"Using platform {platform_name} from {manifests[0]['location']}")
|
|
34
|
+
|
|
35
|
+
return CheckpointPlatform(platform_name, manifests)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class CheckpointPlatform(BuildStep):
|
|
39
|
+
""" This mimics the vivado.Platform interface but loads the checkpoint from
|
|
40
|
+
a barfile rather than trying to build it.
|
|
41
|
+
|
|
42
|
+
This step downloads the barfile to the local pymoku cache; the
|
|
43
|
+
CheckpointShell unpacks each DCP into the build directory.
|
|
44
|
+
"""
|
|
45
|
+
def __init__(self, name, manifests):
|
|
46
|
+
super().__init__(name=name)
|
|
47
|
+
# there is no "platform" checkpoint, only the individual cells
|
|
48
|
+
self.manifests = manifests
|
|
49
|
+
|
|
50
|
+
self.hwver = self.slot_shells()[0].hwver
|
|
51
|
+
self.part = self.slot_shells()[0].part
|
|
52
|
+
self.arch = self.slot_shells()[0].arch
|
|
53
|
+
# TODO these should not be platform properties
|
|
54
|
+
self.slot_ains = self.slot_shells()[0].num_ain
|
|
55
|
+
self.slot_aouts = self.slot_shells()[0].num_aout
|
|
56
|
+
self.slot_cbufs = self.slot_shells()[0].num_cbufs
|
|
57
|
+
self.sha = manifests[0]['plat_sha']
|
|
58
|
+
|
|
59
|
+
@cache
|
|
60
|
+
def slot_shells(self):
|
|
61
|
+
shells = [CheckpointShell(self, m) for m in self.manifests]
|
|
62
|
+
return sorted([s for s in shells if s.slot is not None], key=lambda s: s.slot)
|
|
63
|
+
|
|
64
|
+
async def do_step(self):
|
|
65
|
+
# manifest may be in one shared barfile, reduce to unique barfiles
|
|
66
|
+
barfiles = []
|
|
67
|
+
to_fetch = []
|
|
68
|
+
for m in self.manifests:
|
|
69
|
+
if m['location'] != 'local' and m['barfile'] not in barfiles:
|
|
70
|
+
to_fetch.append(m)
|
|
71
|
+
barfiles.append(m['barfile'])
|
|
72
|
+
|
|
73
|
+
# split downloads across mutliple threads (and unblock other BuildSteps)
|
|
74
|
+
tasks = []
|
|
75
|
+
loop = asyncio.get_running_loop()
|
|
76
|
+
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
|
|
77
|
+
for m in to_fetch:
|
|
78
|
+
tasks.append(loop.run_in_executor(pool, _cache.download_barfile, m))
|
|
79
|
+
await asyncio.gather(*tasks)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class CheckpointShell(BuildStep):
|
|
83
|
+
def __init__(self, platform, manifest):
|
|
84
|
+
self.platform = platform
|
|
85
|
+
self.manifest = manifest
|
|
86
|
+
self.hwver = manifest['extra']['hwver']
|
|
87
|
+
self.cell_name = manifest['extra']['cell_name']
|
|
88
|
+
self.pblock_name = manifest['extra']['pblock_name']
|
|
89
|
+
self.part = manifest['extra']['part']
|
|
90
|
+
self.arch = manifest['extra']['arch']
|
|
91
|
+
self.num_ain = manifest['extra']['num_ain']
|
|
92
|
+
self.num_aout = manifest['extra']['num_aout']
|
|
93
|
+
self.num_cbufs = manifest['extra']['num_cbufs']
|
|
94
|
+
|
|
95
|
+
name = f'{platform.name}-{self.safe_cell_name}'
|
|
96
|
+
super().__init__(name=name)
|
|
97
|
+
self.depends_on(platform)
|
|
98
|
+
|
|
99
|
+
self.workdir = self.build_path / 'checkpoints' / platform.name
|
|
100
|
+
self.shell_dcp = SourceFile(self.workdir / f'{name}.dcp')
|
|
101
|
+
self.creates(self.shell_dcp)
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def plat_name(self):
|
|
105
|
+
return self.platform.name
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def path(self):
|
|
109
|
+
return next(iter(self.outputs())).path
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def safe_cell_name(self):
|
|
113
|
+
name = self.cell_name.translate(
|
|
114
|
+
str.maketrans({"[": "_", "]": None, "*": "_"}))
|
|
115
|
+
name = name.replace('_INSTR', '')
|
|
116
|
+
return name
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def slot(self):
|
|
120
|
+
""" This makes some big assumptions about cell naming conventions"""
|
|
121
|
+
if (m := re.search(r'INSTRS\[(\d+)\]', self.cell_name)) is None:
|
|
122
|
+
return None
|
|
123
|
+
return int(m.group(1))
|
|
124
|
+
|
|
125
|
+
def config_state(self):
|
|
126
|
+
sha = hashlib.sha256(json.dumps(self.manifest['extra']).encode())
|
|
127
|
+
sha.update(self.manifest['sha256'].encode())
|
|
128
|
+
sha.update(self.manifest['plat_sha'].encode())
|
|
129
|
+
return sha.digest()
|
|
130
|
+
|
|
131
|
+
async def do_step(self):
|
|
132
|
+
barfile_data = _cache.download_barfile(self.manifest)
|
|
133
|
+
barfile_data = io.BytesIO(barfile_data)
|
|
134
|
+
|
|
135
|
+
with tarfile.open(fileobj=barfile_data) as arc:
|
|
136
|
+
member = arc.getmember(self.manifest['filename'])
|
|
137
|
+
assert member.isfile()
|
|
138
|
+
data = arc.extractfile(member).read()
|
|
139
|
+
|
|
140
|
+
sha = hashlib.sha256(data)
|
|
141
|
+
|
|
142
|
+
assert sha.hexdigest() == self.manifest['sha256']
|
|
143
|
+
|
|
144
|
+
self.shell_dcp.path.parent.mkdir(exist_ok=True, parents=True)
|
|
145
|
+
self.shell_dcp.path.write_bytes(data)
|
pymoku/sdk/cocotb.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from pymoku.parts import DockerStep, link
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class CocoTB(DockerStep):
|
|
5
|
+
def __init__(self, name, top, sources, python, generics={}, *args, **kwargs):
|
|
6
|
+
generics_str = ' '.join([f'{k}={str(v).lower()}' for k, v in generics.items()])
|
|
7
|
+
|
|
8
|
+
extra_env = dict(
|
|
9
|
+
TEST_TOP=top.lower(),
|
|
10
|
+
TEST_MODULE=python,
|
|
11
|
+
TEST_GENERICS=generics_str,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
super().__init__('li-cocotb-pymoku',
|
|
15
|
+
name=name,
|
|
16
|
+
extra_env=extra_env,
|
|
17
|
+
**kwargs)
|
|
18
|
+
self.depends_on(link('support-sim-python', self.workdir))
|
|
19
|
+
self.depends_on(link('support-slot-vhdl', self.workdir / 'support-vhdl'))
|
|
20
|
+
self.depends_on(link(sources, self.workdir))
|
|
21
|
+
self.creates('wave.ghw')
|
|
22
|
+
|
|
23
|
+
def command(self):
|
|
24
|
+
return 'pytest -vs cocotb_test.py'
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CocoTBLib(DockerStep):
|
|
28
|
+
def __init__(self, name, sources):
|
|
29
|
+
super().__init__('li-cocotb-pymoku', name=name)
|
|
30
|
+
self.depends_on(link(sources, self.workdir))
|
|
31
|
+
self.creates(f'{self.name}-obj08.cf')
|
|
32
|
+
self.creates(s.path.name for s in sources)
|
|
33
|
+
self.sources = sources
|
|
34
|
+
|
|
35
|
+
def command(self):
|
|
36
|
+
cmd = f"ghdl -a --work={self.name} "
|
|
37
|
+
cmd += "--std=08 --ieee=synopsys -frelaxed-rules -P/unisim "
|
|
38
|
+
cmd += " ".join(s.path.name for s in self.sources)
|
|
39
|
+
return cmd
|
pymoku/sdk/keys.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import click
|
|
2
|
+
import logging
|
|
3
|
+
import json
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from base64 import b64encode
|
|
6
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.table import Table
|
|
9
|
+
from rich.pretty import pprint
|
|
10
|
+
from .main import cli
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
log = logging.getLogger("pymokusdk")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
KEY_DIR = Path.home() / '.config/pymoku/keys'
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def generate_private_key(name, overwrite=False):
|
|
20
|
+
private_key = Ed25519PrivateKey.generate()
|
|
21
|
+
KEY_DIR.mkdir(parents=True, exist_ok=True)
|
|
22
|
+
keyfile = KEY_DIR / f'{name}.key'
|
|
23
|
+
|
|
24
|
+
if keyfile.exists() and not overwrite:
|
|
25
|
+
raise Exception(f"private key '{name}' already exists")
|
|
26
|
+
|
|
27
|
+
keyfile.write_bytes(private_key.private_bytes_raw())
|
|
28
|
+
log.info(f'wrote private key to {keyfile}')
|
|
29
|
+
|
|
30
|
+
public_key = private_key.public_key()
|
|
31
|
+
public_key_64 = b64encode(public_key.public_bytes_raw()).decode()
|
|
32
|
+
return public_key_64
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def load_private_key(keyname):
|
|
36
|
+
key_bytes = (KEY_DIR / keyname).with_suffix('.key').read_bytes()
|
|
37
|
+
return Ed25519PrivateKey.from_private_bytes(key_bytes)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def list_private_keys():
|
|
41
|
+
for keyfile in KEY_DIR.glob('*.key'):
|
|
42
|
+
private_key = load_private_key(keyfile.stem)
|
|
43
|
+
yield (keyfile.stem, private_key)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def list_public_keys():
|
|
47
|
+
for keyfile in KEY_DIR.glob('*.key'):
|
|
48
|
+
private_key = load_private_key(keyfile.stem)
|
|
49
|
+
public_key = private_key.public_key()
|
|
50
|
+
public_key_64 = b64encode(public_key.public_bytes_raw()).decode()
|
|
51
|
+
yield (keyfile.stem, public_key_64)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@cli.group()
|
|
55
|
+
def keys():
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@keys.command('create')
|
|
60
|
+
@click.argument('name')
|
|
61
|
+
@click.option('-f', '--force', is_flag=True)
|
|
62
|
+
def create_key(name, force):
|
|
63
|
+
public_key = generate_private_key(name, overwrite=force)
|
|
64
|
+
pprint(f'Public Key: {name} {public_key}')
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@keys.command('delete')
|
|
68
|
+
@click.argument('name')
|
|
69
|
+
def delete_key(name):
|
|
70
|
+
keyfile = KEY_DIR / f'{name}.key'
|
|
71
|
+
keyfile.unlink(missing_ok=True)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@keys.command('list')
|
|
75
|
+
@click.option('--json', '_json', is_flag=True, help='output keys as JSON')
|
|
76
|
+
def list_keys(_json=False):
|
|
77
|
+
if _json:
|
|
78
|
+
print(json.dumps(dict(list_public_keys())))
|
|
79
|
+
else:
|
|
80
|
+
table = Table()
|
|
81
|
+
|
|
82
|
+
table.add_column("Key Name", justify="right", no_wrap=True)
|
|
83
|
+
table.add_column("Public Key (base64)", no_wrap=True)
|
|
84
|
+
|
|
85
|
+
for name, pubkey in list_public_keys():
|
|
86
|
+
table.add_row(name, pubkey)
|
|
87
|
+
|
|
88
|
+
console = Console()
|
|
89
|
+
console.print(table)
|
pymoku/sdk/main.py
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import click
|
|
2
|
+
import logging
|
|
3
|
+
import tarfile
|
|
4
|
+
import json
|
|
5
|
+
import hashlib
|
|
6
|
+
import subprocess
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from rich.pretty import pprint
|
|
9
|
+
from rich.logging import RichHandler
|
|
10
|
+
from jinja2 import (Environment, PackageLoader, StrictUndefined,
|
|
11
|
+
select_autoescape)
|
|
12
|
+
from cryptography.exceptions import InvalidSignature
|
|
13
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
|
|
14
|
+
from base64 import b64decode
|
|
15
|
+
from pymoku.parts import include
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
FORMAT = "%(message)s"
|
|
19
|
+
logging.basicConfig(
|
|
20
|
+
level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
log = logging.getLogger()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
PLATFORMS = { # TODO these could be parsed from part.defs
|
|
27
|
+
'mokugo': [1, 2],
|
|
28
|
+
'mokulab': [1, 2],
|
|
29
|
+
'mokupro': [1, 4, 8],
|
|
30
|
+
'mokudelta': [1, 4, 8],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
templates = Path(__file__).parent / 'templates'
|
|
34
|
+
jenv = Environment(
|
|
35
|
+
loader=PackageLoader('pymoku.sdk'),
|
|
36
|
+
autoescape=select_autoescape(),
|
|
37
|
+
undefined=StrictUndefined,
|
|
38
|
+
keep_trailing_newline=True,
|
|
39
|
+
trim_blocks=True,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def create_parts_def(path, name, namespace):
|
|
44
|
+
path = path / 'parts.def'
|
|
45
|
+
log.info(f"Create '{path}'")
|
|
46
|
+
|
|
47
|
+
if path.exists():
|
|
48
|
+
log.warning(f"'{path}' already exists, skipping")
|
|
49
|
+
return
|
|
50
|
+
|
|
51
|
+
tmpl = jenv.get_template('parts.def')
|
|
52
|
+
text = tmpl.render(name=name.lower(),
|
|
53
|
+
namespace=namespace.lower(),
|
|
54
|
+
top_entity=f'{name}Slot')
|
|
55
|
+
path.write_text(text)
|
|
56
|
+
|
|
57
|
+
if (parts_path := Path('parts.def')).exists():
|
|
58
|
+
include_text = f"include('{name.lower()}', prefix='{name.lower()}')"
|
|
59
|
+
log.info(f'adding {include_text} to {parts_path}')
|
|
60
|
+
with parts_path.open('a') as f:
|
|
61
|
+
f.write(f"{include_text}\n")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def create_vhd_sources(path, name):
|
|
65
|
+
top_entity = f'{name}Slot'
|
|
66
|
+
path = path / f'{top_entity}.vhd'
|
|
67
|
+
log.info(f"Create '{path}'")
|
|
68
|
+
|
|
69
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
70
|
+
|
|
71
|
+
if path.exists():
|
|
72
|
+
log.warning(f"'{path}' already exists, skipping")
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
tmpl = jenv.get_template('Top.vhd')
|
|
76
|
+
text = tmpl.render(top_entity=top_entity)
|
|
77
|
+
path.write_text(text)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def append_platform(path, name, namespace, platform):
|
|
81
|
+
path = path / 'parts.def'
|
|
82
|
+
log.info(f"Adding {platform} to '{path}'")
|
|
83
|
+
|
|
84
|
+
if not path.exists():
|
|
85
|
+
log.error(f"'{path}' doesn't exist")
|
|
86
|
+
return
|
|
87
|
+
|
|
88
|
+
tmpl = jenv.get_template('parts_platform.def')
|
|
89
|
+
text = tmpl.render(top_entity=f'{name}Slot',
|
|
90
|
+
name=name.lower(),
|
|
91
|
+
namespace=namespace.lower(),
|
|
92
|
+
hw=platform,
|
|
93
|
+
platforms=PLATFORMS[platform])
|
|
94
|
+
|
|
95
|
+
with path.open('a') as f:
|
|
96
|
+
f.write(text)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def create_slot_test(path, name):
|
|
100
|
+
top_entity = f'{name}Slot'
|
|
101
|
+
top_path = path / f'Test{top_entity}.vhd'
|
|
102
|
+
log.info(f"Create '{top_path}'")
|
|
103
|
+
|
|
104
|
+
top_path.parent.mkdir(parents=True, exist_ok=True)
|
|
105
|
+
|
|
106
|
+
if not top_path.exists():
|
|
107
|
+
tmpl = jenv.get_template('TestTop.vhd')
|
|
108
|
+
text = tmpl.render(top_entity=top_entity, num_ain=4, num_aout=4)
|
|
109
|
+
top_path.write_text(text)
|
|
110
|
+
else:
|
|
111
|
+
log.warning(f"'{top_path}' already exists, skipping")
|
|
112
|
+
|
|
113
|
+
mod_path = path / f'test_{name.lower()}.py'
|
|
114
|
+
log.info(f"Create '{mod_path}'")
|
|
115
|
+
|
|
116
|
+
if not mod_path.exists():
|
|
117
|
+
tmpl = jenv.get_template('test_module.py')
|
|
118
|
+
text = tmpl.render(name=name.lower(), top_entity=top_entity, num_ain=4, num_aout=4)
|
|
119
|
+
mod_path.write_text(text)
|
|
120
|
+
else:
|
|
121
|
+
log.warning(f"'{mod_path}' already exists, skipping")
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def create_python_module(path, name):
|
|
125
|
+
top_entity = f'{name}Slot'
|
|
126
|
+
mod_path = path / f'{name.lower()}.py'
|
|
127
|
+
log.info(f"Create '{mod_path}'")
|
|
128
|
+
|
|
129
|
+
if not mod_path.exists():
|
|
130
|
+
mod_path.parent.mkdir(parents=True, exist_ok=True)
|
|
131
|
+
tmpl = jenv.get_template('module.py')
|
|
132
|
+
text = tmpl.render(name=name, top_entity=top_entity)
|
|
133
|
+
mod_path.write_text(text)
|
|
134
|
+
else:
|
|
135
|
+
log.warning(f"'{mod_path}' already exists, skipping")
|
|
136
|
+
|
|
137
|
+
init_path = path / '__init__.py'
|
|
138
|
+
log.info(f"Create '{init_path}'")
|
|
139
|
+
|
|
140
|
+
if not init_path.exists():
|
|
141
|
+
tmpl = jenv.get_template('plugin.py')
|
|
142
|
+
text = tmpl.render(name=name, top_entity=top_entity)
|
|
143
|
+
init_path.write_text(text)
|
|
144
|
+
else:
|
|
145
|
+
log.warning(f"'{init_path}' already exists, skipping")
|
|
146
|
+
|
|
147
|
+
applet_path = path / 'applet.py'
|
|
148
|
+
log.info(f"Create '{applet_path}'")
|
|
149
|
+
|
|
150
|
+
if not applet_path.exists():
|
|
151
|
+
tmpl = jenv.get_template('applet.py')
|
|
152
|
+
text = tmpl.render(name=name)
|
|
153
|
+
applet_path.write_text(text)
|
|
154
|
+
else:
|
|
155
|
+
log.warning(f"'{applet_path}' already exists, skipping")
|
|
156
|
+
|
|
157
|
+
api_path = path / 'api.py'
|
|
158
|
+
log.info(f"Create '{api_path}'")
|
|
159
|
+
|
|
160
|
+
if not api_path.exists():
|
|
161
|
+
tmpl = jenv.get_template('api.py')
|
|
162
|
+
text = tmpl.render(name=name)
|
|
163
|
+
api_path.write_text(text)
|
|
164
|
+
else:
|
|
165
|
+
log.warning(f"'{api_path}' already exists, skipping")
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def init_git(path, name):
|
|
169
|
+
gitignore_path = path / '.gitignore'
|
|
170
|
+
log.info("Initializing Git repository")
|
|
171
|
+
|
|
172
|
+
if not gitignore_path.exists():
|
|
173
|
+
tmpl = jenv.get_template('gitignore')
|
|
174
|
+
text = tmpl.render()
|
|
175
|
+
gitignore_path.write_text(text)
|
|
176
|
+
else:
|
|
177
|
+
log.warning(f"'{gitignore_path}' already exists, skipping")
|
|
178
|
+
|
|
179
|
+
subprocess.run(['git', 'init', '.'], cwd=path, capture_output=True)
|
|
180
|
+
subprocess.run(['git', 'add', '-A'], cwd=path, capture_output=True)
|
|
181
|
+
subprocess.run(['git', 'commit', '-m', f"Initial creation of {name}"], cwd=path, capture_output=True)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@click.group()
|
|
185
|
+
@click.version_option(package_name='pymoku.sdk')
|
|
186
|
+
def cli():
|
|
187
|
+
pass
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@cli.command()
|
|
191
|
+
@click.argument('name')
|
|
192
|
+
@click.option('--namespace', default='custom')
|
|
193
|
+
@click.option('--hw', help=f"Target hardware platform {list(PLATFORMS.keys())}")
|
|
194
|
+
@click.option('--git', is_flag=True, help="Initialize Git repository")
|
|
195
|
+
def new(name, namespace='custom', hw=None, git=False):
|
|
196
|
+
path = Path('.') / name.lower()
|
|
197
|
+
log.info(f"Creating new project '{path}'")
|
|
198
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
199
|
+
|
|
200
|
+
create_parts_def(path, name, namespace)
|
|
201
|
+
create_vhd_sources(path / 'vhdl', name)
|
|
202
|
+
create_python_module(path / 'python', name)
|
|
203
|
+
create_slot_test(path / 'tests', name)
|
|
204
|
+
|
|
205
|
+
if hw:
|
|
206
|
+
append_platform(path, name, namespace, hw)
|
|
207
|
+
|
|
208
|
+
if git:
|
|
209
|
+
init_git(path, name)
|
|
210
|
+
|
|
211
|
+
log.info('Finished')
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@cli.command()
|
|
215
|
+
@click.argument('name')
|
|
216
|
+
@click.argument('hw')
|
|
217
|
+
def add_platform(name, hw):
|
|
218
|
+
path = Path('.') / name.lower()
|
|
219
|
+
append_platform(path, name, hw)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
@cli.command()
|
|
223
|
+
@click.argument('name')
|
|
224
|
+
def build(name):
|
|
225
|
+
build_targets = include(Path(name) / 'parts.def')
|
|
226
|
+
print(build_targets)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
from pymoku.sdk import keys # noqa
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
@cli.command()
|
|
233
|
+
@click.argument('barfile', type=click.Path(exists=True, dir_okay=False))
|
|
234
|
+
@click.option('--raw', is_flag=True, help="Don't decode or pretty-print")
|
|
235
|
+
def manifest(barfile, raw=False):
|
|
236
|
+
""" Print the manifest from the specified barfile. """
|
|
237
|
+
with tarfile.open(barfile, mode='r') as tf:
|
|
238
|
+
manifest = tf.extractfile('MANIFEST')
|
|
239
|
+
if raw:
|
|
240
|
+
print(manifest.read().decode())
|
|
241
|
+
else:
|
|
242
|
+
pprint(json.load(manifest))
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
@cli.command()
|
|
246
|
+
@click.argument('barfile', type=click.Path(exists=True, dir_okay=False))
|
|
247
|
+
def verify(barfile):
|
|
248
|
+
""" Verify all manifest signatures available and all file hashes of the
|
|
249
|
+
specified barfile.
|
|
250
|
+
"""
|
|
251
|
+
with tarfile.open(barfile, mode='r') as tf:
|
|
252
|
+
manifest_bytes = tf.extractfile('MANIFEST').read()
|
|
253
|
+
manifest = json.loads(manifest_bytes.decode())
|
|
254
|
+
|
|
255
|
+
# check all the signatures match
|
|
256
|
+
for keyname, pubkey_b64 in keys.list_public_keys():
|
|
257
|
+
pubkey = Ed25519PublicKey.from_public_bytes(b64decode(pubkey_b64))
|
|
258
|
+
try:
|
|
259
|
+
sig_bytes = tf.extractfile(f'MANIFEST.{keyname}.sig').read()
|
|
260
|
+
pubkey.verify(sig_bytes, manifest_bytes)
|
|
261
|
+
log.info(f"Signature matches '{keyname}'")
|
|
262
|
+
except KeyError:
|
|
263
|
+
log.debug(f"no signature for '{keyname}'")
|
|
264
|
+
except InvalidSignature:
|
|
265
|
+
log.error(f"Invalid signature for '{keyname}'")
|
|
266
|
+
|
|
267
|
+
# check all the file hashes
|
|
268
|
+
for item in manifest['items']:
|
|
269
|
+
if (fname := item.get('filename')) is None:
|
|
270
|
+
continue
|
|
271
|
+
|
|
272
|
+
if (manifest_sha := item.get('sha256')) is None:
|
|
273
|
+
continue
|
|
274
|
+
|
|
275
|
+
log.debug(f"Checking hash for '{item.get('_id')}:{fname}'")
|
|
276
|
+
try:
|
|
277
|
+
file_sha = hashlib.sha256(tf.extractfile(fname).read())
|
|
278
|
+
if file_sha.hexdigest() != manifest_sha:
|
|
279
|
+
log.error(f"Hash does not match for '{item.get('_id')}'")
|
|
280
|
+
except KeyError:
|
|
281
|
+
log.error(f"Referenced file '{item.get('_id')}:{fname}' not in archive")
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
if __name__ == '__main__':
|
|
285
|
+
cli()
|