Skip to content

Commit

Permalink
Added metadata value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rashidnhm committed Aug 31, 2023
1 parent 69af812 commit fbb5602
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 34 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/build-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ on:
type: boolean
default: false

# TODO: This is temp workaround to add metadata previewImages validation to this job. Once the static asset files
# are centralized properly, then this parameter can be removed.
validate_metadata_preview_images:
description: Validate the metadata previewImages after sphinx-build concludes
required: false
type: boolean
default: false

jobs:
validate-inputs:
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -424,6 +432,25 @@ jobs:
cat /tmp/execution_times/execution_times.json | jq
# TODO: This is a temporary step to include metadata validation as a part of the build step.
# One the assets in QML are centralized with proper naming convention, this step can be removed
# and the validation can be done with `validate-demo-metadata.yml` workflow.
- name: Validate metadata previewImages
run: |
metadata_files=()
input_demos_to_build=$(jq -r '[.[].name]|join(" ")' ${{ steps.worker_tasks.outputs.file_name }})
for metadata_file in $input_demos_to_build
do
metadata_files+=("demonstrations/${metadata_file%.py}.metadata.json")
done
metadata_files_str="${metadata_files[@]}"
qml_pipeline_utils validate-metadata-preview-images \
--metadata-files $metadata_files_str \
--build-dir="${{ github.workspace }}/_build/html"
- name: Upload Execution Times
uses: actions/upload-artifact@v3
with:
Expand Down
36 changes: 35 additions & 1 deletion .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ jobs:
done
echo "files=${demo_filenames[@]}" >> $GITHUB_OUTPUT
- name: Detect Metadata Files that need to be changed
id: metadata_files_to_check
env:
DEMOS_TO_BUILD: ${{ steps.formatted_changed_demos.outputs.files }}
run: |
metadata_files=()
input_demos_to_build="$DEMOS_TO_BUILD"
if [ -z "$input_demos_to_build" ]; then
readarray -d '' metadata_files < <(find demonstrations -name "*.metadata.json" -not -path "demonstrations/demonstrations_categories.metadata.json" -print0)
else
for metadata_file in $input_demos_to_build
do
metadata_files+=("demonstrations/${metadata_file%.py}.metadata.json")
done
fi
metadata_files_str="${metadata_files[@]}"
echo "file_names=$metadata_files_str" >> $GITHUB_OUTPUT
- name: Set number of workers needed
id: num_workers
env:
Expand All @@ -81,12 +102,25 @@ jobs:
num_workers: ${{ steps.num_workers.outputs.num_workers }}
build_all_demos: ${{ steps.build_all_demos.outputs.build_all_demos }}
demos_to_build: ${{ steps.formatted_changed_demos.outputs.files }}
metadata_file_names: ${{ steps.metadata_files_to_check.outputs.file_names }}

validate_metadata:
uses: ./.github/workflows/validate-demo-metadata.yml

needs:
- set_build_params

with:
branch: ${{ github.ref }}
metadata_files: ${{ needs.set_build_params.outputs.metadata_file_names }}
skip_metadata_preview_image_value_validation: true # TODO: This can be reverted once the static assets are centralized

build:
uses: ./.github/workflows/build-branch.yml

needs:
- set_build_params
- validate_metadata

# Only run if we need to build all the demos or a subset of demos.
# If build_all_demos is false AND demos_to_build_is blank, this means
Expand All @@ -111,12 +145,12 @@ jobs:
sphinx_build_output_format: html
sphinx_examples_to_build: ${{ needs.set_build_params.outputs.demos_to_build }}


save-build-context:
runs-on: ubuntu-22.04

needs:
- build
- validate_metadata

steps:
- name: Save Pull Request Event Context
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/qml_pipeline_utils/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "qml_pipeline_utils"
version = "1"
requires-python = ">= 3.7"
dependencies = [
'tqdm~=4.66.0'
]

[project.scripts]
qml_pipeline_utils = "qml_pipeline_utils.cli:cli_parser"
22 changes: 22 additions & 0 deletions .github/workflows/qml_pipeline_utils/qml_pipeline_utils/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import argparse
import os
from pathlib import Path

import qml_pipeline_utils.services
Expand Down Expand Up @@ -153,6 +154,20 @@ def cli_parser():
"glob-pattern",
)

subparsers_validate_metadata_preview_images = subparsers.add_parser(
"validate-metadata-preview-images",
description="Validate the metadata"
)
add_flags_to_subparser(
subparsers_validate_metadata_preview_images,
"build-dir"
)
subparsers_validate_metadata_preview_images.add_argument(
"--metadata-files",
help="List of paths to metadata files",
nargs="+"
)

parser_results = parser.parse_args()

cli_actions = {
Expand Down Expand Up @@ -225,6 +240,13 @@ def cli_parser():
"sphinx_build_type": getattr(parser_results, "build_type", ""),
},
},
"validate-metadata-preview-images": {
"func": qml_pipeline_utils.services.validate_metadata_preview_images,
"kwargs": {
"metadata_files": getattr(parser_results, "metadata_files", []),
"sphinx_build_directory": Path(getattr(parser_results, "build_dir", ""))
}
}
}

if parser_results.action not in cli_actions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .clean_sitemap import clean_sitemap
from .show_worker_files import show_worker_files
from .parse_execution_times import parse_execution_times
from .validate_metadata_preview_images import validate_metadata_preview_images
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import json
from pathlib import Path
from typing import List, Union, TYPE_CHECKING

from tqdm import tqdm

if TYPE_CHECKING:
from pathlib import Path


def validate_metadata_preview_images(metadata_files: List[str], sphinx_build_directory: Path):
working_dir = Path(os.getcwd())

sphinx_build_directory = working_dir / sphinx_build_directory \
if not sphinx_build_directory.is_absolute() \
else sphinx_build_directory

for metadata_file in metadata_files:
if not metadata_file.startswith("/"):
metadata_file_path = Path(working_dir) / metadata_file
else:
metadata_file_path = Path(metadata_file)

with metadata_file_path.open() as fh:
metadata = json.load(fh)

preview_images = metadata.get("previewImages", [])

image_uris = [
image["uri"]
for image in preview_images
]

for image_uri in tqdm(image_uris, ascii=True, desc=metadata_file):
if not image_uri.startswith("/"):
print(f" Metadata file {metadata_file} contains either remote image uri or non-absolute path "
f"for previewImage {image_uri}. Remote image paths cannot be validated at this time. Skipping.")
continue

uri_path = sphinx_build_directory / image_uri[1:]

assert uri_path.exists(), f"Could not find previewImage {image_uri}"

return None



14 changes: 0 additions & 14 deletions .github/workflows/qml_pipeline_utils/setup.py

This file was deleted.

Loading

0 comments on commit fbb5602

Please sign in to comment.