diff --git a/copy_with_port_portname.py b/copy_with_port_portname.py index 1211adf..083f72d 100644 --- a/copy_with_port_portname.py +++ b/copy_with_port_portname.py @@ -1,9 +1,59 @@ # copythefile.py import sys import os +import subprocess +import logging + +logging.basicConfig( + level=logging.INFO, + format='%(message)s', + datefmt='%Y-%m-%d %H:%M:%S' +) + +def run_copy_script(template_script_path_arg, new_port_name_arg, new_zmq_port_arg, output_directory_arg, python_exe): + base_template_name = os.path.basename(template_script_path_arg) + template_root, template_ext = os.path.splitext(base_template_name) + output_filename = f"{template_root}{template_ext}" + expected_output_path = os.path.join(output_directory_arg, output_filename) + + if os.path.exists(expected_output_path): + logging.info(f"Specialized script '{expected_output_path}' already exists. Skipping generation.") + return output_filename + + copy_script_path = os.path.join(".","copy_with_port_portname.py") + + cmd = [ + python_exe, + copy_script_path, + new_port_name_arg, + new_zmq_port_arg, + template_script_path_arg, + output_directory_arg + ] + logging.info(f"Running: {' '.join(cmd)}") + try: + result = subprocess.run(cmd, capture_output=True, text=True, check=True, encoding='utf-8') + logging.info(f"Successfully generated '{output_filename}' using copy_with_port_portname.py.") + if result.stdout: logging.debug(f"copy_with_port_portname.py stdout:\n{result.stdout}") + if result.stderr: logging.warning(f"copy_with_port_portname.py stderr:\n{result.stderr}") + return output_filename + except subprocess.CalledProcessError as e: + logging.error(f"Error calling copy_with_port_portname.py for '{template_script_path_arg}' with port_name '{new_port_name_arg}':") + logging.error(f"Command: {' '.join(e.cmd)}") + logging.error(f"Return code: {e.returncode}") + logging.error(f"Stdout: {e.stdout}") + logging.error(f"Stderr: {e.stderr}") + return None + except FileNotFoundError: + logging.error(f"Error: Python executable or copy_with_port_portname.py script not found.") + logging.error(f"Attempted command: {' '.join(cmd)}") + return None + except Exception as e: + logging.error(f"An unexpected error occurred while trying to run copy_script: {e}") + return None -def create_modified_script(template_script_path, zmq_port_name_val, zmq_port_val, output_dir): +def create_modified_script(template_script_path, zmq_port_name_val, zmq_port_val, output_dir): try: with open(template_script_path, 'r') as f: lines = f.readlines()