@@ -1028,45 +1028,42 @@ def store_minimized_testcase(
10281028
10291029 # Prepare the file.
10301030 zip_path = None
1031+ testcase_filepath = None
10311032 if testcase .archive_state :
10321033 if len (file_list ) > 1 :
10331034 testcase .archive_state |= data_types .ArchiveStatus .MINIMIZED
10341035 minimize_task_output .archive_state = testcase .archive_state
10351036 zip_path = os .path .join (
10361037 environment .get_value ('INPUT_DIR' ), '%d.zip' % testcase .key .id ())
1037- zip_file = zipfile .ZipFile (zip_path , 'w' )
1038- count = 0
1039- filtered_file_list = []
1040- for file_name in file_list :
1041- absolute_filename = os .path .join (base_directory , file_name )
1042- is_file = os .path .isfile (absolute_filename )
1043- if file_to_run_data and is_file and os .path .getsize (
1044- absolute_filename ) == 0 and (os .path .basename (
1045- absolute_filename ).encode ('utf-8' ) not in file_to_run_data ):
1046- continue
1047- if not os .path .exists (absolute_filename ):
1048- continue
1049- zip_file .write (absolute_filename , file_name , zipfile .ZIP_DEFLATED )
1050- if is_file :
1051- count += 1
1052- filtered_file_list .append (absolute_filename )
1053-
1054- zip_file .close ()
1038+ with zipfile .ZipFile (zip_path , 'w' ) as zip_file :
1039+ count = 0
1040+ filtered_file_list = []
1041+ for file_name in file_list :
1042+ absolute_filename = os .path .join (base_directory , file_name )
1043+ is_file = os .path .isfile (absolute_filename )
1044+ if file_to_run_data and is_file and os .path .getsize (
1045+ absolute_filename ) == 0 and (os .path .basename (
1046+ absolute_filename ).encode ('utf-8' ) not in file_to_run_data ):
1047+ continue
1048+ if not os .path .exists (absolute_filename ):
1049+ continue
1050+ zip_file .write (absolute_filename , file_name , zipfile .ZIP_DEFLATED )
1051+ if is_file :
1052+ count += 1
1053+ filtered_file_list .append (absolute_filename )
1054+
10551055 try :
10561056 if count > 1 :
1057- file_handle = open ( zip_path , 'rb' )
1057+ testcase_filepath = zip_path
10581058 else :
10591059 if not filtered_file_list :
10601060 # We minimized everything. The only thing needed to reproduce is the
10611061 # interaction gesture.
1062- file_path = file_list [0 ]
1063- file_handle = open (file_path , 'wb' )
1064- file_handle .close ()
1062+ testcase_filepath = file_list [0 ]
10651063 else :
1066- file_path = filtered_file_list [0 ]
1067- file_handle = open (file_path , 'rb' )
1068- testcase .absolute_path = os .path .join (base_directory ,
1069- os .path .basename (file_path ))
1064+ testcase_filepath = filtered_file_list [0 ]
1065+ testcase .absolute_path = os .path .join (
1066+ base_directory , os .path .basename (testcase_filepath ))
10701067 minimize_task_output .absolute_path = testcase .absolute_path
10711068 testcase .archive_state &= ~ data_types .ArchiveStatus .MINIMIZED
10721069 minimize_task_output .archive_state = testcase .archive_state
@@ -1075,20 +1072,20 @@ def store_minimized_testcase(
10751072 return
10761073 else :
10771074 absolute_filename = os .path .join (base_directory , file_list [0 ])
1078- file_handle = open ( absolute_filename , 'rb' )
1075+ testcase_filepath = absolute_filename
10791076 testcase .archive_state &= ~ data_types .ArchiveStatus .MINIMIZED
10801077 minimize_task_output .archive_state = testcase .archive_state
10811078 else :
1082- file_handle = open ( file_list [0 ], 'rb' )
1079+ testcase_filepath = file_list [0 ]
10831080 testcase .archive_state &= ~ data_types .ArchiveStatus .MINIMIZED
10841081 minimize_task_output .archive_state = testcase .archive_state
10851082
10861083 # Store the testcase.
1087- data = file_handle .read ()
1088- storage .upload_signed_url (data , minimize_task_input .testcase_upload_url )
1089- minimized_keys = minimize_task_input .testcase_blob_name
1090- file_handle .close ()
1084+ with open (testcase_filepath , 'rb' ) as file_handle :
1085+ data = file_handle .read ()
1086+ storage .upload_signed_url (data , minimize_task_input .testcase_upload_url )
10911087
1088+ minimized_keys = minimize_task_input .testcase_blob_name
10921089 testcase .minimized_keys = minimized_keys
10931090 minimize_task_output .minimized_keys = minimized_keys
10941091
@@ -1318,9 +1315,8 @@ def combine_tokens(tokens):
13181315 return b''
13191316
13201317 # TODO(mbarbella): Allow token combining functions to write files directly.
1321- handle = open (combined_file_path , 'rb' )
1322- result = handle .read ()
1323- handle .close ()
1318+ with open (combined_file_path , 'rb' ) as handle :
1319+ result = handle .read ()
13241320
13251321 shell .remove_file (combined_file_path )
13261322 return result
0 commit comments