Add-on-template/entrypoint.py

89 lines
3.1 KiB
Python

#!/usr/bin/env python3
import argparse
import os
import sys
import json
from allspice import AllSpice
def hello_world(task_type, source_file, output_file, additional_params):
print(f"Hello, World! Performing task: {task_type}")
print(f"Source file: {source_file}")
print(f"Output file: {output_file}")
print(f"Additional parameters: {additional_params}")
# Parse additional_params
params = json.loads(additional_params)
sch_ver = params.get("SCH_VER", "3")
print(f"Schematic version: {sch_ver}")
def test_allspice_connection(allspice_hub_url, auth_token):
try:
allspice = AllSpice(token_text=auth_token, allspice_hub_url=allspice_hub_url)
print("AllSpice Version: " + allspice.get_version())
print("API-Token belongs to user: " + allspice.get_user().username)
except Exception as e:
print(f"Error connecting to AllSpice API: {e}")
sys.exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="hardware_devops_action",
description="Perform hardware development tasks such as Schematic-Review, PCB-Review, ECO-Review, and Release.",
)
parser.add_argument(
"--source_file",
help="The path to the source file used for the task. Example: 'Archimajor.PrjPcb', 'Schematics/Beagleplay.dsn'.",
)
parser.add_argument(
"--task_type",
help="The type of hardware task to perform. Options include 'Schematic-Review', 'PCB-Review', 'ECO-Review', 'Release'.",
default="Schematic-Review",
)
parser.add_argument(
"--source_ref",
help="The git reference the task should be performed for (eg. branch name, tag name, commit SHA).",
default="main",
)
parser.add_argument(
"--server_url",
help="The URL of your GitHub server instance.",
)
parser.add_argument(
"--output_file",
help="The path to the output file. If absent, the output will be printed to the command line.",
)
parser.add_argument(
"--additional_params",
help="Any additional parameters required for the task, provided as a JSON string.",
default="{}",
)
parser.add_argument(
"--allspice_token",
help="Your AllSpice application token. Generate a token: https://hub.allspice.io/user/settings/applications",
)
parser.add_argument("--config_file", help="Path to config file")
parser.add_argument("--input_file", help="path/to/input_file")
args = parser.parse_args()
auth_token = os.environ.get("ALLSPICE_TOKEN") or args.allspice_token
if auth_token is None:
print(
"Please set the environment variable ALLSPICE_TOKEN or supply a token with --allspice_token <your_token>. Generate a token: https://hub.allspice.io/user/settings/applications"
)
exit(1)
# Test connection to AllSpice API
test_allspice_connection(args.server_url, auth_token)
# Perform the Hello World task
hello_world(
task_type=args.task_type,
source_file=args.source_file,
output_file=args.output_file,
additional_params=args.additional_params,
)