77def run_herwig_get_commands(commands, phase="paths"):
78 """Execute multiple Herwig 'get' commands in a single shell session and parse the output.
79
80 Args:
81 commands (list): List of commands or tuples (for interfaces) to execute.
82 phase (str): Execution phase, either 'paths' for path commands or 'interfaces' for interface commands.
83
84 Returns:
85 list: List of tuples containing the command and its output or error message.
86 """
87 if not commands:
88 return []
89
90
91 herwig_path = os.getenv('HERWIG7_PATH')
92 if not herwig_path:
93 return [(cmd, "Error: Environment variable HERWIG7_PATH is not set") for cmd in commands]
94
95
96 herwig_cmd = f"{herwig_path}/bin/Herwig read --repo={herwig_path}/share/Herwig/HerwigDefaults.rpo"
97
98 try:
99
100 temp_script = f"temp_herwig_script_{os.getpid()}.in"
101 with open(temp_script, 'w') as f:
102 for cmd in commands:
103 if phase == "paths":
104 f.write(f"get {cmd}\n")
105 else:
106 f.write(f"get {cmd[0]}:{cmd[1]}\n")
107
108
109 cmd = f"cat {temp_script} | {herwig_cmd}"
110 result = subprocess.run(
111 cmd,
112 shell=True,
113 capture_output=True,
114 text=True,
115 timeout=10 * len(commands)
116 )
117
118
119 output = result.stdout.strip()
120 if result.stderr:
121 output += f"\nError: {result.stderr.strip()}"
122
123
124 Path(temp_script).unlink()
125
126
127 outputs = output.split("Herwig> get ")[1:] if output else []
128 results = []
129 for i, cmd in enumerate(commands):
130 if i < len(outputs):
131 raw_output = outputs[i].
strip()
132
133 cmd_str = cmd if phase == "paths" else f"{cmd[0]}:{cmd[1]}"
134 if raw_output.startswith(cmd_str):
135 raw_output = raw_output[len(cmd_str):].
strip()
136 if phase == "interfaces":
137
138 if len(raw_output.split("\n")) == 1:
139 if raw_output.split("\n")[0] != '' and raw_output.split("\n")[0] != 'Herwig>':
140 filtered_output = raw_output.split("\n")[0]
141 elif raw_output.split("\n")[0] != 'Herwig>':
142 filtered_output = "No response from the interface"
143 else:
144 filtered_output = "No response from the interface"
145 elif len(raw_output.split("\n")) == 2:
146 filtered_output = "No response from the interface"
147 elif len(raw_output.split("\n")) == 3:
148 if all("Herwig" in line for line in raw_output.split("\n")):
149 filtered_output = "No response from the interface"
150 else:
151 filtered_output = raw_output.split("\n")[1]
152 else:
153 filtered_output = raw_output
154 results.append((cmd[0], f"{cmd[0]}:{cmd[1]}", filtered_output))
155 else:
156 results.append((cmd, raw_output))
157 else:
158 if phase == "interfaces":
159 results.append((cmd[0], f"{cmd[0]}:{cmd[1]}", "Error: No output received"))
160 else:
161 results.append((cmd, "Error: No output received"))
162
163 return results
164
165 except subprocess.TimeoutExpired:
166 return [(cmd, "Error: Command timed out") if phase == "paths" else (cmd[0], f"{cmd[0]}:{cmd[1]}", "Error: Command timed out") for cmd in commands]
167 except Exception as e:
168 return [(cmd, f"Error: {str(e)}") if phase == "paths" else (cmd[0], f"{cmd[0]}:{cmd[1]}", f"Error: {str(e)}") for cmd in commands]
169