-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathforeman.py
More file actions
107 lines (85 loc) · 4.15 KB
/
foreman.py
File metadata and controls
107 lines (85 loc) · 4.15 KB
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
# Foreman - python script to build the apps in the apps folder and move them onto the rootfs
print("👷 Foreman v1.0.0 - Building Hydro Apps")
import os
import shutil
import subprocess
persist = False
forceRebuild = False
# check if the argument --persist is passed
if "--persist" in os.sys.argv:
persist = True
print("|-🔒 Persistence mode enabled: won't stop if there's a build failure")
if "--force-rebuild" in os.sys.argv:
forceRebuild = True
print("|-🧱 Forcing a rebuild of all apps")
# Get a list of folders in the apps directory
apps = os.listdir("apps")
print(f"|-🔎 Found {len(apps)} apps.")
# Loop through the apps and build them
print("|-🔨 Building apps...")
for app in apps:
# see if the files in src directory are newer than ./main.wasm (if it exists)
# if they are, then `make clean` and `make`
wasm_file_age = os.path.getmtime(f"apps/{app}/main.wasm") if os.path.exists(f"apps/{app}/main.wasm") else 0
src_files = os.listdir(f"apps/{app}/src")
extern_files = []
# see if there's a extern_deps.foreman file in the app directory
if os.path.exists(f"apps/{app}/extern-deps.foreman"):
with open(f"apps/{app}/extern-deps.foreman", "r") as f:
extern_deps = f.read().split("\n")
# the extern_deps are paths to files that the app should be rebuilt if they change
# we need to check if they're newer than the wasm file
# we'll add them to the src_files list if they are
for dep in extern_deps:
if os.path.exists(dep):
extern_files.append(dep)
else:
print(f" |-❌ Error: extern_dep {dep} not found for {app}.")
if not persist:
exit(1)
else:
apps.remove(app)
# see if there's a .foremanignore file in the app directory
if os.path.exists(f"apps/{app}/.foremanignore"):
with open(f"apps/{app}/.foremanignore", "r") as f:
ignore_files = f.read().split("\n")
src_files = [file for file in src_files if file not in ignore_files]
src_files_age = max([os.path.getmtime(f"apps/{app}/src/{file}") for file in src_files])
extern_files_age = max([os.path.getmtime(file) for file in extern_files]) if len(extern_files) > 0 else 0
if (src_files_age > wasm_file_age) or (extern_files_age > wasm_file_age) or forceRebuild:
print(f" |-🔧 Building {app}...")
if os.path.exists(f"apps/{app}/main.wasm"):
subprocess.run(f"cd apps/{app} && make clean", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
make_process = subprocess.Popen(f"cd apps/{app} && make", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = make_process.communicate()
if make_process.returncode != 0:
print(f" |-❌ Error building {app}.")
# write the stdout and stderr to apps/{app}/build-error.foreman.generated
with open(f"apps/{app}/build-error.foreman.generated", "w", encoding="utf-8") as f:
f.write(stdout.decode(errors="ignore"))
f.write(stderr.decode(errors="ignore"))
# remove it from the list of apps to copy to rootfs
if not persist:
exit(1)
else:
apps.remove(app)
else:
print(f" |-✅ Successfully built {app}.")
else:
print(f" |-💾 Re-using cached build of {app}...")
print("|-🚚 Moving apps to rootfs...")
# clear rootfs/Applications folder, if it exists
if os.path.exists("rootfs/Applications"):
# delete every file in the folder, but not the folder itself
for file in os.listdir("rootfs/Applications"):
os.remove(f"rootfs/Applications/{file}")
else:
# create the folder
os.makedirs("rootfs/Applications")
# Move the built apps to the rootfs
for app in apps:
# write each file to the rootfs at Applications/{app}.wasm
with open(f"apps/{app}/main.wasm", "rb") as src:
with open(f"rootfs/Applications/{app}.wasm", "wb") as dest:
shutil.copyfileobj(src, dest)
print(" |-🎉 Done!")