blob: 1eea3dcaca3d87692658f1d61e2873cf5f8f7abb [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_models ---
Tim Edwardsc6202ef2020-09-20 17:16:33 -04004#
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
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_models.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
35 # Fix: Replace "../cells/<name>/" with "../../libs.ref/sky130_fd_pr/spice/"
Tim Edwardsbfc82692020-09-20 21:33:08 -040036 fixedline = re.sub('\.\./cells/[^/]+/', '../../libs.ref/sky130_fd_pr/spice/', line)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040037 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 Edwardsbfc82692020-09-20 21:33:08 -040059 print('rename_models.py: failed to open ' + outname + ' for writing.', file=sys.stderr)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040060 return 1
61
62
63if __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)