@@ -186,7 +186,8 @@ def _check_execution(self, job_id, tool_id, command_line):
186186 for file in self ._list_dir (tool_files_dir ):
187187 if os .path .isdir (join (tool_files_dir , file )):
188188 continue
189- contents = open (join (tool_files_dir , file )).read ()
189+ with open (join (tool_files_dir , file )) as fh :
190+ contents = fh .read ()
190191 log .debug ("job_id: {} - checking tool file {}" .format (job_id , file ))
191192 authorization .authorize_tool_file (basename (file ), contents )
192193 config_files_dir = job_directory .configs_directory ()
@@ -255,28 +256,21 @@ def calculate_path(self, remote_path, input_type):
255256
256257 def read_file (self , name , size = - 1 , default = None ):
257258 path = self ._job_file (name )
258- job_file = None
259259 try :
260- job_file = open (path , 'rb' )
261- return job_file .read (size )
260+ with open (path , 'rb' ) as job_file :
261+ return job_file .read (size )
262262 except Exception :
263263 if default is not None :
264264 return default
265265 else :
266266 raise
267- finally :
268- if job_file :
269- job_file .close ()
270267
271268 def write_file (self , name , contents ):
272269 path = self ._job_file (name )
273- job_file = open (path , 'wb' )
274- try :
275- if isinstance (contents , str ):
276- contents = contents .encode ("UTF-8" )
270+ if isinstance (contents , str ):
271+ contents = contents .encode ("UTF-8" )
272+ with open (path , "wb" ) as job_file :
277273 job_file .write (contents )
278- finally :
279- job_file .close ()
280274 return path
281275
282276 def remove_file (self , name ):
0 commit comments