@@ -149,28 +149,27 @@ def main(args=None):
149149 parser .add_argument ('output_file' , default = '-' , nargs = '?' ,
150150 help = 'output file name' )
151151 args = parser .parse_args (args )
152- input_file = io .open (args .input_file , 'rb' )
153- sparse_map = list (get_sparse_map (input_file ))
154- header_name = args .input_file
155- if args .override_name :
156- header_name = args .override_name
157- tar_info = TarSparseInfo (header_name , sparse_map )
158- if args .output_file == '-' :
159- output = io .open ('/dev/stdout' , 'wb' )
160- else :
161- output = io .open (args .output_file , 'wb' )
162- if args .use_compress_program :
163- compress = subprocess .Popen ([args .use_compress_program ],
164- stdin = subprocess .PIPE , stdout = output )
165- output = compress .stdin
166- else :
167- compress = None
168- output .write (tar_info .tobuf (tarfile .PAX_FORMAT ))
169- copy_sparse_data (input_file , output , sparse_map )
170- finalize (output )
171- input_file .close ()
172- output .close ()
152+ with io .open (args .input_file , 'rb' ) as input_file :
153+ sparse_map = list (get_sparse_map (input_file ))
154+ header_name = args .input_file
155+ if args .override_name :
156+ header_name = args .override_name
157+ tar_info = TarSparseInfo (header_name , sparse_map )
158+ with io .open (('/dev/stdout' if args .output_file == '-'
159+ else args .output_file ),
160+ 'wb' ) as output :
161+ if args .use_compress_program :
162+ # pylint: disable=consider-using-with
163+ compress = subprocess .Popen ([args .use_compress_program ],
164+ stdin = subprocess .PIPE , stdout = output )
165+ output = compress .stdin
166+ else :
167+ compress = None
168+ output .write (tar_info .tobuf (tarfile .PAX_FORMAT ))
169+ copy_sparse_data (input_file , output , sparse_map )
170+ finalize (output )
173171 if compress is not None :
172+ compress .stdin .close ()
174173 compress .wait ()
175174 return compress .returncode
176175 return 0
0 commit comments