1#!/usr/bin/env python3
2
3import logging
4import os
5import subprocess
6import sys
7from glob import glob
8
9from patches_constants import PATCHES_DIR
10
11def patch():
12    if os.path.isfile("patched"):
13        sys.exit()
14
15    applied_patches = []
16    failed_patches = []
17    for tmpfile in glob(os.path.join(PATCHES_DIR, "*.patch")):
18        print("patch", tmpfile)
19        result = subprocess.run(["git", "apply", "--ignore-space-change", "--ignore-whitespace", tmpfile],
20                                cwd=os.path.join("..", "..", "..", ".."))
21        if result.returncode:
22            failed_patches.append(tmpfile)
23            logging.error("patching failed: %s", tmpfile)
24        else:
25            applied_patches.append(tmpfile)
26
27    with open(os.path.join(PATCHES_DIR, "patched"), "w") as outp:
28        print("Success:", file=outp)
29        print("\n".join(map(lambda x: "\t" + x, applied_patches)), file=outp)
30
31        print("Failure:", file=outp)
32        print("\n".join(map(lambda x: "\t" + x, failed_patches)), file=outp)
33
34
35if __name__ == "__main__":
36    patch()
37