-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
352 lines (303 loc) · 10.6 KB
/
main.py
File metadata and controls
352 lines (303 loc) · 10.6 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import concurrent.futures
import logging
import os
import shutil
import subprocess
from threading import Thread
from dotenv import load_dotenv
from logfmter import Logfmter
logger = logging.getLogger("appstream-generator")
handler = logging.StreamHandler()
handler.setFormatter(Logfmter(keys=["level", "name"], mapping={"level": "levelname"}))
logging.basicConfig(handlers=[handler], level=logging.DEBUG)
# logging.error({"token": "Hello, World!"}) # at=ERROR token="Hello, World!"
# should be self-explanatory lol
# note: this is for DX, not really needed
_dotenv = load_dotenv()
required_envars = ["BASE_DIR", "OUTPUT_DIR"]
missing_vars = [var for var in required_envars if var not in os.environ]
if missing_vars:
raise EnvironmentError(
f"Missing required environment variable(s): {', '.join(missing_vars)}"
)
base_dir = os.environ["BASE_DIR"]
out_dir = os.environ["OUTPUT_DIR"]
# Limits N number of old entries
old_limit = int(os.environ.get("OLD_LIMIT", 5))
# Max concurrent tasks
max_workers = int(os.environ.get("MAX_WORKERS", 5))
def scan_base_dir(base_dir: str):
for entry in os.scandir(base_dir):
if entry.is_dir():
if entry.path == out_dir:
continue
yield entry.path
def format_output_path(out_dir: str, repo_name: str):
from datetime import datetime
date_str = datetime.now().strftime("%Y%m%d%H%M")
return os.path.join(out_dir, repo_name, date_str)
# What this does is:
# - Scan the base directory for subdirectories
# - run appstream-builder on them and output to formatted dir
# - outputs are formatted in <repo>/<date>
# - latest run gets symlinked to latest
def log_stream(stream, logger_obj):
"""Read from stream and log each flush (buffered output)"""
buffer = ""
xml_buffer = []
in_xml = False
xml_level = logging.INFO
while True:
chunk = stream.read(4096)
if not chunk:
break
buffer += chunk
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
line = line.rstrip()
if line:
# Check if this line starts XML output
if "<components" in line:
in_xml = True
xml_buffer = []
# Parse log level from the line before XML starts
xml_level = logging.INFO
if "DEBUG:" in line:
xml_level = logging.DEBUG
elif "WARNING:" in line:
xml_level = logging.WARNING
elif "ERROR:" in line:
xml_level = logging.ERROR
elif "CRITICAL:" in line:
xml_level = logging.CRITICAL
xml_buffer.append(line)
elif in_xml:
xml_buffer.append(line)
# Check if XML is complete
if "</components>" in line:
# Combine all XML lines into one
combined_xml = " ".join(xml_buffer)
logger_obj.log(xml_level, combined_xml)
in_xml = False
xml_buffer = []
else:
# Parse log level from message
level = logging.INFO # default
if "DEBUG:" in line:
level = logging.DEBUG
elif "WARNING:" in line:
level = logging.WARNING
elif "ERROR:" in line:
level = logging.ERROR
elif "CRITICAL:" in line:
level = logging.CRITICAL
logger_obj.log(level, line)
# Log any remaining buffered output
if in_xml and xml_buffer:
combined_xml = " ".join(xml_buffer)
logger_obj.log(xml_level, combined_xml)
elif buffer.strip():
logger_obj.info(buffer.strip())
stream.close()
def build_appstream(path: str, output_dir: str):
repo_name = os.path.basename(path)
logger = logging.getLogger(f"builder-{repo_name}")
appstream_builder = "appstream-builder"
args = [
"--verbose",
"--veto-ignore=missing-parents",
"--veto-ignore=missing-info",
"--output-dir",
f"{output_dir}/appstream",
"--temp-dir",
"/tmp/appstream",
"--icons-dir",
f"{output_dir}/icons",
"--cache-dir",
f"{output_dir}/cache",
"--basename",
repo_name,
"--log-dir",
f"{output_dir}/logs",
"--include-failed",
"--origin",
"terra",
"--packages-dir",
path,
"--old-metadata",
f"{output_dir}../latest/appstream",
]
full_args = [appstream_builder] + args
try:
process = subprocess.Popen(
full_args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
)
stdout_thread = Thread(
target=log_stream,
args=(
process.stdout,
logger,
),
)
stderr_thread = Thread(
target=log_stream,
args=(
process.stderr,
logger,
),
)
stdout_thread.start()
stderr_thread.start()
process.wait()
stdout_thread.join()
stderr_thread.join()
except Exception as e:
logger.error(f"Error running appstream-builder: {e}")
raise
# finally:
# return output_dir
# now go to output_dir/icons
screenshots_dir = os.path.join(output_dir, "icons")
logger.info(f"Checking for screenshots in {screenshots_dir}")
if os.path.exists(screenshots_dir):
for dir in os.scandir(screenshots_dir):
logger.info(f"found screenshots dir at {dir.path}")
if dir.is_dir():
proc = subprocess.Popen(
[
"tar",
"-C",
dir.path,
"-czf",
os.path.join(
output_dir,
"appstream",
f"{repo_name}-icons-{dir.name}.tar.gz",
),
".",
"--strip-components=2",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
stdout_thread = Thread(
target=log_stream,
args=(
proc.stdout,
logger,
),
)
stderr_thread = Thread(
target=log_stream,
args=(
proc.stderr,
logger,
),
)
stdout_thread.start()
stderr_thread.start()
process.wait()
stdout_thread.join()
stderr_thread.join()
return output_dir
def cleanup_old_composes(basedir: str):
composes = sorted(
[
d
for d in os.listdir(basedir)
if os.path.isdir(os.path.join(basedir, d))
and not os.path.islink(os.path.join(basedir, d))
],
reverse=True,
)
logger.info(
{
"msg": "Found composes",
"compose_count": len(composes),
"operation": "cleanup",
"compose_list": composes,
},
)
# Now keep only N most recent, delete the rest
# don't delete symlinks or non-dirs
to_delete = composes[old_limit:]
if not to_delete:
logger.info(
{
"msg": f"No old composes to delete, keeping all {len(composes)}",
"operation": "cleanup",
}
)
return
else:
logger.info(
{
"msg": f"Keeping {old_limit} most recent composes, removing {len(to_delete)} old ones",
"operation": "cleanup-delete",
}
)
for dir in to_delete:
full_path = os.path.join(basedir, dir)
try:
logger.info(
{
"msg": f"Removing old compose at {full_path}",
"operation": "cleanup-delete",
"path": full_path,
}
)
shutil.rmtree(full_path)
except Exception as e:
logger.error(f"Error removing old compose at {full_path}: {e}")
def process_repo(path: str):
# shadow global logger, we're gonna do our own thing
repo_name = os.path.basename(path)
logger = logging.getLogger(repo_name)
out_base_dir = os.path.join(out_dir, repo_name)
logger.info(f"Processing repo at {path}")
compose_out = format_output_path(out_dir, repo_name)
logger.info(f"Outputting to {compose_out}")
try:
# hack: symlink os/repodata to repodata in the path
repodata_src = os.path.join(path, "repodata")
os_dir = os.path.join(path, "os")
os.makedirs(os_dir, exist_ok=True)
os_repodata_link = os.path.join(os_dir, "repodata")
if not os.path.exists(os_repodata_link) and os.path.exists(repodata_src):
logger.info(f"Linking {os_repodata_link} to {repodata_src}")
os.symlink(repodata_src, os_repodata_link)
# end symlink
os.makedirs(compose_out, exist_ok=True)
output = build_appstream(path, compose_out)
latest_path = os.path.join(out_base_dir, "latest")
logger.info(f"Linking latest to {latest_path}")
# ln -sf
if os.path.islink(latest_path) or os.path.exists(latest_path):
os.remove(latest_path)
# Create a relative symlink from latest_path to output
rel_output = os.path.relpath(output, os.path.dirname(latest_path))
os.symlink(rel_output, latest_path)
logger.info(f"Symlinked latest to {latest_path} (-> {rel_output})")
cleanup_old_composes(out_base_dir)
except Exception as e:
logger.error(f"Error processing repo {repo_name}: {e}")
return
finally:
return True
def main():
# setup_logging()
# logger.info("test")
thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=max_workers)
futures = []
for dir in scan_base_dir(base_dir):
futures.append(thread_pool.submit(process_repo, dir))
_ = concurrent.futures.wait(futures)
thread_pool.shutdown(wait=True)
thread_pool = None
if __name__ == "__main__":
main()