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_cells --- |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 4 | # |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 5 | # This script renames include paths from SPICE files in the cells |
| 6 | # (libs.ref/sky130_fd_pr) directory that point to other cells |
| 7 | # directories, to point to the correct location in libs.ref/sky130_fd_pr/spice/ |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 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_cells.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 | |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 35 | # These substitutions are for files originating from cells/*/*.spice |
Tim Edwards | 00bfca5 | 2021-05-04 10:48:46 -0400 | [diff] [blame] | 36 | fixedline = re.sub('\.\./\.\./models/', '../../../libs.tech/ngspice/', line) |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 37 | fixedline = re.sub('\.\./[^/\.]+/', '', fixedline) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 38 | |
| 39 | fixedlines.append(fixedline) |
| 40 | if fixedline != line: |
| 41 | modified = True |
| 42 | |
| 43 | # Write output |
| 44 | if outname == None: |
| 45 | for i in fixedlines: |
| 46 | print(i) |
| 47 | else: |
| 48 | # If the output is a symbolic link but no modifications have been made, |
| 49 | # then leave it alone. If it was modified, then remove the symbolic |
| 50 | # link before writing. |
| 51 | if os.path.islink(outname): |
| 52 | if not modified: |
| 53 | return 0 |
| 54 | else: |
| 55 | os.unlink(outname) |
| 56 | try: |
| 57 | with open(outname, 'w') as outFile: |
| 58 | for i in fixedlines: |
| 59 | print(i, file=outFile) |
| 60 | except: |
Tim Edwards | bfc8269 | 2020-09-20 21:33:08 -0400 | [diff] [blame] | 61 | print('rename_cells.py: failed to open ' + outname + ' for writing.', file=sys.stderr) |
Tim Edwards | c6202ef | 2020-09-20 17:16:33 -0400 | [diff] [blame] | 62 | return 1 |
| 63 | |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | |
| 67 | # This script expects to get one or two arguments. One argument is |
| 68 | # mandatory and is the input file. The other argument is optional and |
| 69 | # is the output file. The output file and input file may be the same |
| 70 | # name, in which case the original input is overwritten. |
| 71 | |
| 72 | options = [] |
| 73 | arguments = [] |
| 74 | for item in sys.argv[1:]: |
| 75 | if item.find('-', 0) == 0: |
| 76 | options.append(item[1:]) |
| 77 | else: |
| 78 | arguments.append(item) |
| 79 | |
| 80 | if len(arguments) > 0: |
| 81 | infilename = arguments[0] |
| 82 | |
| 83 | if len(arguments) > 1: |
| 84 | outfilename = arguments[1] |
| 85 | else: |
| 86 | outfilename = None |
| 87 | |
| 88 | result = filter(infilename, outfilename) |
| 89 | sys.exit(result) |