1
+ import os
1
2
from abc import ABC , abstractmethod
2
3
from typing import Optional
3
4
4
5
import typer
5
6
6
- from cycode .cli .logger import logger
7
7
from cycode .cli .models import Document
8
8
from cycode .cli .utils .path_utils import get_file_content , get_file_dir , get_path_from_context , join_paths
9
9
from cycode .cli .utils .shell_executor import shell
@@ -15,30 +15,27 @@ def build_dep_tree_path(path: str, generated_file_name: str) -> str:
15
15
16
16
def execute_commands (
17
17
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 ,
21
20
working_directory : Optional [str ] = None ,
22
21
) -> Optional [str ]:
23
22
try :
24
- all_dependencies = []
23
+ outputs = []
25
24
26
- # Run all commands and collect outputs
27
25
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 )
30
29
31
- dependencies = '\n ' .join (all_dependencies )
30
+ joined_output = '\n ' .join (outputs )
32
31
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 :
39
36
return None
40
37
41
- return dependencies
38
+ return joined_output
42
39
43
40
44
41
class BaseRestoreDependencies (ABC ):
@@ -64,27 +61,25 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
64
61
relative_restore_file_path = build_dep_tree_path (document .path , self .get_lock_file_name ())
65
62
working_directory_path = self .get_working_directory (document )
66
63
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 (
72
66
self .get_commands (manifest_file_path ),
73
- manifest_file_path ,
74
67
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 ,
77
70
)
78
- restore_file_content = get_file_content (restore_file_path )
71
+ if output is None : # one of the commands failed
72
+ return None
79
73
74
+ restore_file_content = get_file_content (restore_file_path )
80
75
return Document (relative_restore_file_path , restore_file_content , self .is_git_diff )
81
76
82
77
def get_working_directory (self , document : Document ) -> Optional [str ]:
83
78
return None
84
79
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 )
88
83
89
84
@abstractmethod
90
85
def is_project (self , document : Document ) -> bool :
0 commit comments