Example #1
0
    def loadConfig(self):
        """
    Reload the configuration object from disk if its override has changed. The
    override may be changed by a different process. If that's a possibility for
    your app, you may use this method to conditionally reload the config
    periodically.

    NOTE: the ability to detect change is based on file modification time
    resolution, which was one second at the time of this writing
    """
        overrideConfigPath = os.path.join(self._getConfigOverrideDir(),
                                          self._configName)

        overrideConfigChanged = False
        mtime = 0
        try:
            mtime = os.path.getmtime(overrideConfigPath)
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise
            if self._lastModTime != 0:
                # Override config was there earlier but disappeared
                overrideConfigChanged = True
        else:
            if mtime > self._lastModTime:
                # Override config's modification time changed
                overrideConfigChanged = True

        if ((self._mode == self.MODE_LOGICAL and not self._baselineLoaded)
                or overrideConfigChanged):
            # Reset config cache
            self._sections.clear()
            self._defaults.clear()

            if self._mode == self.MODE_LOGICAL:
                # Load baseline config
                baseConfigPath = os.path.join(self.baseConfigDir,
                                              self._configName)
                try:
                    with open(baseConfigPath, "r") as fileObj:
                        self.readfp(fileObj)
                except IOError as e:
                    if e.errno != errno.ENOENT:
                        raise
                    raise ValueError(
                        "Baseline configuration object not found: %r" %
                        (baseConfigPath))

                self._baselineLoaded = True

            if overrideConfigChanged:
                if mtime != 0:
                    # Load override config
                    with open(overrideConfigPath, "r") as fileObj:
                        with file_lock.SharedFileLock(fileObj):
                            self.readfp(fileObj)

                self._lastModTime = mtime
Example #2
0
 def testSharedLock(self):
     flock = file_lock.SharedFileLock(self._tempFileID)
     # Check creation of file_lock object
     self.assertEqual(type(flock), file_lock._FileLock)