Coverage for klayout_pex/netlistsvg/netlistsvg_runner.py: 91%

33 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-06 13:14 +0000

1#! /usr/bin/env python3 

2# 

3# -------------------------------------------------------------------------------- 

4# SPDX-FileCopyrightText: 2024-2025 Martin Jan Köhler and Harald Pretl 

5# Johannes Kepler University, Institute for Integrated Circuits. 

6# 

7# This file is part of KPEX  

8# (see https://github.com/iic-jku/klayout-pex). 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <http://www.gnu.org/licenses/>. 

22# SPDX-License-Identifier: GPL-3.0-or-later 

23# -------------------------------------------------------------------------------- 

24# 

25 

26import os 

27import time 

28from typing import * 

29 

30import subprocess 

31 

32from ..log import ( 

33 info, 

34 # warning, 

35 rule, 

36 subproc, 

37) 

38 

39 

40SKIN_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'analog_skin.svg') 

41"""The analog skin of netlistsvg (its lib/analog.svg), shipped as package data""" 

42 

43 

44class NetlistSVGError(Exception): 

45 pass 

46 

47 

48def run_netlistsvg(exe_path: str, 

49 skin_path: str, 

50 yosys_json_netlist_path: str, 

51 output_svg_path: str, 

52 layout_elk_path: Optional[str]): 

53 """ 

54 :raises NetlistSVGError: if netlistsvg can't be started or fails 

55 """ 

56 args = [ 

57 exe_path, 

58 '-o', output_svg_path, 

59 '--skin', skin_path, 

60 ] 

61 if layout_elk_path is not None: 

62 args += [ 

63 '--layout', layout_elk_path 

64 ] 

65 args += [ 

66 yosys_json_netlist_path, 

67 ] 

68 

69 info('Calling netlistsvg') 

70 subproc(' '.join(args)) 

71 

72 rule('netlistsvg Output') 

73 

74 start = time.time() 

75 

76 try: 

77 proc = subprocess.Popen(args, 

78 stdin=subprocess.DEVNULL, 

79 stdout=subprocess.PIPE, 

80 stderr=subprocess.STDOUT, 

81 text=True, 

82 encoding='utf-8', 

83 errors='replace') 

84 except OSError as e: 

85 raise NetlistSVGError(f"Can't run netlistsvg {exe_path}: {e}") from e 

86 while True: 

87 line = proc.stdout.readline() 

88 if not line: 

89 break 

90 subproc(line[:-1]) # remove newline 

91 proc.wait() 

92 

93 duration = time.time() - start 

94 

95 rule() 

96 

97 if proc.returncode == 0: 

98 info(f"netlistsvg succeeded after {'%.4g' % duration}s") 

99 else: 

100 raise NetlistSVGError(f"netlistsvg failed with status code {proc.returncode} " 

101 f"after {'%.4g' % duration}s, see its output above")