Exemple #1
0
    def load(self, resolve=True, stage=None, skip_actors_discovery=False):
        """
        Loads the repository resources

        :param resolve: Decides whether or not to perform the resolving of model references
        :type resolve: bool
        :param stage: Stage to load - Required for repository managers
        :type stage: _LoadStage value
        """
        if not stage or stage is _LoadStage.INITIAL:
            self.log.debug("Loading repository %s", self.name)
            self.log.debug("Loading tag modules")
            self._load_modules(self.tags, 'leapp.tags')
            self.log.debug("Loading topic modules")
            self._load_modules(self.topics, 'leapp.topics')

        if not stage or stage is _LoadStage.MODELS:
            self.log.debug("Loading model modules")
            self._load_modules(self.models, 'leapp.models')
            if resolve:
                resolve_model_references()

        if not stage or stage is _LoadStage.LIBRARIES:
            self.log.debug("Extending PATH for common tool paths")
            self._extend_environ_paths('PATH', self.tools)
            self.log.debug(
                "Extending LEAPP_COMMON_TOOLS for common tool paths")
            self._extend_environ_paths('LEAPP_COMMON_TOOLS', self.tools)
            self.log.debug(
                "Extending LEAPP_COMMON_FILES for common file paths")
            self._extend_environ_paths('LEAPP_COMMON_FILES', self.files)
            self.log.debug(
                "Installing repository provided common libraries loader hook")
            sys.meta_path.append(
                LeappLibrariesFinder(module_prefix='leapp.libraries.common',
                                     paths=self.libraries))
            sys.meta_path.append(
                LeappLibrariesFinder(module_prefix='leapp.workflows.api',
                                     paths=self.apis))

        if not skip_actors_discovery:
            if not stage or stage is _LoadStage.ACTORS:
                self.log.debug("Running actor discovery")
                for actor in self.actors:
                    actor.discover()

        if not stage or stage is _LoadStage.WORKFLOWS:
            self.log.debug("Loading workflow modules")
            self._load_modules(self.workflows, 'leapp.workflows')
Exemple #2
0
    def injected_context(self):
        """
        Prepares the actor environment for running the actor.
        This includes injecting actor private libraries into :py:mod:`leapp.libraries.actor`
        and setting environment variables for private tools and files.

        :note: Use with caution.
        """
        # Backup of the path variable
        path_backup = os.environ.get('PATH', '')
        os.environ['PATH'] = ':'.join(
            path_backup.split(':') + list(
                os.path.join(self._repo_dir, self._directory, path)
                for path in self.tools))

        files_backup = os.environ.get('LEAPP_FILES', None)
        if self.files:
            os.environ['LEAPP_FILES'] = os.path.join(self._repo_dir,
                                                     self._directory,
                                                     self.files[0])

        tools_backup = os.environ.get('LEAPP_TOOLS', None)
        if self.tools:
            os.environ['LEAPP_TOOLS'] = os.path.join(self._repo_dir,
                                                     self._directory,
                                                     self.tools[0])

        sys.meta_path.append(
            LeappLibrariesFinder(module_prefix='leapp.libraries.actor',
                                 paths=[
                                     os.path.join(self._repo_dir,
                                                  self.directory, x)
                                     for x in self.libraries
                                 ]))

        previous_path = os.getcwd()
        os.chdir(os.path.join(self._repo_dir, self._directory))
        try:
            yield
        finally:
            os.chdir(previous_path)

            # Restoration of the PATH environment variable
            os.environ['PATH'] = path_backup
            # Restoration of the LEAPP_FILES environment variable
            if files_backup is not None:
                os.environ['LEAPP_FILES'] = files_backup
            else:
                os.environ.pop('LEAPP_FILES', None)

            if tools_backup is not None:
                os.environ['LEAPP_TOOLS'] = tools_backup
            else:
                os.environ.pop('LEAPP_TOOLS', None)