forked from ArtemSBulgakov/buildozer-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.py
executable file
·161 lines (137 loc) · 4.96 KB
/
entrypoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/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
import sys
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"])
change_owner(env["USER"], os.environ["GITHUB_OUTPUT"])
set_output(env["INPUT_REPOSITORY_ROOT"], env["INPUT_WORKDIR"])
change_owner("root", repository_root)
change_owner("root", os.environ["GITHUB_OUTPUT"])
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"])
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(f"::error::Specified workdir '{directory}' does not exist.")
exit(1)
os.chdir(directory)
def apply_patches():
# Apply patches
print("::group::Applying patches to Buildozer")
try:
import importlib
import site
importlib.reload(site)
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"
)
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')
""",
f"""
@property
def global_buildozer_dir(self):
return '{env["GITHUB_WORKSPACE"]}/{env["INPUT_REPOSITORY_ROOT"]}/.buildozer_global'
""",
)
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"):
print(
"::error::Output directory does not exist. See Buildozer log for error"
)
exit(1)
filename = [
file
for file in os.listdir("bin")
if os.path.isfile(os.path.join("bin", file))
][0]
path = os.path.normpath(
os.path.join(repository_root, workdir, "bin", filename)
)
with open(os.environ["GITHUB_OUTPUT"], "a") as gofh:
print(f'filename={path}', file=gofh)
if __name__ == "__main__":
main()