From 9ead0e02186e2f754a577ef94fe5030b14ac6054 Mon Sep 17 00:00:00 2001 From: Draff Date: Sun, 4 Feb 2024 02:07:54 +0000 Subject: [PATCH] (hopefully) un fucky my other wucky --- .github/scripts/create-repo.py | 109 +++++++++++++++++++++++++++++++ .github/scripts/create-repo.sh | 68 ------------------- .github/scripts/move-apks.py | 16 +++++ .github/scripts/move-apks.sh | 26 -------- .github/workflows/build_push.yml | 8 +-- 5 files changed, 129 insertions(+), 98 deletions(-) create mode 100755 .github/scripts/create-repo.py delete mode 100755 .github/scripts/create-repo.sh create mode 100755 .github/scripts/move-apks.py delete mode 100755 .github/scripts/move-apks.sh diff --git a/.github/scripts/create-repo.py b/.github/scripts/create-repo.py new file mode 100755 index 000000000..b31cd8183 --- /dev/null +++ b/.github/scripts/create-repo.py @@ -0,0 +1,109 @@ +import json +import os +import re +import subprocess +from pathlib import Path +from zipfile import ZipFile + +PACKAGE_NAME_REGEX = re.compile(r"package: name='([^']+)'") +VERSION_CODE_REGEX = re.compile(r"versionCode='([^']+)'") +VERSION_NAME_REGEX = re.compile(r"versionName='([^']+)'") +IS_NSFW_REGEX = re.compile(r"'tachiyomi.extension.nsfw' value='([^']+)'") +APPLICATION_LABEL_REGEX = re.compile(r"^application-label:'([^']+)'", re.MULTILINE) +APPLICATION_ICON_320_REGEX = re.compile( + r"^application-icon-320:'([^']+)'", re.MULTILINE +) +LANGUAGE_REGEX = re.compile(r"tachiyomi-([^\.]+)") + +*_, ANDROID_BUILD_TOOLS = (Path(os.environ["ANDROID_HOME"]) / "build-tools").iterdir() +REPO_DIR = Path("repo") +REPO_APK_DIR = REPO_DIR / "apk" +REPO_ICON_DIR = REPO_DIR / "icon" + +REPO_ICON_DIR.mkdir(parents=True, exist_ok=True) + +with open("output.json", encoding="utf-8") as f: + inspector_data = json.load(f) + +index_data = [] +index_min_data = [] + +for apk in REPO_APK_DIR.iterdir(): + badging = subprocess.check_output( + [ + ANDROID_BUILD_TOOLS / "aapt", + "dump", + "--include-meta-data", + "badging", + apk, + ] + ).decode() + + package_info = next(x for x in badging.splitlines() if x.startswith("package: ")) + package_name = PACKAGE_NAME_REGEX.search(package_info).group(1) + application_icon = APPLICATION_ICON_320_REGEX.search(badging).group(1) + + with ZipFile(apk) as z, z.open(application_icon) as i, ( + REPO_ICON_DIR / f"{package_name}.png" + ).open("wb") as f: + f.write(i.read()) + + language = LANGUAGE_REGEX.search(apk.name).group(1) + sources = inspector_data[package_name] + + if len(sources) == 1: + source_language = sources[0]["lang"] + + if ( + source_language != language + and source_language not in {"all", "other"} + and language not in {"all", "other"} + ): + language = source_language + + common_data = { + "name": APPLICATION_LABEL_REGEX.search(badging).group(1), + "pkg": package_name, + "apk": apk.name, + "lang": language, + "code": int(VERSION_CODE_REGEX.search(package_info).group(1)), + "version": VERSION_NAME_REGEX.search(package_info).group(1), + "nsfw": int(IS_NSFW_REGEX.search(badging).group(1)), + } + min_data = { + **common_data, + "sources": [], + } + + for source in sources: + min_data["sources"].append( + { + "name": source["name"], + "lang": source["lang"], + "id": source["id"], + "baseUrl": source["baseUrl"], + } + ) + + index_min_data.append(min_data) + index_data.append( + { + **common_data, + "hasReadme": 0, + "hasChangelog": 0, + "sources": sources, + } + ) + +index_data.sort(key=lambda x: x["pkg"]) +index_min_data.sort(key=lambda x: x["pkg"]) + +with (REPO_DIR / "index.json").open("w", encoding="utf-8") as f: + index_data_str = json.dumps(index_data, ensure_ascii=False, indent=2) + + print(index_data_str) + f.write(index_data_str) + +with (REPO_DIR / "index.min.json").open("w", encoding="utf-8") as f: + json.dump(index_min_data, f, ensure_ascii=False, separators=(",", ":")) + diff --git a/.github/scripts/create-repo.sh b/.github/scripts/create-repo.sh deleted file mode 100755 index ac93d5254..000000000 --- a/.github/scripts/create-repo.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash -set -e - -TOOLS="$(ls -d ${ANDROID_HOME}/build-tools/* | tail -1)" - -mkdir -p repo/apk -mkdir -p repo/icon - -cd repo - -APKS=( ../apk/*".apk" ) - -for APK in ${APKS[@]}; do - cp $APK apk/ - FILENAME=$(basename ${APK}) - BADGING="$(${TOOLS}/aapt dump --include-meta-data badging $APK)" - - PACKAGE=$(echo "$BADGING" | grep package:) - PKGNAME=$(echo $PACKAGE | grep -Po "package: name='\K[^']+") - VCODE=$(echo $PACKAGE | grep -Po "versionCode='\K[^']+") - VNAME=$(echo $PACKAGE | grep -Po "versionName='\K[^']+") - NSFW=$(echo $BADGING | grep -Po "tachiyomi.extension.nsfw' value='\K[^']+") - HASREADME=$(echo $BADGING | grep -Po "tachiyomi.extension.hasReadme' value='\K[^']+") - HASCHANGELOG=$(echo $BADGING | grep -Po "tachiyomi.extension.hasChangelog' value='\K[^']+") - - APPLICATION=$(echo "$BADGING" | grep application:) - LABEL=$(echo $APPLICATION | grep -Po "label='\K[^']+") - - LANG=$(echo $APK | grep -Po "tachiyomi-\K[^\.]+") - - ICON=$(echo "$BADGING" | grep -Po "application-icon-320.*'\K[^']+") - unzip -p $APK $ICON > icon/${PKGNAME}.png - - # TODO: legacy icons; remove after a while - cp icon/${PKGNAME}.png icon/${FILENAME%.*}.png - - SOURCE_INFO=$(jq ".[\"$PKGNAME\"]" < ../output.json) - - # Fixes the language code without needing to update the packages. - SOURCE_LEN=$(echo $SOURCE_INFO | jq length) - - if [ $SOURCE_LEN = "1" ]; then - SOURCE_LANG=$(echo $SOURCE_INFO | jq -r '.[0].lang') - - if [ $SOURCE_LANG != $LANG ] && [ $SOURCE_LANG != "all" ] && [ $SOURCE_LANG != "other" ] && [ $LANG != "all" ] && [ $LANG != "other" ]; then - LANG=$SOURCE_LANG - fi - fi - - jq -n \ - --arg name "$LABEL" \ - --arg pkg "$PKGNAME" \ - --arg apk "$FILENAME" \ - --arg lang "$LANG" \ - --argjson code $VCODE \ - --arg version "$VNAME" \ - --argjson nsfw $NSFW \ - --argjson hasReadme $HASREADME \ - --argjson hasChangelog $HASCHANGELOG \ - --argjson sources "$SOURCE_INFO" \ - '{name:$name, pkg:$pkg, apk:$apk, lang:$lang, code:$code, version:$version, nsfw:$nsfw, hasReadme:$hasReadme, hasChangelog:$hasChangelog, sources:$sources}' - -done | jq -sr '[.[]]' > index.json - -# Alternate minified copy -jq -c 'map(del(.hasReadme, .hasChangelog, .sources[]["versionId", "hasCloudflare"]))' < index.json > index.min.json - -cat index.json diff --git a/.github/scripts/move-apks.py b/.github/scripts/move-apks.py new file mode 100755 index 000000000..09622fc14 --- /dev/null +++ b/.github/scripts/move-apks.py @@ -0,0 +1,16 @@ +from pathlib import Path +import shutil + +REPO_APK_DIR = Path("repo/apk") + +try: + shutil.rmtree(REPO_APK_DIR) +except FileNotFoundError: + pass + +REPO_APK_DIR.mkdir(parents=True, exist_ok=True) + +for apk in (Path.home() / "apk-artifacts").glob("**/*.apk"): + apk_name = apk.name.replace("-release.apk", ".apk") + + shutil.move(apk, REPO_APK_DIR / apk_name) \ No newline at end of file diff --git a/.github/scripts/move-apks.sh b/.github/scripts/move-apks.sh deleted file mode 100755 index ba0d3cf3e..000000000 --- a/.github/scripts/move-apks.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -set -e -shopt -s globstar nullglob extglob - -# Get APKs from previous jobs' artifacts -cp -R ~/apk-artifacts/ $PWD -APKS=( **/*".apk" ) - -# Fail if too little extensions seem to have been built -if [ "${#APKS[@]}" -le "100" ]; then - echo "Insufficient amount of APKs found. Please check the project configuration." - exit 1 -else - echo "Moving ${#APKS[@]} APKs" -fi - -DEST=$PWD/apk -rm -rf $DEST && mkdir -p $DEST - -for APK in ${APKS[@]}; do - BASENAME=$(basename $APK) - APKNAME="${BASENAME%%+(-release*)}.apk" - APKDEST="$DEST/$APKNAME" - - cp $APK $APKDEST -done diff --git a/.github/workflows/build_push.yml b/.github/workflows/build_push.yml index 4c8838200..fcb9623a7 100644 --- a/.github/workflows/build_push.yml +++ b/.github/workflows/build_push.yml @@ -136,11 +136,11 @@ jobs: - name: Create repo artifacts run: | cd master - ./.github/scripts/move-apks.sh - INSPECTOR_LINK="$(curl -s "https://api.github.com/repos/tachiyomiorg/tachiyomi-extensions-inspector/releases/latest" | jq -r '.assets[0].browser_download_url')" + python ./.github/scripts/move-apks.py + INSPECTOR_LINK="$(curl -s "https://api.github.com/repos/keiyoushi/extensions-inspector/releases/latest" | jq -r '.assets[0].browser_download_url')" curl -L "$INSPECTOR_LINK" -o ./Inspector.jar - java -jar ./Inspector.jar "apk" "output.json" "tmp" - ./.github/scripts/create-repo.sh + java -jar ./Inspector.jar "repo/apk" "output.json" "tmp" + python ./.github/scripts/create-repo.py - name: Checkout repo branch uses: actions/checkout@v4