DepperDan/build_functions/build_utils.py

38 lines
1.1 KiB
Python

# (c) 2025 by Stephan Menzel
# Licensed under the Apache License, Version 2.0.
# See attached file LICENSE for full details
import datetime
import os
import subprocess
logfile = 'build.log'
def file_and_console_log(*args, **kwargs):
print(*args, **kwargs)
with open(logfile, 'a') as f:
f.write(*args)
f.write('\n')
def print_banner(text: str):
file_and_console_log("#########################################################")
file_and_console_log(" " + text)
file_and_console_log("#########################################################")
def run_in_shell(*args, **kwargs):
cmd = ' '.join(args)
print("> " + cmd)
with open(logfile, 'a') as f:
f.write('[cwd] ' + os.getcwd() + '\n')
f.write(cmd + '\n')
try:
start = datetime.datetime.now()
subprocess.run(*args, **kwargs, shell=True, check=True)
with open(logfile, 'a') as f:
f.write(f' took {(datetime.datetime.now() - start).total_seconds()} seconds.\n\n')
except Exception as e:
file_and_console_log("Error: " + str(e))
raise e