Moved to argparse, pathlib, subprocess

Signed-off-by: Wojciech Gryncewicz <wgryncewicz@antmicro.com>
This commit is contained in:
Wojciech Gryncewicz 2020-11-18 19:42:07 +01:00
parent 3a9fef6ed1
commit 17c37e15cc
1 changed files with 75 additions and 26 deletions

View File

@ -14,17 +14,25 @@ import csv
import json import json
import os import os
import sys import sys
import argparse
import pathlib
# prerequisities: netlistsvg import glob
# input: paths to cell dirs, containing Yosys netlist import subprocess
# output: generates [cell_prefix].schematic.svg
# example usage 1: ./netlistsvg-generate.py \
# ../../../libraries/sky130_fd_sc_ms/latest/cells/a2111o \
# ../../../libraries/sky130_fd_sc_ms/latest/cells/a2111oi
# example usage 2: ./netlistsvg-generate.py ALLLIBS
def outfile(cellpath, define_data, ftype='', extra='', exists=False): def outfile(cellpath, define_data, ftype='', extra='', exists=False):
''' Determines output file path and name.
Args:
cellpath - path to a cell [str of pathlib.Path]
define_data - cell definition data [dic]
ftype - file type suffix [str]
extra - extra suffix [str]
exist - optional check if file exists [bool or None]
Returns:
outpath - output file namepath [str]
'''
fname = define_data['name'].lower().replace('$', '_') fname = define_data['name'].lower().replace('$', '_')
if ftype: if ftype:
ftype = '.'+ftype ftype = '.'+ftype
@ -40,18 +48,29 @@ def outfile(cellpath, define_data, ftype='', extra='', exists=False):
def write_netlistsvg(cellpath, define_data): def write_netlistsvg(cellpath, define_data):
''' Generates netlistsvg for a given cell.
Args:
cellpath - path to a cell [str of pathlib.Path]
define_data - cell definition data [dic]
'''
netlist_json = os.path.join(cellpath, define_data['file_prefix']+'.json') netlist_json = os.path.join(cellpath, define_data['file_prefix']+'.json')
if not os.path.exists(netlist_json): if not os.path.exists(netlist_json):
print("No " + define_data['file_prefix'] + ".json in", cellpath) print("No netlist in", cellpath)
assert os.path.exists(netlist_json), netlist_json assert os.path.exists(netlist_json), netlist_json
outpath = outfile(cellpath, define_data, 'schematic') outpath = outfile(cellpath, define_data, 'schematic')
oscmd = 'netlistsvg ' + netlist_json + ' -o ' + outpath if subprocess.call(['netlistsvg', netlist_json, '-o', outpath]):
r = os.system(oscmd)>>8 raise ChildProcessError("netlistsvg execution failed")
assert r == 0
return r
def process(cellpath): def process(cellpath):
''' Processes cell indicated by path.
Opens cell definiton and calls further processing
Args:
cellpath - path to a cell [str of pathlib.Path]
'''
print() print()
print(cellpath) print(cellpath)
define_json = os.path.join(cellpath, 'definition.json') define_json = os.path.join(cellpath, 'definition.json')
@ -66,20 +85,50 @@ def process(cellpath):
return return
def main(args): def main():
if len(args) and args[0] == 'ALLLIBS': ''' Generates netlistsvg schematic from cell netlist.'''
scrpath = os.path.dirname(os.path.realpath(__file__))
relpath = '/../../../libraries/*/latest/cells/*' prereq_txt = 'prerequisities:\n netlistsvg'
#relpath = '/../../../libraries/*ms/latest/cells/x*' # DBG: limited output_txt = 'output:\n generates [cell_prefix].schematic.svg'
args = os.popen('ls -d ' + scrpath + relpath).read().strip().split('\n') allcellpath = '../../../libraries/*/latest/cells/*'
parser = argparse.ArgumentParser(
description = main.__doc__,
epilog = prereq_txt +'\n\n'+ output_txt,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"--all_libs",
help="process all cells in "+allcellpath,
action="store_true")
parser.add_argument(
"cell_dir",
help="path to the cell directory",
type=pathlib.Path,
nargs="*")
args = parser.parse_args()
if args.all_libs:
path = pathlib.Path(allcellpath).expanduser()
parts = path.parts[1:] if path.is_absolute() else path.parts
paths = pathlib.Path(path.root).glob(str(pathlib.Path("").joinpath(*parts)))
args.cell_dir = list(paths)
cell_dirs = [d.resolve() for d in args.cell_dir if d.is_dir()]
errors = 0 errors = 0
for a in args: for d in cell_dirs:
try: try:
process(os.path.realpath(a)) process(d)
except: except KeyboardInterrupt:
sys.exit(1)
except (AssertionError, FileNotFoundError, ChildProcessError) as ex:
print (f'Error: {type(ex).__name__}')
print (f'{ex.args}')
errors +=1 errors +=1
print (f'\n{len(args)} files processed, {errors} errors.') print (f'\n{len(cell_dirs)} files processed, {errors} errors.')
return 0 if errors else 1
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main(sys.argv[1:])) sys.exit(main())