blob: 3ebd38c3eea804b9b2ca4b68008f48a4687aeada [file] [log] [blame]
Tim Edwards55f4d0e2020-07-05 15:41:02 -04001#!/usr/bin/env python3
2#
3# changepath.py: Look up all .mag files in maglef paths from the root
4# PDK directory given as the first argument, and replace them with the
5# path that is given as the second argument.
6#
7# The purpose of this script is to prepare a technology for relocation.
8# This script may be expanded to take care of all relocation issues.
9# For now, only the property "GDS_FILE" in each .mag file is modified.
10#
11# Usage, e.g.:
12#
13# changepath.py /home/tim/projects/efabless/tech/XFAB/EFXH035B/libs.ref/mag/D_CELLS /ef/tech/XFAB/EFXH035B/libs.ref/mag/D_CELLS
14
15import os
16import re
17import sys
18import glob
19
20def usage():
21 print("changepath.py <orig_path_to_dir> <target_path_to_dir>")
22 return 0
23
24if __name__ == '__main__':
25
26 if len(sys.argv) == 1:
27 usage()
28 sys.exit(0)
29
30 optionlist = []
31 arguments = []
32
33 for option in sys.argv[1:]:
34 if option.find('-', 0) == 0:
35 optionlist.append(option)
36 else:
37 arguments.append(option)
38
39 if len(arguments) != 2:
40 print("Wrong number of arguments given to changepath.py.")
41 usage()
42 sys.exit(0)
43
44 source = arguments[0]
45 target = arguments[1]
46
47 gdssource = source.replace('/mag/', '/gds/')
48 gdssource = gdssource.replace('/maglef/', '/gds/')
49 gdstarget = target.replace('/mag/', '/gds/')
50 gdstarget = gdstarget.replace('/maglef/', '/gds/')
51
52 magpath = source + '/*.mag'
53 sourcefiles = glob.glob(magpath)
54
55 if len(sourcefiles) == 0:
56 print("Warning: No files were found in the path " + magpath + ".")
57
58 for file in sourcefiles:
59 # print("Converting file " + file)
60 with open(file, 'r') as ifile:
61 magtext = ifile.read().splitlines()
62
63 proprex = re.compile('string[ \t]+GDS_FILE[ \t]+([^ \t]+)')
64 with open(file, 'w') as ofile:
65 for line in magtext:
66 pmatch = proprex.match(line)
67 if pmatch:
68 filepath = pmatch.group(1)
69 if filepath.startswith(gdssource):
70 print('string GDS_FILE ' + filepath.replace(gdssource, gdstarget), file=ofile)
71 else:
72 print(line, file=ofile)
73 else:
74 print(line, file=ofile)
75