-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add manifest file parsing (#3105) * Add `-m/--manifest` flag to accept manifest.yml produced by `epicli init/prepare` * Add `-v/--verbose` mode for printing out parsed manifest data * Add ManifestReader class used for paring the manifest.yml file * Move src/command/*.py to debian/redhat subdirs where needed
- Loading branch information
Showing
27 changed files
with
369 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
download_requirements_dir: "/var/tmp/epi-download-requirements" | ||
download_requirements_script: "{{ download_requirements_dir }}/download-requirements.py" | ||
download_requirements_flag: "{{ download_requirements_dir }}/download-requirements-done.flag" | ||
download_requirements_manifest: "{{ download_requirements_dir }}/manifest.yml" | ||
download_requirements_script: "{{ download_requirements_dir }}/download-requirements.py" |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions
16
ansible/playbooks/roles/repository/files/download-requirements/src/command/toolchain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
ansible/playbooks/roles/repository/files/download-requirements/src/config/manifest_reader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from pathlib import Path | ||
from typing import Any, Callable, Dict, List, Set | ||
|
||
import yaml | ||
|
||
from src.error import CriticalError | ||
|
||
|
||
def load_yaml_file_all(filename: Path) -> List[Any]: | ||
try: | ||
with open(filename, encoding="utf-8") as req_handler: | ||
return list(yaml.safe_load_all(req_handler)) | ||
except yaml.YAMLError as yaml_err: | ||
raise CriticalError(f'Failed loading: `{yaml_err}`') from yaml_err | ||
except Exception as err: | ||
raise CriticalError(f'Failed loading: `{filename}`') from err | ||
|
||
|
||
def load_yaml_file(filename: Path) -> Any: | ||
return load_yaml_file_all(filename)[0] | ||
|
||
|
||
class ManifestReader: | ||
""" | ||
Load the manifest file and call defined parser methods to process required documents. | ||
Main running method is :func:`~manifest_reader.ManifestReader.parse_manifest` which returns formatted manifest output. | ||
""" | ||
|
||
def __init__(self, dest_manifest: Path): | ||
self.__dest_manifest = dest_manifest | ||
self.__detected_components: Set = set() | ||
self.__detected_features: Set = set() | ||
|
||
def __parse_cluster_info(self, cluster_doc: Dict): | ||
""" | ||
Parse `epiphany-cluster` document and extract only used components. | ||
:param cluster_doc: handler to a `epiphany-cluster` document | ||
""" | ||
components = cluster_doc['specification']['components'] | ||
for component in components: | ||
if components[component]['count'] > 0: | ||
self.__detected_components.add(component) | ||
|
||
def __parse_feature_mappings_info(self, feature_mappings_doc: Dict): | ||
""" | ||
Parse `configuration/feature-mappings` document and extract only used features (based on `epiphany-cluster` doc). | ||
:param feature_mappings_doc: handler to a `configuration/feature-mappings` document | ||
""" | ||
mappings = feature_mappings_doc['specification']['mappings'] | ||
for mapping in mappings.keys() & self.__detected_components: | ||
for feature in mappings[mapping]: | ||
self.__detected_features.add(feature) | ||
|
||
def parse_manifest(self) -> Dict[str, Any]: | ||
""" | ||
Load the manifest file, call parsers on required docs and return formatted output. | ||
""" | ||
parse_doc: Dict[str, Callable] = { | ||
'epiphany-cluster': self.__parse_cluster_info, | ||
'configuration/feature-mappings': self.__parse_feature_mappings_info | ||
} | ||
|
||
parsed_docs: Set[str] = set() | ||
for manifest_doc in load_yaml_file_all(self.__dest_manifest): | ||
try: | ||
kind: str = manifest_doc['kind'] | ||
parse_doc[kind](manifest_doc) | ||
parsed_docs.add(kind) | ||
except KeyError: | ||
pass | ||
|
||
if len(parsed_docs) != len(parse_doc.keys()): | ||
raise CriticalError(f'ManifestReader - could not find documents: {parsed_docs ^ parse_doc.keys()}') | ||
|
||
return {'detected-components': sorted(list(self.__detected_components)), | ||
'detected-features': sorted(list(self.__detected_features))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ad-requirements/tests/command/test_apt.py → ...irements/tests/command/debian/test_apt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uirements/tests/command/test_apt_cache.py → ...ts/tests/command/debian/test_apt_cache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ad-requirements/tests/command/test_dnf.py → ...irements/tests/command/redhat/test_dnf.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../tests/command/test_dnf_config_manager.py → ...command/redhat/test_dnf_config_manager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ments/tests/command/test_dnf_repoquery.py → ...ests/command/redhat/test_dnf_repoquery.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ad-requirements/tests/command/test_rpm.py → ...irements/tests/command/redhat/test_rpm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
ansible/playbooks/roles/repository/files/download-requirements/tests/config/test_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
from src.config.config import Config | ||
from tests.data.config import EXPECTED_VERBOSE_OUTPUT | ||
from tests.data.manifest_reader import INPUT_MANIFEST_FEATURE_MAPPINGS | ||
|
||
|
||
def test_manifest_verbose_output(mocker, caplog): | ||
''' Check output produced when running download-requirements script with the `-v|--verbose` flag and with provided `-m|--manifest` ''' | ||
|
||
mocker.patch('src.config.manifest_reader.load_yaml_file_all', return_value=yaml.safe_load_all(INPUT_MANIFEST_FEATURE_MAPPINGS)) | ||
caplog.set_level(logging.INFO) | ||
|
||
# mock Config's init methods: | ||
Config._Config__add_args = lambda *args: None | ||
Config._Config__log_info_summary = lambda *args: None | ||
|
||
config = Config([]) | ||
|
||
# mock required config data: | ||
config.dest_manifest = Path('/some/path') | ||
config.verbose_mode = True | ||
config.read_manifest({}) | ||
|
||
log_output = f'\n{"".join(caplog.messages)}\n' | ||
|
||
assert log_output == EXPECTED_VERBOSE_OUTPUT |
13 changes: 13 additions & 0 deletions
13
...aybooks/roles/repository/files/download-requirements/tests/config/test_manifest_reader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
from src.config.manifest_reader import ManifestReader | ||
from tests.data.manifest_reader import EXPECTED_FEATURE_MAPPINGS, INPUT_MANIFEST_FEATURE_MAPPINGS | ||
|
||
def test_parse_manifest(mocker): | ||
''' Check manifest file parsing ''' | ||
mocker.patch('src.config.manifest_reader.load_yaml_file_all', return_value=yaml.safe_load_all(INPUT_MANIFEST_FEATURE_MAPPINGS)) | ||
|
||
mreader = ManifestReader(Path('/some/path')) | ||
assert mreader.parse_manifest() == EXPECTED_FEATURE_MAPPINGS |
Oops, something went wrong.