#!python3 import re import csv import sys import shlex import subprocess as sp # import time prune_warining = "warning: There are too many unreachable loose objects; run 'git prune' to remove them." # List for series entries series = [] # These lists will contain merged and unmnerged series data. merged = [] unmerged = [] # option that we will be operating upon, series or the patch # this is the command line argument to git-pw # for example "git-pw patch apply 12345" or "git-pw series apply "12356" type_ = "series" # Get the csv data from stdin csv_data = [] for line in sys.stdin: if not '"ID","Date","Name","Version","Submitter"' in line: print(line) csv_data.append(line.strip()) # parse the csv entries def read_rows(csvfile): spamreader = csv.reader(csvfile, delimiter=",", quotechar='"') for row in spamreader: # print(row) if not row: return if row and row[1] != "ID": series.append(row) def get_output(cmnd): """ Execute command and check the output, if git throws a warning saying "warning: There are too many unreachable loose objects; run 'git prune' to remove them." `git prune` will be executed. otherwise output will be printed and exit code will be returned. """ try: output = sp.check_output( cmnd, stderr=sp.STDOUT, shell=True, universal_newlines=True ) except sp.CalledProcessError as exc: print("Status : FAIL", exc.returncode, exc.output) return exc.returncode else: print("Output: \n{}\n".format(output)) if prune_warining in output: print("running git prune") get_output("git prune") return 0 def write_file(filename, list_): """ This function is used to write the IDs for patches/series that are merged/unmerged after we have processed everything. """ with open(filename, "w") as f: for i in list_: f.write(i[0] + "\n") if __name__ == "__main__": read_rows(csv_data) # this is crappy, it will be replaced by arg parser. if len(sys.argv) >= 3: if sys.argv[1] == "series" or sys.argv[1] == "patch": type_ = sys.argv[1] if sys.argv[2] == "apply": print("applying ", type_) if series: for i in series: try: print("trying to apply:", i[0], i[1], i[2]) if i[0] == "ID": pass ret = get_output(f"git-pw {type_} apply {i[0]}") print("git exit code: ", ret) # time.sleep(0.5) if ret: # if `git-pw patch/series apply ` fails # resetting to HEAD print("resetting...") get_output("git reset --hard HEAD") get_output("git am --abort") unmerged.append(i) else: merged.append(i) except KeyboardInterrupt as ke: break print("total merged: {0}, total unmerged {1}".format(len(merged), len(unmerged))) write_file("merged.txt", merged) write_file("unmerged.txt", unmerged)