-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[uss_qualifier] Separate artifact-making from main.py (#321)
* Separate artifact-making from main.py * Fix shell lint * Add multiple-config documentation per comments
- Loading branch information
1 parent
079c92b
commit 1973813
Showing
5 changed files
with
207 additions
and
112 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!env/bin/python3 | ||
|
||
import argparse | ||
import os | ||
import sys | ||
|
||
from implicitdict import ImplicitDict | ||
from loguru import logger | ||
|
||
from monitoring.uss_qualifier.configurations.configuration import ( | ||
USSQualifierConfiguration, | ||
USSQualifierConfigurationV1, | ||
) | ||
from monitoring.uss_qualifier.fileio import load_dict_with_references | ||
from monitoring.uss_qualifier.reports.artifacts import generate_artifacts | ||
from monitoring.uss_qualifier.reports.report import TestRunReport, redact_access_tokens | ||
|
||
|
||
def parseArgs() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser( | ||
description="Generate artifacts from USS Qualifier report" | ||
) | ||
|
||
parser.add_argument( | ||
"--config", | ||
help="Configuration string according to monitoring/uss_qualifier/configurations/README.md; Several comma-separated strings may be specified", | ||
required=True, | ||
) | ||
|
||
parser.add_argument( | ||
"--report", | ||
help="File name of the report to read; Several comma-separated file names matching the configurations may be specified", | ||
required=True, | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def main() -> int: | ||
args = parseArgs() | ||
|
||
config_names = str(args.config).split(",") | ||
|
||
report_paths = str(args.report).split(",") | ||
if len(report_paths) != len(config_names): | ||
raise ValueError( | ||
f"Need matching number of report, expected {len(config_names)}, got {len(report_paths)}" | ||
) | ||
|
||
for idx, config_name in enumerate(config_names): | ||
logger.info( | ||
f"========== Generating artifacts for configuration {config_name} ==========" | ||
) | ||
|
||
config_src = load_dict_with_references(config_name) | ||
whole_config = ImplicitDict.parse(config_src, USSQualifierConfiguration) | ||
|
||
report_src = load_dict_with_references(report_paths[idx]) | ||
report = ImplicitDict.parse(report_src, TestRunReport) | ||
|
||
config: USSQualifierConfigurationV1 = whole_config.v1 | ||
if config.artifacts: | ||
generate_artifacts(report, config.artifacts) | ||
else: | ||
logger.warning(f"No artifacts to generate for {config_name}") | ||
|
||
logger.info( | ||
f"========== Completed generating artifacts for configuration {config_name} ==========" | ||
) | ||
|
||
return os.EX_OK | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
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,51 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eo pipefail | ||
|
||
if [[ $# -lt 2 ]]; then | ||
echo "Usage: $0 <CONFIG_NAME(s)> <REPORT_NAME(s)>" | ||
echo "Generates artifacts according to the specified configuration(s) using the specified report(s)" | ||
echo "<CONFIG_NAME>: Location of the configuration file (or multiple locations separated by commas)." | ||
echo "<REPORT_NAME>: Location of the report file (or multiple locations separated by commas)." | ||
exit 1 | ||
fi | ||
|
||
# Find and change to repo root directory | ||
OS=$(uname) | ||
if [[ "$OS" == "Darwin" ]]; then | ||
# OSX uses BSD readlink | ||
BASEDIR="$(dirname "$0")" | ||
else | ||
BASEDIR=$(readlink -e "$(dirname "$0")") | ||
fi | ||
cd "${BASEDIR}/../.." || exit 1 | ||
|
||
( | ||
cd monitoring || exit 1 | ||
make image | ||
) | ||
|
||
CONFIG_NAME="${1}" | ||
|
||
REPORT_NAME="${2}" | ||
|
||
echo "Generating artifacts from configuration(s): ${CONFIG_NAME}" | ||
echo "Reading report(s) from: ${REPORT_NAME}" | ||
|
||
OUTPUT_DIR="monitoring/uss_qualifier/output" | ||
mkdir -p "$OUTPUT_DIR" | ||
|
||
CACHE_DIR="monitoring/uss_qualifier/.templates_cache" | ||
mkdir -p "$CACHE_DIR" | ||
|
||
# shellcheck disable=SC2086 | ||
docker run --name uss_qualifier \ | ||
--rm \ | ||
-u "$(id -u):$(id -g)" \ | ||
-e PYTHONBUFFERED=1 \ | ||
-e MONITORING_GITHUB_ROOT=${MONITORING_GITHUB_ROOT:-} \ | ||
-v "$(pwd)/$OUTPUT_DIR:/app/$OUTPUT_DIR" \ | ||
-v "$(pwd)/$CACHE_DIR:/app/$CACHE_DIR" \ | ||
-w /app/monitoring/uss_qualifier \ | ||
interuss/monitoring \ | ||
python make_artifacts.py --config "$CONFIG_NAME" --report "$REPORT_NAME" |
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,71 @@ | ||
import json | ||
import os | ||
|
||
from loguru import logger | ||
|
||
from implicitdict import ImplicitDict | ||
from monitoring.uss_qualifier.configurations.configuration import ArtifactsConfiguration | ||
from monitoring.uss_qualifier.reports.documents import make_report_html | ||
from monitoring.uss_qualifier.reports.report import TestRunReport, redact_access_tokens | ||
from monitoring.uss_qualifier.reports.sequence_view import generate_sequence_view | ||
from monitoring.uss_qualifier.reports.templates import render_templates | ||
from monitoring.uss_qualifier.reports.tested_requirements import ( | ||
generate_tested_requirements, | ||
) | ||
|
||
|
||
def generate_artifacts(report: TestRunReport, artifacts: ArtifactsConfiguration): | ||
os.makedirs(artifacts.output_path, exist_ok=True) | ||
|
||
def _should_redact(cfg) -> bool: | ||
return "redact_access_tokens" in cfg and cfg.redact_access_tokens | ||
|
||
logger.info(f"Redacting access tokens from report") | ||
redacted_report = ImplicitDict.parse(json.loads(json.dumps(report)), TestRunReport) | ||
redact_access_tokens(redacted_report) | ||
|
||
if artifacts.raw_report: | ||
# Raw report | ||
path = os.path.join(artifacts.output_path, "report.json") | ||
logger.info(f"Writing raw report to {path}") | ||
raw_report = artifacts.raw_report | ||
report_to_write = redacted_report if _should_redact(raw_report) else report | ||
with open(path, "w") as f: | ||
if "indent" in raw_report and raw_report.indent is not None: | ||
json.dump(report_to_write, f, indent=raw_report.indent) | ||
else: | ||
json.dump(report_to_write, f) | ||
|
||
if artifacts.report_html: | ||
# HTML rendering of raw report | ||
path = os.path.join(artifacts.output_path, "report.html") | ||
logger.info(f"Writing HTML report to {path}") | ||
report_to_write = ( | ||
redacted_report if _should_redact(artifacts.report_html) else report | ||
) | ||
with open(path, "w") as f: | ||
f.write(make_report_html(report_to_write)) | ||
|
||
if artifacts.templated_reports: | ||
# Templated reports | ||
render_templates( | ||
artifacts.output_path, | ||
artifacts.templated_reports, | ||
redacted_report, | ||
) | ||
|
||
if artifacts.tested_requirements: | ||
# Tested requirements view | ||
for tested_reqs_config in artifacts.tested_requirements: | ||
path = os.path.join(artifacts.output_path, tested_reqs_config.report_name) | ||
logger.info(f"Writing tested requirements view to {path}") | ||
generate_tested_requirements(redacted_report, tested_reqs_config, path) | ||
|
||
if artifacts.sequence_view: | ||
# Sequence view | ||
path = os.path.join(artifacts.output_path, "sequence") | ||
logger.info(f"Writing sequence view to {path}") | ||
report_to_write = ( | ||
redacted_report if _should_redact(artifacts.sequence_view) else report | ||
) | ||
generate_sequence_view(report_to_write, artifacts.sequence_view, path) |