##// END OF EJS Templates
fallback on copy, copyfile if copy2 fails...
MinRK -
Show More
@@ -85,7 +85,25 class FileNotebookManager(NotebookManager):
85 os.mkdir(new)
85 os.mkdir(new)
86 except:
86 except:
87 raise TraitError("Couldn't create checkpoint dir %r" % new)
87 raise TraitError("Couldn't create checkpoint dir %r" % new)
88
88
89 def _copy(self, src, dest):
90 """copy src to dest
91
92 try copy2, fallback to copy, then finally copyfile
93 """
94 exc = None
95 for name in ('copy2', 'copy', 'copyfile'):
96 cp = getattr(shutil, name)
97 try:
98 cp(src, dest)
99 except OSError as e:
100 exc = e # assignment required for Python 3
101 self.log.debug("%s(%r,%r) failed: %s", name, src, dest, e)
102 else:
103 return
104 # will only get here if all copies fail:
105 raise exc
106
89 def get_notebook_names(self, path=''):
107 def get_notebook_names(self, path=''):
90 """List all notebook names in the notebook dir and path."""
108 """List all notebook names in the notebook dir and path."""
91 path = path.strip('/')
109 path = path.strip('/')
@@ -432,7 +450,7 class FileNotebookManager(NotebookManager):
432 self.log.debug("creating checkpoint for notebook %s", name)
450 self.log.debug("creating checkpoint for notebook %s", name)
433 if not os.path.exists(self.checkpoint_dir):
451 if not os.path.exists(self.checkpoint_dir):
434 os.mkdir(self.checkpoint_dir)
452 os.mkdir(self.checkpoint_dir)
435 shutil.copy2(nb_path, cp_path)
453 self._copy(nb_path, cp_path)
436
454
437 # return the checkpoint info
455 # return the checkpoint info
438 return self.get_checkpoint_model(checkpoint_id, name, path)
456 return self.get_checkpoint_model(checkpoint_id, name, path)
@@ -465,7 +483,7 class FileNotebookManager(NotebookManager):
465 # ensure notebook is readable (never restore from an unreadable notebook)
483 # ensure notebook is readable (never restore from an unreadable notebook)
466 with io.open(cp_path, 'r', encoding='utf-8') as f:
484 with io.open(cp_path, 'r', encoding='utf-8') as f:
467 current.read(f, u'json')
485 current.read(f, u'json')
468 shutil.copy2(cp_path, nb_path)
486 self._copy(cp_path, nb_path)
469 self.log.debug("copying %s -> %s", cp_path, nb_path)
487 self.log.debug("copying %s -> %s", cp_path, nb_path)
470
488
471 def delete_checkpoint(self, checkpoint_id, name, path=''):
489 def delete_checkpoint(self, checkpoint_id, name, path=''):
General Comments 0
You need to be logged in to leave comments. Login now