Skip to content

Commit

Permalink
Try to make configs available for actors to import.
Browse files Browse the repository at this point in the history
* Add comment explaining why configs is not present at the repository level.
* Modify sys.path to find the configs in actor_definition
* Fix actor config import of abc's for Python-2.7
  • Loading branch information
abadger committed Sep 19, 2024
1 parent 8011625 commit bf68773
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
8 changes: 7 additions & 1 deletion leapp/actors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import logging
import os
import sys
from collections.abc import Sequence

try:
# Python 3.3+
from collections.abc import Sequence
except ImportError:
# Python 2.6 through 3.2
from collections import Sequence

from leapp.actors.config import Config, retrieve_config
from leapp.compat import string_types
Expand Down
Empty file added leapp/configs/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions leapp/configs/actor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
:py:mod:`leapp.configs.actor` represents the import location for private actor config schema that
are placed in the actor's configs folder.
Example:
If your actor has a configs folder with a schemas.py python module, import it
from the actor like this::
from leapp.libraries.actor import schemas
This directory is intended for the definitions of actor configuration fields.
"""
21 changes: 20 additions & 1 deletion leapp/repository/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def load(self, resolve=True, stage=None, skip_actors_discovery=False):
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))
sys.meta_path.append(LeappLibrariesFinder(module_prefix='leapp.configs', paths=self.configs))

if not skip_actors_discovery:
if not stage or stage is _LoadStage.ACTORS:
Expand Down Expand Up @@ -189,6 +191,15 @@ def mapped_actor_data(data):
})
return data

# Note: `configs` are not present here because we are not yet making
# them globally accessible. This is to force people to copy the config
# schema to their Actors instead of importing them from other Actors.
# That copy, in turn, is a good idea so the framework can return an
# error if two Actors share the same config but they have different
# schemafor it (for instance, if Actor Foo and Bar were sharing the
# same config entry but Actor Foo updated the entry in a later version.
# We need to error so Actor Bar can either be ported to the new
# definition or use a different config entry for it's needs.)
return {
'repo_dir': self._repo_dir,
'actors': [mapped_actor_data(a.serialize()) for a in self.actors],
Expand All @@ -199,7 +210,8 @@ def mapped_actor_data(data):
'workflows': filtered_serialization(get_workflows, self.workflows),
'tools': [dict([('path', path)]) for path in self.relative_paths(self.tools)],
'files': [dict([('path', path)]) for path in self.relative_paths(self.files)],
'libraries': [dict([('path', path)]) for path in self.relative_paths(self.libraries)]
'libraries': [dict([('path', path)]) for path in self.relative_paths(self.libraries)],
'configs': [dict([('path', path)]) for path in self.relative_paths(self.configs)],
}

def _extend_environ_paths(self, name, paths):
Expand Down Expand Up @@ -268,6 +280,13 @@ def libraries(self):
"""
return tuple(self._definitions.get(DefinitionKind.LIBRARIES, ()))

@property
def configs(self):
"""
:return: Tuple of configs in the repository
"""
return tuple(self._definitions.get(DefinitionKind.CONFIGS, ()))

@property
def files(self):
"""
Expand Down
5 changes: 5 additions & 0 deletions leapp/repository/actor_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ def injected_context(self):
module_prefix='leapp.libraries.actor',
paths=[os.path.join(self._repo_dir, self.directory, x) for x in self.libraries]))

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

previous_path = os.getcwd()
os.chdir(os.path.join(self._repo_dir, self._directory))
try:
Expand Down
16 changes: 16 additions & 0 deletions leapp/repository/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def scan(repository, path):
('workflows', scan_workflows),
('files', scan_files),
('libraries', scan_libraries),
('configs', scan_configs),
('tests', scan_tests),
('tools', scan_tools),
('apis', scan_apis))
Expand Down Expand Up @@ -224,6 +225,21 @@ def scan_libraries(repo, path, repo_path):
repo.add(DefinitionKind.LIBRARIES, os.path.relpath(path, repo_path))


def scan_configs(repo, path, repo_path):
"""
Scans configs and adds them to the repository.
:param repo: Instance of the repository
:type repo: :py:class:`leapp.repository.Repository`
:param path: path to the configs
:type path: str
:param repo_path: path to the repository
:type repo_path: str
"""
if os.listdir(path):
repo.add(DefinitionKind.CONFIGS, os.path.relpath(path, repo_path))


def scan_tools(repo, path, repo_path):
"""
Scans tools and adds them to the repository.
Expand Down

0 comments on commit bf68773

Please sign in to comment.