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 | |
| 17 | def filter(inname, outname): |
| 18 | |
| 19 | # Read input |
| 20 | try: |
| 21 | with open(inname, 'r') as inFile: |
| 22 | spitext = inFile.read() |
| 23 | spilines = spitext.splitlines() |
| 24 | except: |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 25 | 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] | 26 | return 1 |
| 27 | |
| 28 | # Process input with regexp |
| 29 | |
| 30 | fixedlines = [] |
| 31 | modified = False |
| 32 | |
| 33 | for line in spilines: |
| 34 | |
| 35 | # Fix: Replace "../cells/<name>/" with "../../libs.ref/sky130_fd_pr/spice/" |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 36 | fixedline = re.sub('\.\./cells/[^/]+/', '../../libs.ref/sky130_fd_pr/spice/', line) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 37 | fixedlines.append(fixedline) |
| 38 | if fixedline != line: |
| 39 | modified = True |
| 40 | |
| 41 | # Write output |
| 42 | if outname == None: |
| 43 | for i in fixedlines: |
| 44 | print(i) |
| 45 | else: |
| 46 | # If the output is a symbolic link but no modifications have been made, |
| 47 | # then leave it alone. If it was modified, then remove the symbolic |
| 48 | # link before writing. |
| 49 | if os.path.islink(outname): |
| 50 | if not modified: |
| 51 | return 0 |
| 52 | else: |
| 53 | os.unlink(outname) |
| 54 | try: |
| 55 | with open(outname, 'w') as outFile: |
| 56 | for i in fixedlines: |
| 57 | print(i, file=outFile) |
| 58 | except: |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 59 | 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] | 60 | return 1 |
| 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | |
| 65 | # This script expects to get one or two arguments. One argument is |
| 66 | # mandatory and is the input file. The other argument is optional and |
| 67 | # is the output file. The output file and input file may be the same |
| 68 | # name, in which case the original input is overwritten. |
| 69 | |
| 70 | options = [] |
| 71 | arguments = [] |
| 72 | for item in sys.argv[1:]: |
| 73 | if item.find('-', 0) == 0: |
| 74 | options.append(item[1:]) |
| 75 | else: |
| 76 | arguments.append(item) |
| 77 | |
| 78 | if len(arguments) > 0: |
| 79 | infilename = arguments[0] |
| 80 | |
| 81 | if len(arguments) > 1: |
| 82 | outfilename = arguments[1] |
| 83 | else: |
| 84 | outfilename = None |
| 85 | |
| 86 | result = filter(infilename, outfilename) |
| 87 | sys.exit(result) |