Пример #1
0
 def test_smoke(self):
     # Empty directory looks like an empty repository
     with TemporaryDirectory() as tmp:
         repo = SessionStorageRepository(tmp)
         self.assertEqual(repo.location, tmp)
         self.assertEqual(repo.get_storage_list(), [])
         self.assertEqual(list(iter(repo)), [])
         self.assertEqual(repo.get_last_storage(), None)
Пример #2
0
 def test_get_last_storage__broken_symlink(self):
     # Directory with some sub-directories looks like a repository
     # with a bunch of sessions inside.
     with TemporaryDirectory() as tmp:
         # Create a repository without any sessions and one broken symlink
         repo = SessionStorageRepository(tmp)
         self._populate_dummy_repo(repo, [], "b0rken")
         # Get the last storage object
         storage = repo.get_last_storage()
         # Make sure it's not valid
         self.assertEqual(storage, None)
Пример #3
0
 def test_get_last_storage(self):
     # Directory with some sub-directories looks like a repository
     # with a bunch of sessions inside.
     with TemporaryDirectory() as tmp:
         # Create a repository and some dummy data
         repo = SessionStorageRepository(tmp)
         self._populate_dummy_repo(repo)
         # Get the last storage object
         storage = repo.get_last_storage()
         # Check that we got session1
         self.assertEqual(
             os.path.basename(storage.location), 'session1')
Пример #4
0
 def test_get_storage_list(self):
     # Directory with some sub-directories looks like a repository
     # with a bunch of sessions inside.
     with TemporaryDirectory() as tmp:
         # Create a repository and some dummy data
         repo = SessionStorageRepository(tmp)
         self._populate_dummy_repo(repo)
         # Get a list of storage objects
         storage_list = repo.get_storage_list()
         # Check if we got our data right.
         # The results are not sorted so we sort them for testing
         storage_name_list = [
             os.path.basename(storage.location) for storage in storage_list
         ]
         self.assertEqual(sorted(storage_name_list),
                          ["session1", "session2"])
Пример #5
0
 def test_get_storage_list(self):
     # Directory with some sub-directories looks like a repository
     # with a bunch of sessions inside.
     with TemporaryDirectory() as tmp:
         # Create a repository and some dummy data
         repo = SessionStorageRepository(tmp)
         self._populate_dummy_repo(repo)
         # Get a list of storage objects
         storage_list = repo.get_storage_list()
         # Check if we got our data right.
         # The results are not sorted so we sort them for testing
         storage_name_list = [
             os.path.basename(storage.location)
             for storage in storage_list]
         self.assertEqual(
             sorted(storage_name_list), ["session1", "session2"])
Пример #6
0
    def create_with_state(cls, state, repo=None, legacy_mode=False):
        """
        Create a session manager by wrapping existing session state.

        This method populates the session storage with all of the well known
        directories (using :meth:`WellKnownDirsHelper.populate()`)

        :param stage:
            A pre-existing SessioState object.
        :param repo:
            If specified then this particular repository will be used to create
            the storage for this session. If left out, a new repository is
            constructed with the default location.
        :ptype repo:
            :class:`~plainbox.impl.session.storage.SessionStorageRepository`.
        :param legacy_mode:
            Propagated to
            :meth:`~plainbox.impl.session.storage.SessionStorage.create()`
            to ensure that legacy (single session) mode is used.
        :ptype legacy_mode:
            bool
        :return:
            fresh :class:`SessionManager` instance
        """
        logger.debug("SessionManager.create_with_state()")
        if repo is None:
            repo = SessionStorageRepository()
        storage = SessionStorage.create(repo.location, legacy_mode)
        WellKnownDirsHelper(storage).populate()
        return cls(state, storage)
Пример #7
0
    def get_throwaway_manager(cls, provider_list=None):
        """
        Create a temporary session manager.

        :param provider_list:
            (optional) A list of providers to put into the session manager. By
            default all known providers are added. You can use this argument to
            customize the behaviour beyond defaults.
        :returns:
            A new SessionManager object that will be destroyed when the context
            manager is left.

        This method can be used to create a throw-away session manager which is
        not really meant for running jobs but can be useful to access exporters
        and other objects stored in providers.
        """
        key = hash(frozenset(provider_list)) if provider_list else ''
        if not cls._throwaway_managers.get(key):
            # for safety let's create more persistent tempdir than
            # the TemporaryDirectory context_manager
            tmp = tempfile.mkdtemp()
            atexit.register(lambda: shutil.rmtree(tmp))
            repo = SessionStorageRepository(tmp)
            if provider_list is None:
                provider_list = get_providers()
            manager = cls.create(repo=repo)
            manager.add_local_device_context()
            device_context = manager.default_device_context
            cls._throwaway_managers[key] = manager
            for provider in provider_list:
                device_context.add_provider(provider)
        yield cls._throwaway_managers[key]
Пример #8
0
    def create_with_unit_list(cls, unit_list=None, repo=None):
        """
        Create a session manager with a fresh session.

        This method populates the session storage with all of the well known
        directories (using :meth:`WellKnownDirsHelper.populate()`)

        :param unit_list:
            If specified then this will be the initial list of units known by
            the session state object.
        :param repo:
            If specified then this particular repository will be used to create
            the storage for this session. If left out, a new repository is
            constructed with the default location.
        :ptype repo:
            :class:`~plainbox.impl.session.storage.SessionStorageRepository`.
        :return:
            fresh :class:`SessionManager` instance
        """
        logger.debug("SessionManager.create_with_unit_list()")
        if unit_list is None:
            unit_list = []
        state = SessionState(unit_list)
        if repo is None:
            repo = SessionStorageRepository()
        storage = SessionStorage.create(repo.location)
        context = SessionDeviceContext(state)
        WellKnownDirsHelper(storage).populate()
        return cls([context], storage)
Пример #9
0
    def create(cls, repo=None, prefix='pbox-'):
        """
        Create an empty session manager.

        This method creates an empty session manager. This is the most generic
        API that allows applications to freely work with any set of devices.

        Typically applications will use the :meth:`add_device_context()` method
        to add additional context objects at a later time. This method creates
        and populates the session storage with all of the well known
        directories (using :meth:`WellKnownDirsHelper.populate()`).

        :param repo:
            If specified then this particular repository will be used to create
            the storage for this session. If left out, a new repository is
            constructed with the default location.
        :ptype repo:
            :class:`~plainbox.impl.session.storage.SessionStorageRepository`.
        :return:
            fresh :class:`SessionManager` instance
        """
        logger.debug("SessionManager.create()")
        if repo is None:
            repo = SessionStorageRepository()
        storage = SessionStorage.create(repo.location, prefix)
        WellKnownDirsHelper(storage).populate()
        return cls([], storage)
Пример #10
0
 def _commit_resume(self):
     logger.debug("_commit_resume()")
     last_storage = SessionStorageRepository().get_last_storage()
     assert last_storage is not None, "no saved session to resume"
     self._manager = SessionManager.load_session(self.job_list,
                                                 last_storage,
                                                 lambda session: self)
     logger.debug("_commit_resume() finished")
Пример #11
0
 def previous_session_file(self):
     """
     Check the filesystem for previous session data
     Returns the full pathname to the session file if it exists
     """
     last_storage = SessionStorageRepository().get_last_storage()
     if last_storage and os.path.exists(last_storage.session_file):
         return last_storage.session_file
Пример #12
0
 def test_get_default_location_with_XDG_CACHE_HOME(self):
     """
     verify return value of get_default_location() when XDG_CACHE_HOME is
     set and HOME has any value.
     """
     env_patch = {'XDG_CACHE_HOME': 'XDG_CACHE_HOME'}
     with mock.patch.dict('os.environ', values=env_patch):
         measured = SessionStorageRepository.get_default_location()
         expected = "XDG_CACHE_HOME/plainbox/sessions"
         self.assertEqual(measured, expected)
Пример #13
0
 def test_get_default_location_with_HOME(self):
     """
     verify return value of get_default_location() when XDG_CACHE_HOME is
     not set but HOME is set
     """
     env_patch = {'HOME': 'HOME'}
     with mock.patch.dict('os.environ', values=env_patch, clear=True):
         measured = SessionStorageRepository.get_default_location()
         expected = "HOME/.cache/plainbox/sessions"
         self.assertEqual(measured, expected)
Пример #14
0
 def test_get_default_location_with_HOME(self):
     """
     verify return value of get_default_location() when XDG_CACHE_HOME is
     not set but HOME is set
     """
     env_patch = {'HOME': 'HOME'}
     with mock.patch.dict('os.environ', values=env_patch, clear=True):
         measured = SessionStorageRepository.get_default_location()
         expected = "HOME/.cache/plainbox/sessions"
         self.assertEqual(measured, expected)
Пример #15
0
 def test_get_default_location_with_XDG_CACHE_HOME(self):
     """
     verify return value of get_default_location() when XDG_CACHE_HOME is
     set and HOME has any value.
     """
     env_patch = {'XDG_CACHE_HOME': 'XDG_CACHE_HOME'}
     with mock.patch.dict('os.environ', values=env_patch):
         measured = SessionStorageRepository.get_default_location()
         expected = "XDG_CACHE_HOME/plainbox/sessions"
         self.assertEqual(measured, expected)
Пример #16
0
    def create_session(cls, job_list=None, repo=None, legacy_mode=False):
        """
        Create a session manager with a fresh session.

        This method populates the session storage with all of the well known
        directories (using :meth:`WellKnownDirsHelper.populate()`)

        :param job_list:
            If specified then this will be the initial list of jobs known
            by the session state object. This can be specified for convenience
            but is really optional since the application can always add more
            jobs to an existing session.
        :ptype job_list:
            list of :class:`~plainbox.abc.IJobDefinition`.
        :param repo:
            If specified then this particular repository will be used to create
            the storage for this session. If left out, a new repository is
            constructed with the default location.
        :ptype repo:
            :class:`~plainbox.impl.session.storage.SessionStorageRepository`.
        :param legacy_mode:
            Propagated to
            :meth:`~plainbox.impl.session.storage.SessionStorage.create()`
            to ensure that legacy (single session) mode is used.
        :ptype legacy_mode:
            bool
        :return:
            fresh :class:`SessionManager` instance
        """
        logger.debug("SessionManager.create_session()")
        if job_list is None:
            job_list = []
        state = SessionState(job_list)
        if repo is None:
            repo = SessionStorageRepository()
        storage = SessionStorage.create(repo.location, legacy_mode)
        WellKnownDirsHelper(storage).populate()
        return cls(state, storage)