git » kde5-pkgbuilds.git » main » tree

[main] / build-in-order.py

#!/usr/bin/env python3

# BootStrapBot (lol)
# BuildSystemBot
# BullShitBot :P


import os
import sys
from time import strftime
import subprocess
from subprocess import DEVNULL, PIPE, STDOUT

class Return:
    def __init__(self, rv, out=None):
        self.rv = rv
        self.out = out
    def __bool__(self):
        return self.rv == 0
    def __int__(self):
        return self.rv
    def __str__(self):
        if self.out:
            return self.out
        return str(self.rv)

def sub(*args, **kwargs):
    c = subprocess.run(*args, **kwargs)
    return Return(c.returncode, c.stdout)

def subc(*args, **kwargs):
    c = subprocess.run(*args, **kwargs)
    if c.returncode != 0:
        print("subprocess failed: ", args)
        print("code:", c.returncode)
        sys.exit(1)
    return Return(c.returncode, c.stdout)


def subtea(cmds, log):
    if not isinstance(cmds[0], list):
        cmds = [cmds]
    teecmd = ["tee", log ]
    for cmd in cmds:
        proc = subprocess.Popen(cmd, stdin=DEVNULL, stderr=STDOUT, stdout=PIPE)
        tee = subprocess.Popen(teecmd, stdin=proc.stdout)
        r1 = proc.wait()
        tee.wait()
        if r1:
            print("subprocess failed: ", cmd)
            print("code:", r1)
            sys.exit(1)
        teecmd = ["tee", "-a", log ]

if len(sys.argv) != 2:
    print(f"Usage: {sys.argv[0]} <list|directory>")
    sys.exit(0)

try:
    with open(sys.argv[1], 'r') as pkgf:
        pkgs = pkgf.readlines()
except FileNotFoundError:
    if ' ' in sys.argv[1]:
        pkgs = [ sys.argv[1] ]
    else:
        raise
except IsADirectoryError:
    pkgs = [ sys.argv[1] ]

basedir = os.getcwd()
logdir = strftime("logs-%y%m%d-") + str(os.getpid())
os.mkdir(logdir)


for pkgline in pkgs:
    pl = pkgline.rstrip().split(' ',maxsplit=1)
    pkg = ' '.join(pl)

    os.chdir(basedir)
    print(f"{strftime('%H:%M:%S')} Build {' '.join(pl)}")

    logfn = logdir + '/' + '-'.join(pl)  + ".log"
    with open(logfn, "w") as lf:
        bcmd = [ "./mpkg.sh" ] + pl
        print(f"Building (log: '{logfn}')..")
        bres = sub(bcmd, stdin=DEVNULL, stderr=STDOUT, stdout=lf)

    if not bres:
        print(f"Build {pkg} failed. Tail:")
        sub(["tail", "-n", "100", logfn])
        print(f"Tail over. Build {pkg} failed.")
        sys.exit(1)

    print("Done.")