@@ -630,88 +630,5 @@ def setup_logging(env_key="LOG_CFG"):
630
630
logging .config .fileConfig (Settings .loggingconf )
631
631
632
632
633
- def safely_run (path : Path , cmd : List [str ], timeout : int ) -> None :
634
- """
635
- Executes a subprocess command through a dynamically created Python script.
636
- """
637
- import shlex
638
- from runpy import run_path
639
-
640
- try :
641
- # Don't modify the original command - create the full path properly
642
- full_cmd = " " .join (cmd )
643
- script_content = f"""import subprocess
644
- subprocess.run({ shlex .quote (full_cmd )} , shell=True, timeout={ timeout } )"""
645
-
646
- # Create the script path correctly
647
- script_path = path .parent / "cmd.py"
648
-
649
- # Write the script
650
- script_path .write_text (script_content )
651
-
652
- # Execute the script
653
- run_path (str (script_path ))
654
-
655
- except FileNotFoundError as e :
656
- print (f"Runner unable to be created or read: { e } " )
657
- except Exception as e :
658
- print (f"Error during execution: { e } " )
659
-
660
-
661
- def safely_run_save_output (path : Union [str , Path ], cmd : List [str ], timeout : int ) -> Any :
662
- """
663
- Executes a command through a dynamic script and returns its output.
664
- Similar to subprocess.run with capture_output=True.
665
- """
666
- import json
667
- import shlex
668
- from pathlib import Path
669
-
670
- try :
671
- full_cmd = " " .join (cmd )
672
- # Ensure path is a Path object
673
- path_obj = Path (path ) if isinstance (path , str ) else path
674
- # Create a temporary file to store the output
675
- output_file = path_obj .parent / "cmd_output.json"
676
-
677
- # Create script that captures output and saves to file
678
- script_content = f"""
679
- import subprocess
680
- import json
681
-
682
- try:
683
- result = subprocess.run({ shlex .quote (full_cmd )} , shell=True, capture_output=True, text=True, timeout={ timeout } )
684
- output = {{"stdout": result.stdout, "stderr": result.stderr, "returncode": result.returncode}}
685
- except subprocess.CalledProcessError as e:
686
- output = {{"stdout": e.stdout, "stderr": e.stderr, "returncode": e.returncode}}
687
- except Exception as e:
688
- output = {{"stdout": "", "stderr": str(e), "returncode": -1}}
689
-
690
- with open("{ output_file } ", "w") as f:
691
- json.dump(output, f)
692
- """
693
-
694
- script_path = path_obj .parent / "cmd.py"
695
- script_path .write_text (script_content )
696
-
697
- # Execute the script
698
- from runpy import run_path
699
-
700
- run_path (str (script_path ))
701
-
702
- # Read the output
703
- with open (output_file ) as f :
704
- result = json .load (f )
705
-
706
- # Clean up temporary files
707
- script_path .unlink (missing_ok = True )
708
- output_file .unlink (missing_ok = True )
709
-
710
- # Handle errors similar to subprocess.run(check=True)
711
- if result ["returncode" ] != 0 :
712
- raise RuntimeError (f"Command failed with exit code { result ['returncode' ]} \n Stderr: { result ['stderr' ]} " )
713
-
714
- return result ["stdout" ]
715
-
716
- except Exception as e :
717
- raise RuntimeError (f"Failed to execute command: { e } " )
633
+ def safely_run (path : Union [str , Path ], cmd : List [str ], timeout : int ) -> Any :
634
+ return subprocess .run ({shlex .quote (full_cmd )}, shell = False , capture_output = True , text = True , timeout = {timeout })
0 commit comments