Skip to content

Commit 7c52de9

Browse files
committed
openflow: add configure.py
A class to configure the OCI engine, containers and tool names.
1 parent 3248478 commit 7c52de9

File tree

5 files changed

+201
-1
lines changed

5 files changed

+201
-1
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,33 @@ Currently, it is based on `GHDL`, `Yosys`, `ghdl-yosys-plugin`, `nextpnr`,
1313

1414
> **NOTE:** it started as part of [PyFPGA](https://github.com/PyFPGA/pyfpga)
1515
> and will be used to solves the `openflow` **tool**.
16+
17+
## Installation
18+
19+
Openflow requires Python `>=3.6`. For now, it's only available as a git repository
20+
hosted on GitHub. It can be installed with pip:
21+
22+
```
23+
pip install 'git+https://github.com/PyFPGA/pyfpga#egg=pyfpga'
24+
```
25+
26+
> On GNU/Linux, installing pip packages on the system requires `sudo`.
27+
> Alternatively, use `--local` for installing PyFPGA in your HOME.
28+
29+
You can get a copy of the repository either through git clone or downloading a
30+
tarball/zipfile:
31+
32+
```
33+
git clone https://github.com/PyFPGA/openflow.git
34+
cd openflow
35+
```
36+
37+
Then, use pip from the root of the repo:
38+
39+
```
40+
pip install -e .
41+
```
42+
43+
> With `-e` (`--editable`) your application is installed into site-packages via
44+
> a kind of symlink. That allows pulling changes through git or changing the
45+
> branch, without the need to reinstall the package.

openflow/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
__version__ = '0.1.0'
44

55
from openflow.openflow import Openflow
6+
from openflow.configure import ConfigureTools

openflow/configure.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#
2+
# Copyright (C) 2020-2021 Rodrigo A. Melo
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
#
17+
18+
"""openflow.configure
19+
20+
A Class to configure the OCI engine, the containers and the name of the
21+
underlying FOSS tools.
22+
"""
23+
24+
25+
import os
26+
from yaml import safe_load, dump
27+
28+
29+
_DEFAULTS = {
30+
'ecppack': 'hdlc/prjtrellis',
31+
'ghdl': 'hdlc/ghdl:yosys',
32+
'icepack': 'hdlc/icestorm',
33+
'iceprog': '--device /dev/bus/usb hdlc/icestorm',
34+
'icetime': 'hdlc/icestorm',
35+
'nextpnr-ecp5': 'hdlc/nextpnr:ecp5',
36+
'nextpnr-ice40': 'hdlc/nextpnr:ice40',
37+
'openocd': '--device /dev/bus/usb hdlc/prog',
38+
'yosys': 'hdlc/ghdl:yosys',
39+
}
40+
41+
42+
class ConfigureTools:
43+
"""Configure Tools."""
44+
45+
def __init__(self, filename=None):
46+
"""Class constructor."""
47+
self.configs = {}
48+
if filename is not None:
49+
with open(filename, 'r') as file:
50+
self.configs = safe_load(file)
51+
else:
52+
self.configs['engine'] = 'docker'
53+
self.configs['volumes'] = ['$HOME:$HOME']
54+
self.configs['work'] = '$PWD'
55+
self.configs['containers'] = {}
56+
self.configs['names'] = {}
57+
for key, value in _DEFAULTS.items():
58+
self.configs['containers'][key] = value
59+
self.configs['names'][key] = key
60+
61+
def get_command(self, tool):
62+
"""Get the command-line needed to run a tool."""
63+
oci = ''
64+
container = ''
65+
name = self.configs['names'][tool]
66+
if self.configs['engine'] is not None:
67+
oci = '{} run --rm {} {}'.format(
68+
self.configs['engine'],
69+
'-v ' + ('-v ').join(self.configs['volumes']),
70+
'-w ' + self.configs['work']
71+
)
72+
container = self.configs['containers'][tool]
73+
return '{} {} {}'.format(oci, container, name)
74+
return name
75+
76+
def get_tools(self):
77+
"""Returns the list of configured tools."""
78+
return sorted(list(self.configs['names'].keys()))
79+
80+
def dump(self):
81+
"""Dumps the configuration in YAML format (debug purpouses)."""
82+
return dump(self.configs)
83+
84+
def set_engine(self, engine):
85+
"""Set the OCI engine."""
86+
self.configs['engine'] = engine
87+
88+
def unset_engine(self):
89+
"""Disable the OCI engine. """
90+
self.configs['engine'] = None
91+
92+
def set_volumes(self, volumes):
93+
"""Set the volumes of the OCI engine."""
94+
self.configs['volumes'] = volumes
95+
96+
def set_work(self, work):
97+
"""Set the work of the OCI engine."""
98+
self.configs['work'] = work
99+
100+
def set_container(self, tool, container):
101+
"""Set the container of the specified tool."""
102+
self.configs['containers'][tool] = container
103+
104+
def set_name(self, tool, name):
105+
"""Set the name of the specified tool."""
106+
self.configs['names'][tool] = name

openflow/helpers/configure.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (C) 2021 Rodrigo A. Melo
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
19+
"""
20+
A CLI to get the full OCI command for the specified tool.
21+
"""
22+
23+
import argparse
24+
25+
from openflow import __version__ as version
26+
from openflow.configure import ConfigureTools
27+
28+
29+
def main():
30+
"""Solves the main functionality of this helper."""
31+
32+
cfg = ConfigureTools()
33+
34+
# Parsing the command-line.
35+
36+
parser = argparse.ArgumentParser(
37+
description=__doc__,
38+
formatter_class=argparse.RawDescriptionHelpFormatter
39+
)
40+
41+
parser.add_argument(
42+
'-v', '--version',
43+
action='version',
44+
version='v{}'.format(version)
45+
)
46+
47+
parser.add_argument(
48+
'tool',
49+
metavar='TOOL',
50+
choices=cfg.get_tools(),
51+
help=', '.join(cfg.get_tools())
52+
)
53+
54+
args = parser.parse_args()
55+
56+
# Solving the functionality
57+
58+
print(cfg.get_command(args.tool))
59+
60+
61+
if __name__ == "__main__":
62+
main()

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
'console_scripts': [
2121
'openflow_syn = openflow.helpers.synthesis:main',
2222
'openflow_imp = openflow.helpers.implementation:main',
23-
'openflow_bit = openflow.helpers.bitstream:main'
23+
'openflow_bit = openflow.helpers.bitstream:main',
24+
'openflow_cfg = openflow.helpers.configure:main'
2425
],
2526
},
2627
classifiers=[

0 commit comments

Comments
 (0)