Files
Daniel Lindmark 40f1d80b98 Pair down example
2025-07-06 19:18:46 -05:00

128 lines
4.2 KiB
Python
Executable File

#! /usr/bin/env python3
# Generate a BOM from a PrjPcb/DSN/SDAX file.
# For more information, read the README file in this directory.
import argparse
import csv
import logging
import os
import yaml
import sys
from contextlib import ExitStack
from allspice import AllSpice
from allspice.utils.bom_generation import generate_bom, ColumnConfig
logger = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logger.addHandler(handler)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="generate_bom", description="Generate a BOM from a project repository."
)
parser.add_argument(
"repository", help="The repo containing the project in the form 'owner/repo'"
)
parser.add_argument(
"source_file",
help=(
"The path to the source file used to generate the BOM. This should be "
"a .PrjPcb file for Altium projects, a .DSN file for OrCAD projects, "
"or a .SDAX file for System Capture projects."
"Example: 'Archimajor.PrjPcb', 'Schematics/Beagleplay.dsn'."
),
)
parser.add_argument(
"--columns",
help=(
"A path to a YAML file mapping columns to the attributes they are from. See the README "
"for more details. Defaults to 'columns.yml'."
),
default="columns.yml",
)
parser.add_argument(
"--source_ref",
help=(
"The git reference the BOM should be generated for (eg. branch name, tag name, commit "
"SHA). Defaults to the main branch."
),
default="main",
)
parser.add_argument(
"--allspice_hub_url",
help="The URL of your AllSpice Hub instance. Defaults to https://hub.allspice.io.",
)
parser.add_argument(
"--output_file",
help="The path to the output file. If absent, the CSV will be output to the command line.",
)
parser.add_argument(
"--log-level",
help="The log level for the logger. Defaults to INFO.",
default="INFO",
)
args = parser.parse_args()
logger.setLevel(args.log_level.upper())
logger.info("Running generate-bom action.")
logger.debug("Arguments: %s", vars(args))
columns_file = args.columns
columns = {}
design_reuse_repos = []
try:
with open(columns_file, "r") as f:
columns_data = yaml.safe_load(f.read())
logger.info("Loaded columns configuration from %s", columns_file)
if not isinstance(columns_data, dict):
raise ValueError("Columns configuration must be a dictionary.")
logger.info("listing columns_data: %s", columns_data)
except KeyError as e:
logger.critical(
"Error: columns file %s does not seem to be in the right format.",
columns_file,
)
logger.critical("Please refer to the README for more information.")
logger.critical("Caused by", exc_info=e)
sys.exit(1)
auth_token = os.environ.get("ALLSPICE_AUTH_TOKEN")
if auth_token is None:
logger.critical("Please set the environment variable ALLSPICE_AUTH_TOKEN")
exit(1)
if args.allspice_hub_url is None:
allspice = AllSpice(token_text=auth_token, log_level=args.log_level.upper())
else:
allspice = AllSpice(
token_text=auth_token,
allspice_hub_url=args.allspice_hub_url,
log_level=args.log_level.upper(),
)
allspice.logger.addHandler(handler)
repo_owner, repo_name = args.repository.split("/")
repository = allspice.get_repository(repo_owner, repo_name)
logger.info("Testing AllSpice API connection..." + allspice.get_version())
# Your Action code starts here
logger.info("My first custom action is running!")
# Write test file to upload as artifact
with open(args.output_file, mode='w', newline='') as file:
writer = csv.writer(file)
# Write the header row
writer.writerow(['Header_row', 'Column_1', 'Column_2'])
# Your Action code starts here
logger.info("Saved test output to output.csv")