def testBackup(self):
        """
		This method tests :meth:`foundations.rotatingBackup.RotatingBackup.backup` method.
		"""

        tempDirectory = tempfile.mkdtemp()
        rotatingBackup = RotatingBackup(TEST_FILE, tempDirectory, 3)
        rotatingBackup.backup()
        self.assertTrue(
            os.path.exists(
                os.path.join(tempDirectory, os.path.basename(TEST_FILE))))
        for i in range(1, 4):
            rotatingBackup.backup()
            self.assertTrue(
                os.path.exists(
                    os.path.join(
                        tempDirectory,
                        os.path.basename("{0}.{1}".format(TEST_FILE, i)))))
        rotatingBackup.backup()
        self.assertFalse(
            os.path.exists(
                os.path.join(tempDirectory,
                             os.path.basename("{0}.4".format(TEST_FILE)))))
        shutil.rmtree(tempDirectory)
예제 #2
0
    def initialize(self):
        """
		Initializes the Component.

		:return: Method success.
		:rtype: bool
		"""

        LOGGER.debug("> Initializing '{0}' Component.".format(
            self.__class__.__name__))

        LOGGER.debug("> Initializing '{0}' SQLiteDatabase.".format(
            Constants.databaseFile))
        if self.__engine.parameters.databaseDirectory:
            if foundations.common.pathExists(
                    self.__engine.parameters.databaseDirectory):
                self.__databaseName = os.path.join(
                    self.__engine.parameters.databaseDirectory,
                    Constants.databaseFile)
            else:
                raise foundations.exceptions.DirectoryExistsError(
                    "{0} | '{1}' Database storing directory doesn't exists, {2} will now close!"
                    .format(self.__class__.__name__,
                            self.__engine.parameters.databaseDirectory,
                            Constants.applicationName))
        else:
            self.__databaseName = os.path.join(
                self.__engine.userApplicationDataDirectory,
                Constants.databaseDirectory, Constants.databaseFile)

        LOGGER.info("{0} | Session Database location: '{1}'.".format(
            self.__class__.__name__, self.__databaseName))
        self.__databaseConnectionString = "sqlite:///{0}".format(
            self.__databaseName)

        if foundations.common.pathExists(self.__databaseName):
            if not self.__engine.parameters.databaseReadOnly:
                backupDestination = os.path.join(
                    os.path.dirname(self.databaseName),
                    self.__databaseBackupDirectory)

                LOGGER.info("{0} | Backing up '{1}' Database to '{2}'!".format(
                    self.__class__.__name__, Constants.databaseFile,
                    backupDestination))
                rotatingBackup = RotatingBackup(self.__databaseName,
                                                backupDestination,
                                                self.__databaseBackupCount)
                rotatingBackup.backup()
            else:
                LOGGER.info(
                    "{0} | Database backup deactivated by '{1}' command line parameter value!"
                    .format(self.__class__.__name__, "databaseReadOnly"))

        LOGGER.debug("> Creating Database engine.")
        self.__databaseEngine = sqlalchemy.create_engine(
            self.__databaseConnectionString)

        LOGGER.debug("> Creating Database metadata.")
        self.__databaseCatalog = Base.metadata
        self.__databaseCatalog.create_all(self.__databaseEngine)

        LOGGER.debug("> Initializing Database session.")
        self.__databaseSessionMaker = sibl_gui.components.core.database.operations.DEFAULT_SESSION_MAKER = \
        sqlalchemy.orm.sessionmaker(bind=self.__databaseEngine)

        self.__databaseSession = sibl_gui.components.core.database.operations.DEFAULT_SESSION = \
        self.__databaseSessionMaker()

        self.initialized = True
        return True