Last active
October 3, 2024 08:27
-
-
Save MustafaJafar/be7989dbee17b1bfb574612f978c77d1 to your computer and use it in GitHub Desktop.
Upload addon zips automatically using ayon-python-api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!Upload addons via ayon api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AYON_SERVER_URL="AYON Server Url" # e.g. "http://127.0.0.1:5000/" | |
AYON_API_KEY="AYON service user api key" | |
ADDON_PATH="E:/Ynput/ayon-packages" # It supports relative paths, and you can override it by using `--addon-dir` flag |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Upload addon zip using ayon-python-api. | |
It's used to upload addons versions that epcified as arguments. | |
It requires having a .env file with the following keys: | |
- 'AYON_SERVER_URL': AYON server URL | |
- 'AYON_API_KEY': AYON service user api key | |
- 'ADDON_PATH': path to addons directory. this script will search it for the given addon versions. | |
It supports relative paths. | |
Script usage: | |
python upload_addon_zip.py --addon-version addon-x.y.z | |
e.g. | |
python upload_addon_zip.py --addon-version houdini-0.2.12 | |
Support flags: | |
'--debug': used to make log more verbose. | |
'--addon-version' ('-a'): used to specify addon versions to upload. you can use it multiple times. | |
e.g. | |
python upload_addon_zip.py --debug --addon-version houdini-0.2.12 --addon-version maya-0.1.16 --addon-version aftereffects-0.1.3 | |
This snippet is brought from https://github.com/ynput/ayon-kitsu/blob/develop/create_package.py | |
""" | |
import argparse | |
import logging | |
import os | |
import sys | |
from pathlib import Path | |
try: | |
import ayon_api | |
from ayon_api import get_server_api_connection | |
has_ayon_api = True | |
except ModuleNotFoundError: | |
has_ayon_api = False | |
try: | |
from dotenv import load_dotenv | |
load_dotenv() | |
except ModuleNotFoundError: | |
if has_ayon_api: | |
logging.warning("dotenv not installed, skipping loading .env file") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"--debug", | |
dest="debug", | |
action="store_true", | |
help="Debug log messages." | |
) | |
parser.add_argument( | |
"-a", | |
"--addon-version", | |
dest="addons_version", | |
action="append", | |
help="Limit addon creation to given addon name.", | |
) | |
parser.add_argument( | |
"--all", | |
dest="upload_all", | |
action="store_true", | |
help="Upload all addons exist in the addon path.", | |
) | |
parser.add_argument( | |
"--addon-dir", | |
dest="addon_dir", | |
default=None, | |
help="Override default addon path." | |
) | |
args = parser.parse_args(sys.argv[1:]) | |
# Set Log Level and create log object | |
level = logging.INFO | |
if args.debug: | |
level = logging.DEBUG | |
logging.basicConfig(level=level) | |
log: logging.Logger = logging.getLogger("upload_package") | |
# Check if addon path exists. | |
addon_path = args.addon_dir or os.getenv("ADDON_PATH") | |
addon_path = addon_path.replace(".", os.getcwd()) | |
addon_path = Path(addon_path) | |
addon_path = addon_path.resolve() | |
if not addon_path.exists(): | |
log.debug(addon_path) | |
raise RuntimeError( | |
"Addon path doesn't exist: {}".format(addon_path) | |
) | |
addon_path = str(addon_path) | |
# Log in and Try to upload addons | |
ayon_api.init_service() | |
log.info("Trying to upload zips") | |
trigger_restart = False | |
if args.upload_all: | |
addons = os.listdir(addon_path) | |
else: | |
# Check for input arguments. | |
if not args.addons_version: | |
raise RuntimeError( | |
"No Addons found in arguments. Please specify addon version to upload." | |
) | |
addons = args.addons_version | |
for addon_zip in addons: | |
# TODO: Find latest version if user didn't specify version in the argument. | |
# e.g. --addon-version houdini | |
# Tbh, it can be tricky because user may have | |
# 'addon-x.y.z', 'addon-x.y.z-studio.i', 'addon-x.y.z-dev.i' | |
# so, we would need to find a way to specify which one is preferred. | |
if not addon_zip.endswith(".zip"): | |
addon_zip += ".zip" | |
path = os.path.join( | |
addon_path, addon_zip | |
).replace("\\", "/") | |
if not os.path.isfile(path): | |
log.warning("Skipping, file is not found: '{}'." | |
.format(path)) | |
continue | |
log.info("Uploading: '{}'".format(path)) | |
response = ayon_api.upload_addon_zip(path) | |
trigger_restart = True | |
if trigger_restart: | |
server = get_server_api_connection() | |
if server: | |
server.trigger_server_restart() | |
else: | |
log.warning("Could not restart server") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// my .vscode/tasks.json | |
// this vscode task makes use of `upload_addon.py` | |
// I'm using it with every addon on my disk. | |
"version": "2.0.0", | |
"tasks": [ | |
{ | |
"label": "Create addon", | |
"type": "shell", | |
"command": "python", | |
"args": [ | |
"${cwd}/create_package.py", | |
], | |
"problemMatcher": [] | |
}, | |
{ | |
"label": "Upload addon", | |
"type": "shell", | |
"command": "python", | |
"args": [ | |
"E:/Ynput/ayon-helper-scripts/upload_addon.py", // find upload_addon.py in https://gist.github.com/MustafaJafar/be7989dbee17b1bfb574612f978c77d1 | |
"--addon-dir", | |
"${cwd}/package", | |
"--addon-version", | |
"${input:addon_version}" | |
], | |
"problemMatcher": [] | |
} | |
], | |
"inputs": [ | |
{ | |
"id": "addon_version", | |
"description": "", | |
"default": "ayon_third_party-1.1.2-dev.1", | |
"type": "promptString" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can also use the script in a different way than inside vscode tasks.
Basically, I can write a bat file to create all the packages in some location
and then upload them all to ayon.
Tip
Don't forget to update dependency packages.