From 56068b078579818db9fc5d85a91ac395ee1cf758 Mon Sep 17 00:00:00 2001 From: Rhythmic System Date: Tue, 31 Dec 2024 15:38:26 -0800 Subject: [PATCH] attempt v2 --- .github/workflows/deploy.py | 130 ++++++++++++++++++++--------------- .github/workflows/deploy.yml | 2 + 2 files changed, 75 insertions(+), 57 deletions(-) diff --git a/.github/workflows/deploy.py b/.github/workflows/deploy.py index 3e8afce..181851f 100644 --- a/.github/workflows/deploy.py +++ b/.github/workflows/deploy.py @@ -3,6 +3,7 @@ import zipfile import requests + # Constants datapack_prefix = "purpurpack_" datapack_folder = "packs" @@ -13,7 +14,7 @@ print("The required environment variable MODRINTH_TOKEN is not set.") exit(1) -def fetch_modrinth_info(project_id): +def fetch_versions_json(project_id): """Fetches information about available versions of a project from Modrinth.""" url = f"https://api.modrinth.com/v2/project/{project_id}/version" headers = { @@ -26,70 +27,85 @@ def fetch_modrinth_info(project_id): print(f"Failed to fetch info for project ID: {project_id} (HTTP {response.status_code})") return None -def get_latest_version(versions): - """Extracts the latest version number from a JSON array of version data.""" +def get_latest_modrinth_version(project_id): + versions = fetch_versions_json(project_id) for version_data in versions: if "datapack" in version_data.get("loaders", []): return version_data.get("version_number") return None -def main(): - for datapack_path in os.listdir(datapack_folder): - full_datapack_path = os.path.join(datapack_folder, datapack_path) - modrinth_file_path = os.path.join(full_datapack_path, "modrinth.json") - - # Continue if not a directory - if not os.path.isdir(full_datapack_path): - continue - - # Continue if datapack folder does not include modrinth.json - if not os.path.exists(modrinth_file_path): - print(f"No modrinth.json file located in {full_datapack_path}") - continue +def get_datapack_info(modrinth_json_path): + with open(modrinth_json_path, "r") as modrinth_file: + file_info = json.load(modrinth_file) + return file_info + +def get_current_version(json_file): + return json_file["version_number"] + +def get_project_id(json_file): + return json_file["project_id"] + +def should_update_pack(modrinth_json): + if modrinth_json is None: + print("Modrinth JSON is empty or missing, skipping") + return False + project_id = get_project_id(modrinth_json) + current_version = get_current_version(modrinth_json) + if current_version is None: + print ("Pack does not have a version number, skipping") + return False + if project_id is None: + print ("Pack does not have a project ID, skipping") + return False + latest_version = get_latest_modrinth_version(project_id) + if latest_version >= current_version: + print("Pack does not need to be updated, skipping") + return False + if latest_version is None: + return True + if latest_version < current_version: + return True + +def zip_and_post(modrinth_info, root): + version = get_current_version(modrinth_info) + project_id = get_project_id(modrinth_info) + relative_path = os.path.relpath(root, datapack_folder) + datapack_name = f"{relative_path.replace(os.sep, '_')}_v{version}.zip" + full_datapack_path = os.path.join(root, datapack_name) + dist_dir = os.path.join(full_datapack_path, "dist") + os.makedirs(dist_dir, exist_ok=True) + zipped_file_path = os.path.join(dist_dir, datapack_name) + with zipfile.ZipFile(zipped_file_path, "w", zipfile.ZIP_DEFLATED) as zipped_file: + for root, dirs, files in os.walk(full_datapack_path): + for file in files: + if file == 'modrinth.json': + continue + zipped_file.write(os.path.join(root, file), arcname=file) + print(f"zipping {file}") + + files = { + 'data': (None, json.dumps(modrinth_info), 'application/json'), + 'file': (datapack_name, open(zipped_file_path, 'rb'), 'application/zip') + } + response = requests.post(modrinth_post_version_route, headers={"Authorization": modrinth_token}, files=files) + upload_response = response.json() - print(f"Processing {datapack_path}...") + if 'error' not in upload_response: + print(f"Successfully uploaded version {version} for {project_id}! Output:\n{upload_response}") + else: + print(f"Failed to upload {datapack_name}. Output:\n{upload_response}") - with open('modrinth.json') as base_file, open(modrinth_file_path) as datapack_file: - base_json = json.load(base_file) - datapack_json = json.load(datapack_file) - modrinth_json = {**base_json, **datapack_json} - project_id = modrinth_json["project_id"] - current_version = modrinth_json["version_number"] - versions = fetch_modrinth_info(project_id) - if versions is None: +def main(): + for root, dirs, files in os.walk(datapack_folder): + if 'pack.mcmeta' not in files: + print(f"{root} is not a pack directory, skipping") continue - - latest_version = get_latest_version(versions) - if latest_version is None or latest_version == current_version: - print(f"No new version available for project {project_id}. Skipping...") + if 'modrinth.json' not in files: + print(f"{root} does not have a modrinth json file, skipping") continue - - zipped_file_name = f"{datapack_prefix}{datapack_path}_{current_version}.zip" - dist_dir = os.path.join(full_datapack_path, "dist") - os.makedirs(dist_dir, exist_ok=True) - zipped_file_path = os.path.join(dist_dir, zipped_file_name) - - with zipfile.ZipFile(zipped_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf: - for root, _, files in os.walk(full_datapack_path): - for file in files: - if file != 'modrinth.json' and 'dist' not in root: - zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), full_datapack_path)) - - print(f"Datapack located at: {zipped_file_path}") - - files = { - 'data': (None, json.dumps(modrinth_json), 'application/json'), - 'file': (zipped_file_name, open(zipped_file_path, 'rb'), 'application/zip') - } - response = requests.post(modrinth_post_version_route, headers={"Authorization": modrinth_token}, files=files) - upload_response = response.json() - - if 'error' not in upload_response: - print(f"Successfully uploaded version {current_version} for {project_id}! Output:\n{upload_response}") - else: - print(f"Failed to upload {datapack_path}. Output:\n{upload_response}") - -if __name__ == "__main__": - main() \ No newline at end of file + datapack_info = get_datapack_info(os.path.join(root, "modrinth.json")) + if not should_update_pack(datapack_info): + continue + zip_and_post(datapack_info, root) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 00cd754..2b6a1be 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -15,6 +15,8 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 + with: + python-version: '3.12' - name: Install dependencies run: |