Tim Edwards | 9d3debb | 2020-10-20 20:52:18 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 2 | # |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 3 | # rename_models --- |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 4 | # |
| 5 | # This script renames include paths from SPICE files in the models |
| 6 | # (libs.tech/ngspice) directory that point to the original ../cells/ |
| 7 | # directory, to point to the correct location in libs.ref/sky130_fd_pr/spice/ |
| 8 | # |
| 9 | # |
| 10 | # This script is a filter to be run by setting the name of this script as |
| 11 | # the value to "filter=" for the model install in the sky130 Makefile. |
| 12 | |
| 13 | import re |
| 14 | import os |
| 15 | import sys |
| 16 | |
Tim Edwards | 8ae2261 | 2021-08-27 20:54:41 -0400 | [diff] [blame] | 17 | def filter(inname, outname, ef_format = True): |
| 18 | |
| 19 | if ef_format: |
| 20 | libpath = 'spi/sky130_fd_pr/' |
| 21 | else: |
| 22 | libpath = 'sky130_fd_pr/spice/' |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 23 | |
| 24 | # Read input |
| 25 | try: |
| 26 | with open(inname, 'r') as inFile: |
| 27 | spitext = inFile.read() |
| 28 | spilines = spitext.splitlines() |
| 29 | except: |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 30 | print('rename_models.py: failed to open ' + fnmIn + ' for reading.', file=sys.stderr) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 31 | return 1 |
| 32 | |
| 33 | # Process input with regexp |
| 34 | |
| 35 | fixedlines = [] |
| 36 | modified = False |
| 37 | |
| 38 | for line in spilines: |
| 39 | |
| 40 | # Fix: Replace "../cells/<name>/" with "../../libs.ref/sky130_fd_pr/spice/" |
Tim Edwards | 8ae2261 | 2021-08-27 20:54:41 -0400 | [diff] [blame] | 41 | fixedline = re.sub('\.\./cells/[^/]+/', '../../libs.ref/' + libpath, line) |
Tim Edwards | 1c74f85 | 2021-06-09 16:46:50 -0400 | [diff] [blame] | 42 | |
| 43 | # This subsitution makes SPICE files compatible with Xyce without |
| 44 | # breaking ngspice compatibility ('$' comments changed to ';') |
| 45 | fixedline = re.sub('(.*[ \t]+)\$([ \t+].*)', '\g<1>;\g<2>', fixedline) |
| 46 | |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 47 | fixedlines.append(fixedline) |
| 48 | if fixedline != line: |
| 49 | modified = True |
| 50 | |
| 51 | # Write output |
| 52 | if outname == None: |
| 53 | for i in fixedlines: |
| 54 | print(i) |
| 55 | else: |
| 56 | # If the output is a symbolic link but no modifications have been made, |
| 57 | # then leave it alone. If it was modified, then remove the symbolic |
| 58 | # link before writing. |
| 59 | if os.path.islink(outname): |
| 60 | if not modified: |
| 61 | return 0 |
| 62 | else: |
| 63 | os.unlink(outname) |
| 64 | try: |
| 65 | with open(outname, 'w') as outFile: |
| 66 | for i in fixedlines: |
| 67 | print(i, file=outFile) |
| 68 | except: |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 69 | print('rename_models.py: failed to open ' + outname + ' for writing.', file=sys.stderr) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 70 | return 1 |
| 71 | |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | |
| 75 | # This script expects to get one or two arguments. One argument is |
| 76 | # mandatory and is the input file. The other argument is optional and |
| 77 | # is the output file. The output file and input file may be the same |
| 78 | # name, in which case the original input is overwritten. |
| 79 | |
| 80 | options = [] |
| 81 | arguments = [] |
| 82 | for item in sys.argv[1:]: |
| 83 | if item.find('-', 0) == 0: |
| 84 | options.append(item[1:]) |
| 85 | else: |
| 86 | arguments.append(item) |
| 87 | |
| 88 | if len(arguments) > 0: |
| 89 | infilename = arguments[0] |
Tim Edwards | 8ae2261 | 2021-08-27 20:54:41 -0400 | [diff] [blame] | 90 | else: |
| 91 | print('Usage: rename_models.py <filename> [<outfilename>] [-ef_format]') |
| 92 | sys.exit(1) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 93 | |
| 94 | if len(arguments) > 1: |
| 95 | outfilename = arguments[1] |
| 96 | else: |
| 97 | outfilename = None |
| 98 | |
Tim Edwards | 8ae2261 | 2021-08-27 20:54:41 -0400 | [diff] [blame] | 99 | ef_format = False |
| 100 | if len(options) > 0: |
| 101 | if options[0] == 'ef_format': |
| 102 | ef_format = True |
| 103 | |
| 104 | result = filter(infilename, outfilename, ef_format) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 105 | sys.exit(result) |