1+ import os
12from abc import ABC , abstractmethod
23from typing import Optional
34
45import typer
56
6- from cycode .cli .logger import logger
77from cycode .cli .models import Document
88from cycode .cli .utils .path_utils import get_file_content , get_file_dir , get_path_from_context , join_paths
99from cycode .cli .utils .shell_executor import shell
@@ -15,30 +15,27 @@ def build_dep_tree_path(path: str, generated_file_name: str) -> str:
1515
1616def execute_commands (
1717 commands : list [list [str ]],
18- file_name : str ,
19- command_timeout : int ,
20- dependencies_file_name : Optional [str ] = None ,
18+ timeout : int ,
19+ output_file_path : Optional [str ] = None ,
2120 working_directory : Optional [str ] = None ,
2221) -> Optional [str ]:
2322 try :
24- all_dependencies = []
23+ outputs = []
2524
26- # Run all commands and collect outputs
2725 for command in commands :
28- dependencies = shell (command = command , timeout = command_timeout , working_directory = working_directory )
29- all_dependencies .append (dependencies ) # Collect each command's output
26+ command_output = shell (command = command , timeout = timeout , working_directory = working_directory )
27+ if command_output :
28+ outputs .append (command_output )
3029
31- dependencies = '\n ' .join (all_dependencies )
30+ joined_output = '\n ' .join (outputs )
3231
33- # Write all collected outputs to the file if dependencies_file_name is provided
34- if dependencies_file_name :
35- with open (dependencies_file_name , 'w' ) as output_file : # Open once in 'w' mode to start fresh
36- output_file .writelines (dependencies )
37- except Exception as e :
38- logger .debug ('Failed to restore dependencies via shell command, %s' , {'filename' : file_name }, exc_info = e )
32+ if output_file_path :
33+ with open (output_file_path , 'w' , encoding = 'UTF-8' ) as output_file :
34+ output_file .writelines (joined_output )
35+ except Exception :
3936 return None
4037
41- return dependencies
38+ return joined_output
4239
4340
4441class BaseRestoreDependencies (ABC ):
@@ -64,27 +61,25 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
6461 relative_restore_file_path = build_dep_tree_path (document .path , self .get_lock_file_name ())
6562 working_directory_path = self .get_working_directory (document )
6663
67- if self .verify_restore_file_already_exist (restore_file_path ):
68- restore_file_content = get_file_content (restore_file_path )
69- else :
70- output_file_path = restore_file_path if self .create_output_file_manually else None
71- execute_commands (
64+ if not self .verify_restore_file_already_exist (restore_file_path ):
65+ output = execute_commands (
7266 self .get_commands (manifest_file_path ),
73- manifest_file_path ,
7467 self .command_timeout ,
75- output_file_path ,
76- working_directory_path ,
68+ output_file_path = restore_file_path if self . create_output_file_manually else None ,
69+ working_directory = working_directory_path ,
7770 )
78- restore_file_content = get_file_content (restore_file_path )
71+ if output is None : # one of the commands failed
72+ return None
7973
74+ restore_file_content = get_file_content (restore_file_path )
8075 return Document (relative_restore_file_path , restore_file_content , self .is_git_diff )
8176
8277 def get_working_directory (self , document : Document ) -> Optional [str ]:
8378 return None
8479
85- @abstractmethod
86- def verify_restore_file_already_exist (self , restore_file_path : str ) -> bool :
87- pass
80+ @staticmethod
81+ def verify_restore_file_already_exist (restore_file_path : str ) -> bool :
82+ return os . path . isfile ( restore_file_path )
8883
8984 @abstractmethod
9085 def is_project (self , document : Document ) -> bool :
0 commit comments