@@ -436,17 +436,6 @@ def filter_ansi(line: str, is_windows: bool) -> str:
436
436
return line
437
437
438
438
439
- @dataclass
440
- class Command :
441
- """
442
- Custom dataclass for the generated command dictionaries.
443
- """
444
-
445
- command : str
446
- method : str # 'get' or 'put'
447
- headers : Dict [str , str ]
448
-
449
-
450
439
@dataclass
451
440
class CommandResult :
452
441
"""
@@ -985,7 +974,7 @@ def _generate_commands(
985
974
s3_path : str ,
986
975
in_path : str ,
987
976
out_path : str ,
988
- ) -> Tuple [List [Command ], Optional [ Dict ] ]:
977
+ ) -> Tuple [List [Dict ], dict ]:
989
978
"""
990
979
Generate commands for the specified bucket, S3 path, input path, and output path.
991
980
@@ -995,7 +984,7 @@ def _generate_commands(
995
984
:param out_path: Output path
996
985
:param method: The request method to use for the command (can be "get" or "put").
997
986
998
- :returns: List of Command dictionaries containing the command string and metadata .
987
+ :returns: A tuple containing a list of command dictionaries along with any ``put_args`` dictionaries .
999
988
"""
1000
989
1001
990
put_args , put_headers = self ._generate_encryption_settings ()
@@ -1029,7 +1018,7 @@ def _generate_commands(
1029
1018
),
1030
1019
# The "method" key indicates to _file_transport_command which commands are put_commands
1031
1020
"method" : "put" ,
1032
- "headers" : put_headers
1021
+ "headers" : put_headers ,
1033
1022
}) # fmt: skip
1034
1023
else :
1035
1024
put_command_headers = " " .join ([f"-H '{ h } : { v } '" for h , v in put_headers .items ()])
@@ -1055,6 +1044,8 @@ def _generate_commands(
1055
1044
"touch "
1056
1045
f"'{ out_path } '"
1057
1046
),
1047
+ "method" : "get" ,
1048
+ "headers" : {},
1058
1049
}) # fmt: skip
1059
1050
commands .append ({
1060
1051
"command" :
@@ -1066,12 +1057,12 @@ def _generate_commands(
1066
1057
),
1067
1058
# The "method" key indicates to _file_transport_command which commands are put_commands
1068
1059
"method" : "put" ,
1069
- "headers" : put_headers
1060
+ "headers" : put_headers ,
1070
1061
}) # fmt: skip
1071
1062
1072
1063
return commands , put_args
1073
1064
1074
- def _exec_transport_commands (self , in_path : str , out_path : str , commands : List [Command ]) -> CommandResult :
1065
+ def _exec_transport_commands (self , in_path : str , out_path : str , commands : List [dict ]) -> Tuple [ int , str , str ] :
1075
1066
"""
1076
1067
Execute the provided transport commands.
1077
1068
@@ -1084,7 +1075,10 @@ def _exec_transport_commands(self, in_path: str, out_path: str, commands: List[C
1084
1075
1085
1076
stdout_combined , stderr_combined = "" , ""
1086
1077
for command in commands :
1087
- (returncode , stdout , stderr ) = self .exec_command (command ["command" ], in_data = None , sudoable = False )
1078
+ result = self .exec_command (command ["command" ], in_data = None , sudoable = False )
1079
+
1080
+ returncode = result [0 ]
1081
+ stdout , stderr = result [1 ], result [2 ]
1088
1082
1089
1083
# Check the return code
1090
1084
if returncode != 0 :
@@ -1101,7 +1095,7 @@ def _file_transport_command(
1101
1095
in_path : str ,
1102
1096
out_path : str ,
1103
1097
ssm_action : str ,
1104
- ) -> CommandResult :
1098
+ ) -> Tuple [ int , str , str ] :
1105
1099
"""
1106
1100
Transfer file(s) to/from host using an intermediate S3 bucket and then delete the file(s).
1107
1101
@@ -1117,30 +1111,25 @@ def _file_transport_command(
1117
1111
1118
1112
client = self ._s3_client
1119
1113
1114
+ commands , put_args = self ._generate_commands (
1115
+ bucket_name ,
1116
+ s3_path ,
1117
+ in_path ,
1118
+ out_path ,
1119
+ )
1120
+
1120
1121
try :
1121
1122
if ssm_action == "get" :
1122
- put_commands , put_args = self ._generate_commands (
1123
- bucket_name ,
1124
- s3_path ,
1125
- in_path ,
1126
- out_path ,
1127
- )
1128
- put_commands = [cmd ["command" ] for cmd in put_commands if cmd .get ("method" ) == "put" ]
1129
- (returncode , stdout , stderr ) = self ._exec_transport_commands (in_path , out_path , put_commands )
1123
+ put_commands = [cmd for cmd in commands if cmd .get ("method" ) == "put" ]
1124
+ result = self ._exec_transport_commands (in_path , out_path , put_commands )
1130
1125
with open (to_bytes (out_path , errors = "surrogate_or_strict" ), "wb" ) as data :
1131
1126
client .download_fileobj (bucket_name , s3_path , data )
1132
1127
else :
1133
- get_commands , put_args = self ._generate_commands (
1134
- bucket_name ,
1135
- s3_path ,
1136
- in_path ,
1137
- out_path ,
1138
- )
1139
- get_commands = [cmd ["command" ] for cmd in get_commands if cmd .get ("method" ) == "get" ]
1128
+ get_commands = [cmd for cmd in commands if cmd .get ("method" ) == "get" ]
1140
1129
with open (to_bytes (in_path , errors = "surrogate_or_strict" ), "rb" ) as data :
1141
1130
client .upload_fileobj (data , bucket_name , s3_path , ExtraArgs = put_args )
1142
- ( returncode , stdout , stderr ) = self ._exec_transport_commands (in_path , out_path , get_commands )
1143
- return CommandResult (returncode , stdout , stderr )
1131
+ result = self ._exec_transport_commands (in_path , out_path , get_commands )
1132
+ return CommandResult (result )
1144
1133
finally :
1145
1134
# Remove the files from the bucket after they've been transferred
1146
1135
client .delete_object (Bucket = bucket_name , Key = s3_path )
0 commit comments