Skip to content

Commit

Permalink
feat: use ConfigArgParse instead of ArgParse for env variable usability
Browse files Browse the repository at this point in the history
  • Loading branch information
FHeilmann committed Dec 1, 2023
1 parent 60925f5 commit 02e571e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 65 deletions.
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tweaker3 = {git = "https://github.com/ChristophSchranz/Tweaker-3.git"}
pyyaml = "^6.0.1"
gitpython = "^3.1.40"
requests = "^2.31.0"
configargparse = "^1.7"


[tool.poetry.group.dev.dependencies]
Expand Down Expand Up @@ -54,6 +55,7 @@ module = [
"admesh.*",
"tweaker3.*",
"imagekitio.*",
"configargparse.*",
]
ignore_missing_imports = true

Expand Down
67 changes: 55 additions & 12 deletions voron_ci/tools/imagekit_uploader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import argparse
import os
import sys
import tempfile
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import TYPE_CHECKING, Self

import configargparse
from imagekitio import ImageKit
from imagekitio.models.UploadFileRequestOptions import UploadFileRequestOptions

Expand All @@ -19,20 +19,25 @@

logger = init_logging(__name__)

ENV_VAR_PREFIX = "IMAGEKIT_UPLOADER"


class ImageKitUploader:
def __init__(self: Self, args: argparse.Namespace) -> None:
def __init__(self: Self, args: configargparse.Namespace) -> None:
self.artifact_name: str = args.artifact_name
self.workflow_run_id: str = args.workflow_run_id
self.verbosity: bool = args.verbose
self.fail_on_error: bool = args.fail_on_error
self.github_repository: str = args.github_repository
self.tmp_path: Path = Path()

if args.verbose:
logger.setLevel("INFO")

try:
self.imagekit: ImageKit = ImageKit(
private_key=os.environ["IMAGEKIT_PRIVATE_KEY"],
public_key=os.environ["IMAGEKIT_PUBLIC_KEY"],
url_endpoint=os.environ["IMAGEKIT_URL_ENDPOINT"],
private_key=args.private_key,
public_key=args.public_key,
url_endpoint=args.url_endpoint,
)
self.imagekit_options_common: UploadFileRequestOptions = UploadFileRequestOptions(
use_unique_file_name=False,
Expand All @@ -56,16 +61,13 @@ def upload_image(self: Self, image_path: Path) -> bool:
return result.url != ""

def run(self: Self) -> None:
if self.verbosity:
logger.setLevel("INFO")

logger.info("Downloading artifact '%s' from workflow '%s'", self.artifact_name, self.workflow_run_id)
with tempfile.TemporaryDirectory() as tmpdir:
logger.info("Created temporary directory '%s'", tmpdir)
self.tmp_path = Path(tmpdir)

GithubActionHelper.download_artifact(
repo=os.environ["GITHUB_REPOSITORY"],
repo=self.github_repository,
workflow_run_id=self.workflow_run_id,
artifact_name=self.artifact_name,
target_directory=self.tmp_path,
Expand All @@ -86,7 +88,7 @@ def run(self: Self) -> None:


def main() -> None:
parser: argparse.ArgumentParser = argparse.ArgumentParser(
parser: configargparse.ArgumentParser = configargparse.ArgumentParser(
prog="VoronDesign Imagekit uploader",
description="This tool can be used to upload images to an imagekit account",
)
Expand All @@ -96,6 +98,7 @@ def main() -> None:
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_WORKFLOW_RUN_ID",
help="Run ID of the workflow from which to pull the artifact",
)
parser.add_argument(
Expand All @@ -104,13 +107,42 @@ def main() -> None:
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_ARTIFACT_NAME",
help="Name of the artifact to download and extract images from",
)
parser.add_argument(
"-r",
"--private_key",
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_PRIVATE_KEY",
help="Imagekit private key",
)
parser.add_argument(
"-p",
"--public_key",
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_PUBLIC_KEY",
help="Imagekit public key",
)
parser.add_argument(
"-u",
"--url_endpoint",
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_URL_ENDPOINT",
help="Imagekit url endpoint (e.g. https://ik.imagekit.io/username)",
)
parser.add_argument(
"-f",
"--fail_on_error",
required=False,
action="store_true",
env_var=f"{ENV_VAR_PREFIX}_FAIL_ON_ERROR",
help="Whether to return an error exit code if one of the STLs is faulty",
default=False,
)
Expand All @@ -119,10 +151,21 @@ def main() -> None:
"--verbose",
required=False,
action="store_true",
env_var=f"{ENV_VAR_PREFIX}_VERBOSE",
help="Print debug output to stdout",
default=False,
)
args: argparse.Namespace = parser.parse_args()
parser.add_argument(
"-g",
"--github_repository",
required=False,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_GITHUB_REPOSITORY",
default=os.environ["GITHUB_REPOSITORY"],
help="Repository from which to download the artifact",
)
args: configargparse.Namespace = parser.parse_args()
ImageKitUploader(args=args).run()


Expand Down
20 changes: 12 additions & 8 deletions voron_ci/tools/mod_structure_checker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import argparse
import sys
from enum import StrEnum
from pathlib import Path
from typing import Any, Self

import configargparse
import yaml

from voron_ci.contants import EXTENDED_OUTCOME, ReturnStatus
Expand All @@ -22,19 +22,22 @@ class FileErrors(StrEnum):

IGNORE_FILES = ["README.md", "mods.json"]
MOD_DEPTH = 2
ENV_VAR_PREFIX = "MOD_STRUCTURE_CHECKER"


class ModStructureChecker:
def __init__(self: Self, args: argparse.Namespace) -> None:
def __init__(self: Self, args: configargparse.Namespace) -> None:
self.input_dir: Path = Path(Path.cwd(), args.input_dir)
self.verbosity: bool = args.verbose
self.fail_on_error: bool = args.fail_on_error
self.print_gh_step_summary: bool = args.github_step_summary
self.return_status: ReturnStatus = ReturnStatus.SUCCESS
self.check_summary: list[tuple[str, ...]] = []

self.errors: dict[Path, str] = {}

if args.verbose:
logger.setLevel("INFO")

def _check_mods(self: Self, input_dir: Path) -> None:
mod_folders = [folder for folder in input_dir.glob("*/*") if folder.is_dir() and folder.relative_to(input_dir).as_posix() not in IGNORE_FILES]

Expand Down Expand Up @@ -68,9 +71,6 @@ def _check_shallow_files(self: Self, input_dir: Path) -> None:
self.return_status = ReturnStatus.FAILURE

def run(self: Self) -> None:
if self.verbosity:
logger.setLevel("INFO")

logger.info("Starting files check in '%s'", str(self.input_dir))

self._check_shallow_files(input_dir=self.input_dir)
Expand All @@ -97,7 +97,7 @@ def run(self: Self) -> None:


def main() -> None:
parser: argparse.ArgumentParser = argparse.ArgumentParser(
parser: configargparse.ArgumentParser = configargparse.ArgumentParser(
prog="VoronDesign VoronUsers mod checker",
description="This tool is used to check whether all mods in VoronUsers are properly specified",
)
Expand All @@ -107,13 +107,15 @@ def main() -> None:
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_INPUT_DIR",
help="Directory containing STL files to be checked",
)
parser.add_argument(
"-v",
"--verbose",
required=False,
action="store_true",
env_var=f"{ENV_VAR_PREFIX}_VERBOSE",
help="Print debug output to stdout",
default=False,
)
Expand All @@ -122,6 +124,7 @@ def main() -> None:
"--fail_on_error",
required=False,
action="store_true",
env_var=f"{ENV_VAR_PREFIX}_FAIL_ON_ERROR",
help="Whether to return an error exit code if one of the STLs is faulty",
default=False,
)
Expand All @@ -130,10 +133,11 @@ def main() -> None:
"--github_step_summary",
required=False,
action="store_true",
env_var=f"{ENV_VAR_PREFIX}_GITHUB_STEP_SUMMARY",
help="Whether to output a step summary when running inside a github action",
default=False,
)
args: argparse.Namespace = parser.parse_args()
args: configargparse.Namespace = parser.parse_args()
ModStructureChecker(args=args).run()


Expand Down
20 changes: 13 additions & 7 deletions voron_ci/tools/readme_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import argparse
import json
import textwrap
from pathlib import Path
from typing import Any, Self

import configargparse
import yaml

from voron_ci.utils.github_action_helper import GithubActionHelper
Expand All @@ -25,17 +25,19 @@
"""

ENV_VAR_PREFIX = "README_GENERATOR"


class ReadmeGenerator:
def __init__(self: Self, args: argparse.Namespace) -> None:
def __init__(self: Self, args: configargparse.Namespace) -> None:
self.input_dir: Path = Path(Path.cwd(), args.input_dir)
self.verbosity: bool = args.verbose
self.json_path: str = args.json_path
self.readme_path: str = args.readme_path

def run(self: Self) -> None:
if self.verbosity:
if args.verbose:
logger.setLevel("INFO")

def run(self: Self) -> None:
logger.info(
"ReadmeGenerator starting up with readme_path: '%s', json_path: '%s', input_dir: '%s'", self.readme_path, self.json_path, self.input_dir.as_posix()
)
Expand Down Expand Up @@ -89,7 +91,7 @@ def run(self: Self) -> None:


def main() -> None:
parser: argparse.ArgumentParser = argparse.ArgumentParser(
parser: configargparse.ArgumentParser = configargparse.ArgumentParser(
prog="VoronDesign VoronUsers readme generator",
description="This tool is used to generate the readme and json overview files for VORONUsers",
)
Expand All @@ -99,6 +101,7 @@ def main() -> None:
required=True,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_INPUT_DIR",
help="Base directory to search for metadata files",
)
parser.add_argument(
Expand All @@ -107,6 +110,7 @@ def main() -> None:
required=False,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_README_PATH",
help="Readme output path (leave empty to not generate a Readme file)",
default="",
)
Expand All @@ -116,6 +120,7 @@ def main() -> None:
required=False,
action="store",
type=str,
env_var=f"{ENV_VAR_PREFIX}_JSON_PATH",
help="Json output path (leave empty to not generate a json file)",
default="",
)
Expand All @@ -124,10 +129,11 @@ def main() -> None:
"--verbose",
required=False,
action="store_true",
env_var=f"{ENV_VAR_PREFIX}_VERBOSE",
help="Print debug output to stdout",
default=False,
)
args: argparse.Namespace = parser.parse_args()
args: configargparse.Namespace = parser.parse_args()
ReadmeGenerator(args=args).run()


Expand Down
Loading

0 comments on commit 02e571e

Please sign in to comment.