Skip to content

Commit f39b61e

Browse files
committed
openflow: add configure.py
It allows configuring the OCI engine, containers and tool names.
1 parent 3248478 commit f39b61e

File tree

5 files changed

+190
-1
lines changed

5 files changed

+190
-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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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
27+
28+
29+
_DEFAULTS = {
30+
'ecppack': 'hdlc/prjtrellis',
31+
'ghdl': 'hdlc/ghdl:yosys',
32+
'icepack': 'hdlc/icestorm',
33+
'iceprog': 'hdlc/icestorm',
34+
'icetime': 'hdlc/icestorm',
35+
'nextpnr-ecp5': 'hdlc/nextpnr:ecp5',
36+
'nextpnr-ice40': 'hdlc/nextpnr:ice40',
37+
'openocd': 'hdlc/prog',
38+
'yosys': 'hdlc/ghdl:yosys',
39+
}
40+
41+
42+
class ConfigureTools:
43+
"""Configure Tools."""
44+
45+
def __init__(self):
46+
"""Class constructor."""
47+
self.configs = {}
48+
self.configs['engine'] = 'docker'
49+
self.configs['volumes'] = ['$HOME:$HOME']
50+
self.configs['work'] = '$PWD'
51+
self.configs['containers'] = {}
52+
self.configs['names'] = {}
53+
for key, value in _DEFAULTS.items():
54+
self.configs['containers'][key] = value
55+
self.configs['names'][key] = key
56+
57+
def get_command(self, tool):
58+
"""Get the command-line needed to run a tool."""
59+
oci = ''
60+
container = ''
61+
command = self.configs['names'][tool]
62+
if self.configs['engine'] in ['docker', 'podman']:
63+
oci = '{} run --rm {} {}'.format(
64+
self.configs['engine'],
65+
'-v ' + ('-v ').join(self.configs['volumes']),
66+
'-w ' + self.configs['work']
67+
)
68+
container = self.configs['containers'][tool]
69+
return '{} {} {}'.format(oci, container, command)
70+
71+
def get_tools(self):
72+
"""Returns the list of configured tools."""
73+
return sorted(list(self.configs['names'].keys()))
74+
75+
def set_engine(self, engine, volumes, work):
76+
"""Set the OCI engine."""
77+
self.configs['engine'] = engine
78+
self.configs['volumes'] = volumes
79+
self.configs['work'] = work
80+
81+
def set_container(self, tool, container):
82+
"""Set the container of the specified tool."""
83+
self.configs['containers'][tool] = container
84+
85+
def set_name(self, tool, name):
86+
"""Set the name of the specified tool."""
87+
self.configs['names'][tool] = name
88+
89+
def set_configs_from_dict(self, configs):
90+
"""Set all the configurations from a dict."""
91+
raise NotImplementedError('set_configs_from_dict')
92+
93+
def set_configs_from_yaml(self, filename):
94+
"""Set all the configurations from a YAML file."""
95+
raise NotImplementedError('set_configs_from_yaml')

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)