| #!/usr/bin/env python3 |
| |
| import os |
| import pprint |
| import sys |
| |
| |
| def main(outdir, f): |
| outdir = os.path.abspath(outdir) |
| |
| fpath = os.path.abspath(f) |
| assert os.path.exists(fpath), fpath |
| assert os.path.isfile(fpath), fpath |
| fdir = os.path.dirname(f) |
| assert os.path.exists(fdir), fdir |
| assert os.path.isdir(fdir), fdir |
| |
| ffile = os.path.basename(f) |
| fbase, fext = ffile.split('.', 1) |
| |
| print("split_one_model.py: Reading", fpath) |
| data = open(fpath).read().splitlines() |
| |
| sections = [[]] |
| while len(data) > 0: |
| line = data.pop(0) |
| if line.startswith('.model'): |
| sections.append([]) |
| if sections[-1] and sections[-1][0].startswith('.model'): |
| if line.strip() and (line.strip()[0] not in ('*', '+')): |
| sections.append([]) |
| if 'subckt' in line: |
| sections.append([]) |
| sections[-1].append(line) |
| if sections[-1] and 'subckt' in sections[-1][0]: |
| if 'ends' in line: |
| sections.append([]) |
| |
| top = sections.pop(0) |
| top_file = list(top) |
| for s in sections: |
| if not s: |
| continue |
| if s[0].startswith('.model'): |
| bits = s[0].split() |
| assert len(bits) > 2, bits |
| outname = bits[1]+'.'+fext |
| elif 'subckt' in s[0]: |
| bits = s[0].replace('.subckt', 'subckt').split() |
| assert 'subckt' in bits, bits |
| outname = bits[bits.index('subckt')+1]+'.'+fext |
| else: |
| top_file.extend(s) |
| continue |
| |
| foutpath = os.path.join(outdir, outname) |
| assert not os.path.exists(foutpath), foutpath |
| print("split_one_model.py: Writing", foutpath) |
| with open(foutpath, 'w') as f: |
| f.write("\n".join(top+s)) |
| |
| top_file.append('.include "{}"'.format(outname)) |
| |
| foutpath = os.path.join(outdir, fbase+'_top.'+fext) |
| assert not os.path.exists(foutpath), foutpath |
| print("split_one_model.py: Writing", foutpath) |
| with open(foutpath, 'w') as f: |
| f.write("\n".join(top_file)) |
| |
| return 0 |
| |
| |
| if __name__ == "__main__": |
| files = list(sys.argv[1:]) |
| outdir = files.pop(-1) |
| for f in files: |
| retcode = main(outdir, f) |
| if retcode != 0: |
| sys.exit(retcode) |
| sys.exit(0) |