blob: 5692c41d33173dd8619b11c6e8ab26420d6e0ab4 [file] [log] [blame]
Tim Edwards9d3debb2020-10-20 20:52:18 -04001#!/usr/bin/env python3
Tim Edwardsc6202ef2020-09-20 17:16:33 -04002#
Tim Edwardsbfc82692020-09-20 21:33:08 -04003# rename_cells ---
Tim Edwardsc6202ef2020-09-20 17:16:33 -04004#
Tim Edwardsbfc82692020-09-20 21:33:08 -04005# 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 Edwardsc6202ef2020-09-20 17:16:33 -04008#
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
13import re
14import os
15import sys
16
17def 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 Edwardsbfc82692020-09-20 21:33:08 -040025 print('rename_cells.py: failed to open ' + fnmIn + ' for reading.', file=sys.stderr)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040026 return 1
27
28 # Process input with regexp
29
30 fixedlines = []
31 modified = False
32
33 for line in spilines:
34
Tim Edwardsbfc82692020-09-20 21:33:08 -040035 # These substitutions are for files originating from cells/*/*.spice
Tim Edwards00bfca52021-05-04 10:48:46 -040036 fixedline = re.sub('\.\./\.\./models/', '../../../libs.tech/ngspice/', line)
Tim Edwardsbfc82692020-09-20 21:33:08 -040037 fixedline = re.sub('\.\./[^/\.]+/', '', fixedline)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040038
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 Edwardsbfc82692020-09-20 21:33:08 -040061 print('rename_cells.py: failed to open ' + outname + ' for writing.', file=sys.stderr)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040062 return 1
63
64
65if __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)