def test_pack(self): fsm = FileStructManager(base_dir=self.base_dir, is_continue=False) cm = CheckpointsManager(fsm) with self.assertRaises(CheckpointsManager.SMException): cm.pack() os.mkdir(cm.weights_file()) os.mkdir(cm.optimizer_state_file()) with self.assertRaises(CheckpointsManager.SMException): cm.pack() shutil.rmtree(cm.weights_file()) shutil.rmtree(cm.optimizer_state_file()) f = open(cm.weights_file(), 'w') f.close() f = open(cm.optimizer_state_file(), 'w') f.close() f = open(cm.trainer_file(), 'w') f.close() try: cm.pack() except Exception as err: self.fail('Exception on packing files: [{}]'.format(err)) for f in [cm.weights_file(), cm.optimizer_state_file()]: if os.path.exists(f) and os.path.isfile(f): self.fail("File '{}' doesn't remove after pack".format(f)) result = os.path.join( fsm.get_path(cm, check=False, create_if_non_exists=False), 'last_checkpoint.zip') self.assertTrue(os.path.exists(result) and os.path.isfile(result)) f = open(cm.weights_file(), 'w') f.close() f = open(cm.optimizer_state_file(), 'w') f.close() f = open(cm.trainer_file(), 'w') f.close() try: cm.pack() result = os.path.join( fsm.get_path(cm, check=False, create_if_non_exists=False), 'last_checkpoint.zip.old') self.assertTrue(os.path.exists(result) and os.path.isfile(result)) except Exception as err: self.fail('Fail to pack with existing previous state file')
def test_object_registration(self): fsm = FileStructManager(base_dir=self.base_dir, is_continue=False) fsm_exist_ok = FileStructManager(base_dir=self.base_dir, is_continue=False, exists_ok=True) o = self.TestObj(fsm, 'test_dir', 'test_name') fsm.register_dir(o) expected_path = os.path.join(self.base_dir, 'test_dir') self.assertFalse(os.path.exists(expected_path)) self.assertEqual(fsm.get_path(o), expected_path) with self.assertRaises(FileStructManager.FSMException): fsm.register_dir(self.TestObj(fsm, 'test_dir', 'another_name')) try: fsm.register_dir(self.TestObj(fsm, 'test_dir', 'another_name'), check_dir_registered=False) fsm_exist_ok.register_dir( self.TestObj(fsm, 'test_dir', 'another_name')) fsm_exist_ok.register_dir(self.TestObj(fsm, 'test_dir', 'another_name2'), check_dir_registered=False) except: self.fail("Folder registrable test fail when it's disabled") with self.assertRaises(FileStructManager.FSMException): fsm.register_dir(self.TestObj(fsm, 'another_dir', 'test_name')) fsm.register_dir(self.TestObj(fsm, 'another_dir', 'another_name')) with self.assertRaises(FileStructManager.FSMException): fsm_exist_ok.register_dir( self.TestObj(fsm, 'another_dir', 'test_name')) fsm_exist_ok.register_dir( self.TestObj(fsm, 'another_dir', 'another_name')) try: fsm.register_dir(self.TestObj(fsm, 'another_dir2', 'test_name'), check_name_registered=False) fsm_exist_ok.register_dir(self.TestObj(fsm, 'another_dir2', 'test_name'), check_name_registered=False) except: self.fail("Folder registrable test fail when it's disabled") os.makedirs(os.path.join(self.base_dir, 'dir_dir', 'dir')) with self.assertRaises(FileStructManager.FSMException): fsm.register_dir(self.TestObj(fsm, 'dir_dir', 'name_name')) try: fsm_exist_ok.register_dir(self.TestObj(fsm, 'dir_dir', 'name_name')) except: self.fail("Folder registrable test fail when exists_ok=True")
def test_initialisation(self): fsm = FileStructManager(base_dir=self.base_dir, is_continue=False) try: cm = CheckpointsManager(fsm) except Exception as err: self.fail("Fail init CheckpointsManager; err: ['{}']".format(err)) with self.assertRaises(FileStructManager.FSMException): CheckpointsManager(fsm) os.mkdir(os.path.join(fsm.get_path(cm), 'test_dir')) with self.assertRaises(FileStructManager.FSMException): CheckpointsManager(fsm)