1
0
Fork 0
mirror of https://github.com/ArtemSBulgakov/buildozer-action.git synced 2024-08-16 10:09:52 +02:00
gh-buildozer-action/entrypoint.py

160 lines
4.8 KiB
Python
Raw Permalink Normal View History

#!/bin/python3
"""
Buildozer action
================
It sets some environment variables, installs Buildozer, runs Buildozer and finds
output file.
You can read this file top down because functions are ordered by their execution
order.
"""
import os
import subprocess
2020-07-31 22:32:16 +02:00
import sys
2020-07-29 15:19:30 +02:00
from os import environ as env
def main():
repository_root = os.path.abspath(env["INPUT_REPOSITORY_ROOT"])
change_owner(env["USER"], repository_root)
fix_home()
install_buildozer(env["INPUT_BUILDOZER_VERSION"])
apply_buildozer_settings()
change_directory(env["INPUT_REPOSITORY_ROOT"], env["INPUT_WORKDIR"])
apply_patches()
run_command(env["INPUT_COMMAND"])
set_output(env["INPUT_REPOSITORY_ROOT"], env["INPUT_WORKDIR"])
change_owner("root", repository_root)
def change_owner(user, repository_root):
# GitHub sets root as owner of repository directory. Change it to user
# And return to root after all commands
subprocess.check_call(["sudo", "chown", "-R", user, repository_root])
def fix_home():
# GitHub sets HOME to /github/home, but Buildozer is installed to /home/user. Change HOME to user's home
env["HOME"] = env["HOME_DIR"]
def install_buildozer(buildozer_version):
# Install required Buildozer version
print("::group::Installing Buildozer")
pip_install = [sys.executable] + "-m pip install --user --upgrade".split()
if buildozer_version == "stable":
# Install stable buildozer from PyPI
subprocess.check_call([*pip_install, "buildozer"])
2020-07-29 15:08:19 +02:00
elif os.path.exists(buildozer_version) and os.path.exists(
os.path.join(buildozer_version, "buildozer", "__init__.py")
):
# Install from local directory
subprocess.check_call([*pip_install, buildozer_version])
elif buildozer_version.startswith("git+"):
# Install from specified git+ link
subprocess.check_call([*pip_install, buildozer_version])
elif buildozer_version == "":
# Just do nothing
print(
"::warning::Buildozer is not installed because "
"specified buildozer_version is nothing."
)
else:
# Install specified ref from repository
subprocess.check_call(
[
*pip_install,
f"git+https://github.com/kivy/buildozer.git@{buildozer_version}",
]
)
print("::endgroup::")
def apply_buildozer_settings():
# Buildozer settings to disable interactions
env["BUILDOZER_WARN_ON_ROOT"] = "0"
env["APP_ANDROID_ACCEPT_SDK_LICENSE"] = "1"
# Do not allow to change directories
env["BUILDOZER_BUILD_DIR"] = "./.buildozer"
env["BUILDOZER_BIN"] = "./bin"
def change_directory(repository_root, workdir):
directory = os.path.join(repository_root, workdir)
# Change directory to workir
if not os.path.exists(directory):
print("::error::Specified workdir is not exists.")
exit(1)
os.chdir(directory)
def apply_patches():
# Apply patches
print("::group::Applying patches to Buildozer")
try:
2020-07-31 22:06:14 +02:00
import importlib
import site
2020-07-31 22:32:16 +02:00
importlib.reload(site)
2020-07-31 22:06:14 +02:00
globals()["buildozer"] = importlib.import_module("buildozer")
except ImportError:
print(
"::error::Cannot apply patches to buildozer (ImportError). "
"Update buildozer-action to new version or create a Bug Request"
)
2020-07-31 21:34:08 +02:00
print("::endgroup::")
return
print("Changing global_buildozer_dir")
source = open(buildozer.__file__, "r", encoding="utf-8").read()
new_source = source.replace(
"""
@property
def global_buildozer_dir(self):
return join(expanduser('~'), '.buildozer')
2020-07-31 22:32:16 +02:00
""",
f"""
@property
def global_buildozer_dir(self):
return '{env["GITHUB_WORKSPACE"]}/{env["INPUT_REPOSITORY_ROOT"]}/.buildozer_global'
2020-07-31 22:32:16 +02:00
""",
)
if new_source == source:
print(
"::warning::Cannot change global buildozer directory. "
"Update buildozer-action to new version or create a Bug Request"
)
open(buildozer.__file__, "w", encoding="utf-8").write(new_source)
print("::endgroup::")
def run_command(command):
# Run command
retcode = subprocess.check_call(command, shell=True)
if retcode:
print(f'::error::Error while executing command "{command}"')
exit(1)
def set_output(repository_root, workdir):
if not os.path.exists("bin"):
2020-07-29 15:19:30 +02:00
print(
"::error::Output directory does not exist. See Buildozer log for error"
)
exit(1)
2020-07-29 15:08:19 +02:00
filename = [
2020-07-29 15:19:30 +02:00
file
for file in os.listdir("bin")
if os.path.isfile(os.path.join("bin", file))
2020-07-29 15:08:19 +02:00
][0]
2020-07-29 15:19:30 +02:00
path = os.path.normpath(
os.path.join(repository_root, workdir, "bin", filename)
)
2020-07-29 15:08:19 +02:00
print(f"::set-output name=filename::{path}")
if __name__ == "__main__":
main()