Skip to content

Commit

Permalink
Put back rm -rf instead of rmtree() for non-Windows systems.
Browse files Browse the repository at this point in the history
`rm -rf` appears more likely to succeed rather than throwing an OSError.
  • Loading branch information
emitra17 committed Jul 5, 2019
1 parent 6cb953c commit 24af099
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
28 changes: 19 additions & 9 deletions pybnf/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,18 @@ def run_simulation(self, debug=False, failed_logs_dir=''):
logger.warning('Discarding Result %s as having an infinite objective function value' % res.name)
res.simdata = None
if self.delete_folder:
try:
shutil.rmtree(self.folder)
self.jlogger.debug('Removed folder %s' % self.folder)
except OSError:
self.jlogger.error('Failed to remove folder %s.' % self.folder)
if os.name == 'nt': # Windows
try:
shutil.rmtree(self.folder)
self.jlogger.debug('Removed folder %s' % self.folder)
except OSError:
self.jlogger.error('Failed to remove folder %s.' % self.folder)
else:
try:
run(['rm', '-rf', self.folder], check=True, timeout=1800)
self.jlogger.debug('Removed folder %s' % self.folder)
except (CalledProcessError, TimeoutExpired):
self.jlogger.error('Failed to remove folder %s.' % self.folder)

return res

Expand Down Expand Up @@ -988,10 +995,13 @@ def run(self, client, resume=None, debug=False):
if (isinstance(self, SimplexAlgorithm) or self.config.config['refine'] != 1) and self.bootstrap_number is None:
# End of fitting; delete unneeded files
if self.config.config['delete_old_files'] >= 1:
try:
shutil.rmtree(self.sim_dir)
except OSError:
logger.error('Failed to remove simulations directory '+self.sim_dir)
if os.name == 'nt': # Windows
try:
shutil.rmtree(self.sim_dir)
except OSError:
logger.error('Failed to remove simulations directory '+self.sim_dir)
else:
run(['rm', '-rf', self.sim_dir]) # More likely to succeed than rmtree()

logger.info("Fitting complete")

Expand Down
11 changes: 9 additions & 2 deletions pybnf/pybnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pybnf.algorithms as algs
import pybnf.printing as printing

from subprocess import run
from numpy import inf

import logging
Expand Down Expand Up @@ -372,10 +373,16 @@ def main():
# (exists in directory where workers were instantiated)
# Tries current and home directories
if os.path.isdir('dask-worker-space'):
shutil.rmtree('dask-worker-space', ignore_errors=True)
if os.name == 'nt': # Windows
shutil.rmtree('dask-worker-space', ignore_errors=True)
else:
run(['rm', '-rf', 'dask-worker-space']) # More likely to succeed than rmtree()
home_dask_dir = os.path.expanduser(os.path.join('~', 'dask-worker-space'))
if os.path.isdir(home_dask_dir):
shutil.rmtree(home_dask_dir, ignore_errors=True)
if os.name == 'nt': # Windows
shutil.rmtree(home_dask_dir, ignore_errors=True)
else:
run(['rm', '-rf', home_dask_dir])

# After any error, try to clean up.
try:
Expand Down

0 comments on commit 24af099

Please sign in to comment.