Skip to content

Commit

Permalink
#128: Big round of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrothea committed Apr 10, 2021
1 parent 53318c1 commit fa9820d
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 175 deletions.
2 changes: 1 addition & 1 deletion src/ipbb/cli/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def proj():
# ------------------------------------------------------------------------------
# TODO: move the list of supported products somewhere else
@proj.command('create', short_help="Create a new project area.")
@click.argument('toolset', type=click.Choice(['vivado', 'sim', 'vivadohls']))
@click.argument('toolset', type=click.Choice(['vivado', 'sim', 'vivado-hls']))
@click.argument('projname')
@click.argument('component', callback=validateComponent, autocompletion=completeComponent)
@click.argument('topdep', default='__auto__', autocompletion=completeDepFile('component'))
Expand Down
40 changes: 17 additions & 23 deletions src/ipbb/cmds/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import sys
import click

from click import echo, secho, style, confirm
from rich.prompt import Confirm
from rich.text import Text
from os.path import join, split, exists, abspath, splitext, relpath, basename

from ..console import cprint, console
from ..defaults import kProjAreaFile, kProjUserFile
from ..utils import DirSentry, formatDictTable
from ..tools.common import which
Expand All @@ -21,13 +24,9 @@ def cleanup(ictx):

lFiles.remove(f)

if lFiles and not click.confirm(
style(
"All files and directories in\n'{}'\n will be deleted.\nDo you want to continue?".format(
ictx.currentproj.path
),
fg='yellow',
)
if lFiles and not Confirm.ask(
Text(f"All files and directories in\n'{ictx.currentproj.path}'\n will be deleted.\nDo you want to continue?",
style='yellow')
):
return

Expand All @@ -41,7 +40,7 @@ def cleanup(ictx):
# ------------------------------------------------------------------------------
def user_config(ictx, aList, aAdd, aUnset):

echo("User settings")
cprint("User settings")

if aAdd:
lKey, lValue = aAdd
Expand All @@ -53,7 +52,7 @@ def user_config(ictx, aList, aAdd, aUnset):
ictx.currentproj.saveUserSettings()

if ictx.currentproj.usersettings:
echo(formatDictTable(ictx.currentproj.usersettings))
cprint(formatDictTable(ictx.currentproj.usersettings))


# ------------------------------------------------------------------------------
Expand All @@ -65,22 +64,17 @@ def addrtab(ictx, aDest):
except OSError:
pass

import sh

if not ictx.depParser.commands["addrtab"]:
secho(
"\nWARNING no address table files defined in {}.\n".format(
ictx.currentproj.name
),
fg='yellow',
cprint(
f"\nWARNING no address table files defined in {ictx.currentproj.name}.\n",
style='yellow',
)
return

for addrtab in ictx.depParser.commands["addrtab"]:
print(sh.cp('-avL', addrtab.filepath, join(aDest, basename(addrtab.filepath))))
secho(
"\n{}: Address table files collected in '{}'.\n".format(
ictx.currentproj.name, aDest
),
fg='green',
cprint(sh.cp('-avL', addrtab.filepath, join(aDest, basename(addrtab.filepath))))

console.log(
f"{ictx.currentproj.name}: Address table files collected in '{aDest}'.",
style='green',
)
57 changes: 26 additions & 31 deletions src/ipbb/cmds/ipbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import sys
import click

from click import echo, secho, style, confirm
from rich.prompt import Confirm

from os.path import join, split, exists, abspath, splitext, relpath, basename
from ..console import cprint, console
from ..defaults import kProjAreaFile, kProjUserFile
from ..tools.common import which, DEFAULT_ENCODING
from ..utils import DirSentry, formatDictTable
Expand All @@ -30,7 +32,7 @@ def gendecoders(ictx, aCheckUpToDate, aForce):
if not which(lGenScript):
raise click.ClickException("'{0}' script not found.".format(lGenScript))

secho("Using " + which(lGenScript), fg='green')
cprint(f"Using {which(lGenScript)}", style='green')

# ------------------------------------------------------------------------------

Expand All @@ -39,28 +41,24 @@ def gendecoders(ictx, aCheckUpToDate, aForce):
lErrors = {}
with DirSentry(join(ictx.currentproj.path, lDecodersDir)):
for lAddr in ictx.depParser.commands['addrtab']:
echo("Processing " + style(basename(lAddr.filepath), fg='blue'))
cprint(f"Processing [blue]{basename(lAddr.filepath)}[/blue]")
# Interested in top-level address tables only
if not lAddr.toplevel:
secho(
"{} is not a top-level address table. Decoder will not be generated.".format(
lAddr.filepath
),
fg='cyan',
cprint(
f"{lAddr.filepath} is not a top-level address table. Decoder will not be generated.",
style='cyan',
)
continue

# Generate a new decoder file
try:
lGen(basename(lAddr.filepath), _out=sys.stdout, _err=sys.stderr, _tee=True)
except Exception as lExc:
secho('Failed to generate decoder for '+basename(lAddr.filepath), fg='red')
cprint(f"Failed to generate decoder for {basename(lAddr.filepath)}", style='red')
lErrors[lAddr] = lExc
continue

lDecoder = 'ipbus_decode_{0}.vhd'.format(
splitext(basename(lAddr.filepath))[0]
)
lDecoder = f'ipbus_decode_{splitext(basename(lAddr.filepath))[0]}.vhd'
lTarget = ictx.pathMaker.getPath(
lAddr.package, lAddr.component, 'src', lDecoder
)
Expand All @@ -74,46 +72,43 @@ def gendecoders(ictx, aCheckUpToDate, aForce):
lUpdatedDecoders.append((lDecoder, lTarget))

if lErrors:
secho(
cprint(
"\nERROR: decoder generation failed",
fg='red',
style='red',
)
for a in sorted(lErrors):
echo(' - ' + basename(a.filepath))
echo(' ' + lErrors[a].stdout.decode(DEFAULT_ENCODING, "replace"))
cprint(' - ' + basename(a.filepath))
cprint(' ' + lErrors[a].stdout.decode(DEFAULT_ENCODING, "replace"))
raise SystemExit(-1)



# ------------------------------------------------------------------------------
# If no difference between old and newly generated decoders, quit here.
if not lUpdatedDecoders:
secho(
"\n{}: All ipbus decoders are up-to-date.\n".format(
ictx.currentproj.name
),
fg='green',
console.log(
f"{ictx.currentproj.name}: All ipbus decoders are up-to-date.",
style='green',
)
return

# ------------------------------------------------------------------------------
echo(
cprint(
'The following decoders have changed and must be updated:\n'
+ '\n'.join(map(lambda s: '* ' + style(s[0], fg='blue'), lUpdatedDecoders))
+ '\n'.join([f" * [blue]{d}[/blue]" for d in UpdatedDecoders])
+ '\n'
)
if aCheckUpToDate:
raise SystemExit(-1)

if not aForce:
confirm('Do you want to continue?', abort=True)
if not aForce and not Confirm.ask("Do you want to continue?"):
return

for lDecoder, lTarget in lUpdatedDecoders:
print(sh.cp('-av', lDecoder, lTarget))
cprint(sh.cp('-av', lDecoder, lTarget))

secho(
"\n\n{}: {} decoders updated.\n".format(
ictx.currentproj.name, len(lUpdatedDecoders)
),
fg='green',
console.log(
f"{ictx.currentproj.name}: {len(lUpdatedDecoders)} decoders updated.",
style='green',
)
# ------------------------------------------------------------------------------
8 changes: 3 additions & 5 deletions src/ipbb/cmds/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


# Elements
from ..console import cprint
from ..console import cprint, console
from ..tools.common import SmartOpen
from ..defaults import kProjAreaFile, kProjDir, kTopDep
from ..context import ProjectInfo
Expand Down Expand Up @@ -135,17 +135,15 @@ def create(ictx, toolset, projname, component, topdep):
pi = ProjectInfo()
pi.path = lProjAreaPath
pi.settings = {
'toolset': toolset,
'toolset': toolset.replace('-', '_'),
'topPkg': lTopPackage,
'topCmp': lTopComponent,
'topDep': lTopDep,
'name': projname,
}
pi.saveSettings()

cprint(
'{} project area \'{}\' created'.format(toolset.capitalize(), projname), style='green'
)
console.log(f"{toolset.capitalize()} project area '{projname}' created", style='green')


# ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/ipbb/cmds/schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_schema = {
'toolset': {'type': 'string', 'allowed': ['ModelSim', 'Vivado', 'VivadoHLS'], 'required': True},
'toolset': {'type': 'string', 'allowed': ['sim', 'vivado', 'vivado_hls'], 'required': True},
'device_generation': {'type': 'string'},
'device_name': {'type': 'string', 'required': True},
'device_speed': {'type': 'string', 'required': True},
Expand Down
25 changes: 16 additions & 9 deletions src/ipbb/cmds/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from copy import deepcopy
from rich.table import Table
from rich.prompt import Confirm

from .schema import project_schema

Expand Down Expand Up @@ -118,11 +119,16 @@ def sim(ictx, proj):
from .proj import cd

cd(ictx, projname=proj, aVerbose=False)
else:
if ictx.currentproj.name is None:
raise click.ClickException(
'Project area not defined. Move into a project area and try again.'
)

if ictx.currentproj.name is None:
raise click.ClickException(
'Project area not defined. Move into a project area and try again.'
)

lValidator = cerberus.Validator(_schema)
if not lValidator.validate(ictx.depParser.settings.dict()):
cprint(f"ERROR:\n{lValidator.errors}\n{ictx.depParser.settings.dict()}", style="red")
raise RuntimeError(f"vivadohls settings validation failed: {lValidator.errors}")

ensureModelsim(ictx)

Expand Down Expand Up @@ -165,7 +171,7 @@ def setupsimlib(ictx, aXilSimLibsPath, aForce):

if not lCompileSimlib:
cprint(
f"Xilinx simulation library exist at {lSimlibPath}. Compilation will be skipped.".
f"Xilinx simulation library exist at {lSimlibPath}. Compilation will be skipped."
)
else:
cprint(
Expand Down Expand Up @@ -239,10 +245,11 @@ def ipcores(ictx, aXilSimLibsPath, aToScript, aToStdout):

if not exists(lSimlibPath):
cprint(
f"WARNING: Xilinx simulation libraries not found. Likely this is a problem.\nPlease execute {getClickRootName()} sim setup-simlibs to generate them."
style='yellow',
f"WARNING: Xilinx simulation libraries not found. Likely this is a problem.\nPlease execute {getClickRootName()} sim setup-simlibs to generate them.",
style='yellow'
)
confirm("Do you want to continue anyway?", abort=True)
if not Confirm.ask("Do you want to continue anyway?"):
return
# -------------------------------------------------------------------------

lDepFileParser = ictx.depParser
Expand Down
7 changes: 4 additions & 3 deletions src/ipbb/cmds/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import sh
import os

# from click import echo, style, secho, confirm
from rich.prompt import Confirm
from os.path import basename, dirname, relpath, abspath, exists, splitext, join, isabs, sep, isdir, isfile
# from texttable import Texttable

from ..console import cprint, console
from ..depparser import Pathmaker, DepFileParser
Expand Down Expand Up @@ -216,7 +215,9 @@ def vhdl_beautify(env, component, path):
+ '\n'.join( [f"* [blue]{f}[/blue]" for f in lBeautifiedFiles])
+ '\n'
)
confirm('Do you want to continue?', abort=True)
if not Confirm.ask("Do you want to continue?"):
return

for lTarget, lBeautified in lBeautifiedFiles:
print(sh.cp('-av', lBeautified, lTarget))

Expand Down
30 changes: 20 additions & 10 deletions src/ipbb/cmds/vivado.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def ensureVivado(ictx):
)

# ------------------------------------------------------------------------------
def ensureVivadoProjPath(aProjPath):
def ensureVivadoProjPath(aProjPath: str):
"""Utility function to ensure that the project path exists
Args:
aProjPath (TYPE): Description
aProjPath (str): Vivado Project path
Raises:
click.ClickException: Description
Expand Down Expand Up @@ -101,14 +101,16 @@ def vivado(ictx, proj, verbosity, cmdlist):
from .proj import cd

cd(ictx, projname=proj, aVerbose=False)
return
else:
if ictx.currentproj.name is None:
raise click.ClickException(
'Project area not defined. Move to a project area and try again'
)

ensureVivado(ictx)
if ictx.currentproj.name is None:
raise click.ClickException(
'Project area not defined. Move to a project area and try again'
)

lValidator = cerberus.Validator(_schema)
if not lValidator.validate(ictx.depParser.settings.dict()):
cprint(f"ERROR:\n{lValidator.errors}\n{ictx.depParser.settings.dict()}", style="red")
raise RuntimeError(f"vivadohls settings validation failed: {lValidator.errors}")

lKeep = True
lLogLabel = None if not lKeep else '_'.join( cmdlist )
Expand All @@ -121,6 +123,9 @@ def vivado(ictx, proj, verbosity, cmdlist):

ictx.vivadoSessions = VivadoSessionManager(keep=lKeep, loglabel=lLogLabel)

ensureVivado(ictx)



# ------------------------------------------------------------------------------
def genproject(ictx, aEnableIPCache, aOptimise, aToScript, aToStdout):
Expand Down Expand Up @@ -164,10 +169,15 @@ def genproject(ictx, aEnableIPCache, aOptimise, aToScript, aToStdout):
except RuntimeError as lExc:
cprint(
"Error caught while generating Vivado TCL commands:",
style='red',
style='red'
)
cprint(lExc)
raise click.Abort()

console.log(
f"{ictx.currentproj.name}: Project created successfully.",
style='green',
)
# -------------------------------------------------------------------------


Expand Down
Loading

0 comments on commit fa9820d

Please sign in to comment.