emnux/x.py

117 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python
import sys
import subprocess
import threading
import os
__FILENAME = ""
MAX_JOBS = 4
CWD = os.curdir
BUILD_D = f"{CWD}/build"
def shift(arr):
if not arr:
return None # or raise IndexError if you prefer
return arr.pop(0)
def run(*cmds, parallel=False, tag=None, interactive=False):
threads = []
def run_single(cmd, tag):
print(f"CMD: {cmd}")
with subprocess.Popen(
cmd, shell=True,
stdout=None if interactive else subprocess.PIPE,
stderr=None if interactive else subprocess.STDOUT,
text=None if interactive else True,
bufsize=1
) as proc:
if not interactive:
for line in proc.stdout:
line = line.rstrip("\n")
if tag:
print(f"stdout ({tag}): {line}", flush=True)
else:
print(f"stdout: {line}", flush=True)
proc.wait()
if proc.returncode != 0:
raise subprocess.CalledProcessError(proc.returncode, cmd)
for i, command in enumerate(cmds):
current_tag = f"{tag}-{i}" if tag else None
if parallel:
t = threading.Thread(
target=run_single, args=(command, current_tag))
t.start()
threads.append(t)
else:
run_single(command, current_tag)
for t in threads:
t.join()
def cmd_configure(args):
def cmd_help():
print(f"Usage: {__FILENAME} configure [component]")
print("Components:")
print(" kernel - The linux kernel")
print(" busybox - Busybox")
# $(MAKE) -C $(MAKEFILE_DIR)/kernel O=$(KERNEL_BUILD_DIR) KCONFIG_CONFIG="$(MAKEFILE_DIR)/.kernel-config" menuconfig
subc = shift(args)
if not subc:
cmd_help()
exit(1)
elif subc == "kernel":
run(f"make -C {CWD}/linux/kernel O={BUILD_D}/linux/kernel KCONFIG_CONFIG='{
CWD}/linux/.kernel-config' menuconfig", interactive=True)
elif subc == "busybox":
run(f"make -C {CWD}/linux/busybox O={BUILD_D}/linux/busybox KCONFIG_CONFIG='{
CWD}/linux/.busybox-config' menuconfig", interactive=True)
else:
print(f"ERROR: Unknown component {subc}")
cmd_help()
exit(1)
AVAILABLE_SUBCOMMANDS = {
"help": help,
"configure": cmd_configure,
}
def help():
print(f"Usage: {__FILENAME} [subcommand]")
print("Subcommands:")
print(" help - Show this help")
print(" configure - Configure components")
if __name__ == "__main__":
args = sys.argv
__FILENAME = shift(args)
cmd = shift(args)
if not cmd:
help()
exit(1)
cmd_v = AVAILABLE_SUBCOMMANDS[cmd]
if not cmd_v:
print(f"ERROR: Unknown subcommand '{cmd}'")
help()
exit(1)
(cmd_v)(args)
exit(0)