xref: /aosp_15_r20/external/pytorch/tools/onnx/sarif/gen_sarif.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/bin/bash
2# Run this script inside its folder to generate the SARIF python object model files
3# from the SARIF schema.
4# e.g. ./gen_sarif.sh
5#
6# This script requires the jschema_to_python package to be installed.
7# To install it, run:
8#   pip install jschema_to_python
9
10set -e -x
11ROOT="${PWD}/../../.."
12SARIF_DIR="torch/onnx/_internal/diagnostics/infra/sarif"
13
14# SARIF version
15SARIF_VERSION="2.1.0"
16SARIF_SCHEMA_LINK="https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json"
17
18# Download SARIF schema
19tmp_dir="$(mktemp -d)"
20sarif_schema_file_path="${tmp_dir}/sarif-schema-${SARIF_VERSION}.json"
21curl -L -o "$sarif_schema_file_path" "$SARIF_SCHEMA_LINK"
22
23# TODO: A private branch of jschema_to_python was used to enable
24#       the generation to dataclasses and support annotation.
25python -m jschema_to_python \
26    --schema-path "$sarif_schema_file_path" \
27    --module-name torch.onnx._internal.diagnostics.infra.sarif \
28    --output-directory "${ROOT}/${SARIF_DIR}" \
29    --root-class-name SarifLog \
30    --hints-file-path code-gen-hints.json \
31    --force \
32    --library dataclasses \
33    -vv
34
35# Generate SARIF version file
36echo "from typing import Final" > "${ROOT}/${SARIF_DIR}/version.py"
37echo "SARIF_VERSION: Final = \"${SARIF_VERSION}\"" >> "${ROOT}/${SARIF_DIR}/version.py"
38echo "SARIF_SCHEMA_LINK: Final = \"${SARIF_SCHEMA_LINK}\"" >> "${ROOT}/${SARIF_DIR}/version.py"
39
40pushd "$ROOT"
41(
42    # Hack to have flake8 not complain about generated code.
43    set +x
44    while IFS= read -r -d '' file; do
45        echo "# flake8: noqa" >> "$file"
46    done < <(find "$SARIF_DIR" -name '*.py' -print0)
47    set -x
48
49    lintrunner "${SARIF_DIR}/"** -a
50)
51popd
52