From 591c68a6e478467ea45784e3556a098457058381 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 10 Aug 2023 02:58:08 +0530 Subject: [PATCH] scripts: return to monke I don't like bash but turns out I dislike Python more This reverts commit 2628bfc2cd083122c994b5e0a64702e299a7aea4, d77fa13903c7c2a144de7af63015bc7478c55f6e and 69f63cced4d6d2bf123b78df1425e2c2a5a7098e. --- .github/workflows/ci.yml | 12 +-- .../msfjarvis/claw/gradle/SpotlessPlugin.kt | 4 - scripts/boot-emulator.py | 71 ------------ scripts/boot-emulator.sh | 28 +++++ scripts/deploy-snapshot.py | 101 ------------------ scripts/deploy-snapshot.sh | 43 ++++++++ scripts/encrypt-secret.py | 31 ------ scripts/encrypt-secret.sh | 18 ++++ 8 files changed, 90 insertions(+), 218 deletions(-) delete mode 100755 scripts/boot-emulator.py create mode 100755 scripts/boot-emulator.sh delete mode 100755 scripts/deploy-snapshot.py create mode 100755 scripts/deploy-snapshot.sh delete mode 100755 scripts/encrypt-secret.py create mode 100755 scripts/encrypt-secret.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 735559b1..8a8018ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,10 +28,6 @@ jobs: distribution: temurin java-version: 18 - - name: Set up black (Python) - shell: bash - run: pip install black==23.3.0 - - name: Run unit tests uses: gradle/gradle-build-action@a4cf152f482c7ca97ef56ead29bf08bcd953284c # v2.7.0 with: @@ -61,12 +57,6 @@ jobs: distribution: temurin java-version: 18 - - name: Set up Git author - shell: bash - run: | - git config user.name "GitHub Actions" - git config user.email noreply@github.com - - name: Decrypt secrets run: | ./scripts/setup-age.sh @@ -87,6 +77,6 @@ jobs: run: scripts/signing-cleanup.sh - name: Deploy snapshot - run: scripts/deploy-snapshot.py + run: scripts/deploy-snapshot.sh env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/build-logic/src/main/kotlin/dev/msfjarvis/claw/gradle/SpotlessPlugin.kt b/build-logic/src/main/kotlin/dev/msfjarvis/claw/gradle/SpotlessPlugin.kt index a9025602..4b712908 100644 --- a/build-logic/src/main/kotlin/dev/msfjarvis/claw/gradle/SpotlessPlugin.kt +++ b/build-logic/src/main/kotlin/dev/msfjarvis/claw/gradle/SpotlessPlugin.kt @@ -35,10 +35,6 @@ class SpotlessPlugin : Plugin { targetExclude("**/build/") licenseHeaderFile(project.file("spotless/license.kt"), "import|plugins|@file") } - python { - target("scripts/**.py") - black("23.3.0") - } format("xml") { target("**/*.xml") targetExclude("**/build/", ".idea/", "/spotless/", "**/lint-baseline.xml") diff --git a/scripts/boot-emulator.py b/scripts/boot-emulator.py deleted file mode 100755 index 952906f2..00000000 --- a/scripts/boot-emulator.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python3 - - -import signal -import subprocess -import os -from pathlib import Path - -emu_process = None - - -def signal_handler(sig, frame): - if emu_process is not None: - emu_process.signal(signal.SIGINT) - - -def make_cmdline_tools_path(android_home: Path) -> str: - return android_home / "cmdline-tools" / "latest" / "bin" - - -def main(): - signal.signal(signal.SIGINT, signal_handler) - android_home = os.getenv("ANDROID_HOME") - if not android_home: - raise RuntimeError("$ANDROID_HOME must be set to use this script") - android_home = Path(android_home) - api_level = os.getenv("ANDROID_API_LEVEL") - if not api_level: - print("$ANDROID_API_LEVEL not defined; defaulting to 33") - api_level = "33" - cmdline_path = make_cmdline_tools_path(android_home) - image_package = f"system-images;android-{api_level};google_apis;x86_64" - subprocess.run( - [ - cmdline_path / "sdkmanager", - image_package, - ], - check=True, - ) - subprocess.run( - [ - cmdline_path / "avdmanager", - "create", - "avd", - "--force", - "-n", - f"Pixel_XL_API_{api_level}", - "--abi", - "google_apis/x86_64", - "--package", - image_package, - "--device", - "pixel_xl", - ], - check=True, - ) - emu_process = subprocess.Popen( - [ - android_home / "emulator" / "emulator", - "-avd", - f"Pixel_XL_API_{api_level}", - "-gpu", - "swiftshader_indirect", - "-noaudio", - ], - ) - emu_process.wait() - - -if __name__ == "__main__": - main() diff --git a/scripts/boot-emulator.sh b/scripts/boot-emulator.sh new file mode 100755 index 00000000..cbb568ce --- /dev/null +++ b/scripts/boot-emulator.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# Creates and boots an emulator that exactly matches the one in our CI. It is recommended +# to use this as the target device for screenshot tests. + +set -euo pipefail + +[ -n "${ANDROID_HOME:-}" ] || { + echo "ANDROID_HOME must be set to use this script" + exit 1 +} +[ -n "${ANDROID_API_LEVEL:-}" ] || { echo "ANDROID_API_LEVEL not defined; defaulting to 33"; } + +API_LEVEL="${ANDROID_API_LEVEL:-33}" + +sdkmanager "system-images;android-${API_LEVEL};google_apis;x86_64" + +echo no | "${ANDROID_HOME}"/cmdline-tools/latest/bin/avdmanager create avd \ + --force \ + -n "Pixel_XL_API_${API_LEVEL}" \ + --abi 'google_apis/x86_64' \ + --package "system-images;android-${API_LEVEL};google_apis;x86_64" \ + --device 'pixel_xl' + +"${ANDROID_HOME}"/emulator/emulator \ + -avd "Pixel_XL_API_${API_LEVEL}" \ + -gpu 'swiftshader_indirect' \ + -noaudio diff --git a/scripts/deploy-snapshot.py b/scripts/deploy-snapshot.py deleted file mode 100755 index bd58f499..00000000 --- a/scripts/deploy-snapshot.py +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env python3 - -import subprocess -import os -from typing import Optional -from pathlib import Path -import glob -import tempfile - -NIGHTLY_TAG = "nightly" - - -def exec(command_str: str, shell: bool = False) -> Optional[int]: - print(f"Executing '{command_str}'") - proc = None - if shell: - proc = subprocess.run( - command_str, - text=True, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - ) - else: - proc = subprocess.run( - command_str.split(" "), - text=True, - shell=False, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - ) - result = proc.returncode - print(f"{proc.stdout}") - return result - - -def exec_out(command_str: str, shell: bool = False) -> str: - print(f"Executing '{command_str}'") - proc = None - if shell: - proc = subprocess.run(command_str, capture_output=True, text=True, shell=True) - else: - proc = subprocess.run( - command_str.split(" "), capture_output=True, text=True, shell=False - ) - data = proc.stdout - return data - - -def get_current_rev() -> str: - return exec_out("git rev-parse --short HEAD") - - -def get_asset_directory() -> Path: - workspace = os.getenv("GITHUB_WORKSPACE") - return Path(workspace) / "android" / "apk" - - -def overwrite_local_tag(): - exec(f"git tag -f {NIGHTLY_TAG} -m 'Nightly release'", shell=True) - - -def overwrite_remote_tag(): - exec(f"git push -f origin {NIGHTLY_TAG}") - - -def has_release() -> bool: - result = exec(f"gh release view {NIGHTLY_TAG}") - return result is not None and result == 0 - - -def delete_release(): - exec(f"gh release delete --yes {NIGHTLY_TAG}") - - -def create_release(): - with tempfile.NamedTemporaryFile("w+") as cf: - cf.write(f"Latest release for Claw from revision {get_current_rev()}") - cf.flush() - cwd = os.getcwd() - os.chdir(get_asset_directory()) - apks = " ".join(glob.glob("*.apk")) - exec( - "gh release create --prerelease " - + f"--title 'Latest snapshot build' --notes-file {cf.name} " - + f"{NIGHTLY_TAG} {apks}", - shell=True, - ) - os.chdir(cwd) - - -def main(): - overwrite_local_tag() - if has_release(): - delete_release() - overwrite_remote_tag() - create_release() - - -if __name__ == "__main__": - main() diff --git a/scripts/deploy-snapshot.sh b/scripts/deploy-snapshot.sh new file mode 100755 index 00000000..37556f9e --- /dev/null +++ b/scripts/deploy-snapshot.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +NIGHTLY_TAG="nightly" +CURRENT_REV="$(git rev-parse --short HEAD)" +ASSET_DIRECTORY="${GITHUB_WORKSPACE:?}/android/apk" + +function overwrite_local_tag() { + git tag -f "${NIGHTLY_TAG}" +} + +function overwrite_remote_tag() { + git push -f origin "${NIGHTLY_TAG}" +} + +function has_release() { + gh release view "${NIGHTLY_TAG}" &>/dev/null + echo "$?" +} + +function delete_release() { + gh release delete --yes "${NIGHTLY_TAG}" +} + +function create_release() { + local CHANGELOG_FILE + CHANGELOG_FILE="$(mktemp)" + echo "Latest release for Claw from revision ${CURRENT_REV}" | tee "${CHANGELOG_FILE}" + pushd "${ASSET_DIRECTORY}" || return + gh release create --prerelease --title "Latest snapshot build" --notes-file "${CHANGELOG_FILE}" "${NIGHTLY_TAG}" ./*.apk + popd || return +} + +overwrite_local_tag + +if [[ "$(has_release)" -eq 0 ]]; then + delete_release +fi + +overwrite_remote_tag + +create_release diff --git a/scripts/encrypt-secret.py b/scripts/encrypt-secret.py deleted file mode 100755 index 295facee..00000000 --- a/scripts/encrypt-secret.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python3 - -import sys -import subprocess -import shutil -from pathlib import Path - - -def main(): - if len(sys.argv) != 4: - raise RuntimeError( - "USAGE: encrypt-secret.py " - ) - input_file = Path(sys.argv[1]) - output_file = Path(sys.argv[2]) - age_key = sys.argv[3] - if shutil.which("age") is None: - raise RuntimeError("age not installed") - if not input_file.exists(): - raise RuntimeError(f"Input file '{input_file.name}' does not exist") - recipient = subprocess.run( - ["age-keygen", "-y"], capture_output=True, text=True, input=age_key - ).stdout.strip() - subprocess.run( - ["age", "--encrypt", "-r", recipient, "-o", output_file], - input=input_file.read_bytes(), - ) - - -if __name__ == "__main__": - main() diff --git a/scripts/encrypt-secret.sh b/scripts/encrypt-secret.sh new file mode 100755 index 00000000..80f93d0a --- /dev/null +++ b/scripts/encrypt-secret.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +set -euo pipefail + +INPUT_FILE="${1:-}" +OUTPUT_FILE="${2:-}" +AGE_KEY="${3:-}" + +if ! command -v age 1>/dev/null; then + echo "age not installed" + exit 1 +fi + +if [[ -n "$AGE_KEY" && -n "$INPUT_FILE" && -n "$OUTPUT_FILE" ]]; then + age --encrypt -r "$(echo "${AGE_KEY}" | age-keygen -y)" -o "${OUTPUT_FILE}" < "${INPUT_FILE}" +else + echo "Usage: ./encrypt-secret.sh " +fi