1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

compiles_table: reformat with black

This commit is contained in:
Sylvestre Ledru 2022-08-01 10:13:25 +02:00
parent d7a67454f6
commit 0d593f334b

View file

@ -12,8 +12,8 @@ from tqdm import tqdm
# spell-checker:ignore (libs) tqdm imap ; (shell/mac) xcrun ; (vars) nargs # spell-checker:ignore (libs) tqdm imap ; (shell/mac) xcrun ; (vars) nargs
BINS_PATH=Path("../src/uu") BINS_PATH = Path("../src/uu")
CACHE_PATH=Path("compiles_table.csv") CACHE_PATH = Path("compiles_table.csv")
TARGETS = [ TARGETS = [
# Linux - GNU # Linux - GNU
"aarch64-unknown-linux-gnu", "aarch64-unknown-linux-gnu",
@ -51,6 +51,7 @@ TARGETS = [
"x86_64-fuchsia", "x86_64-fuchsia",
] ]
class Target(str): class Target(str):
def __new__(cls, content): def __new__(cls, content):
obj = super().__new__(cls, content) obj = super().__new__(cls, content)
@ -83,13 +84,26 @@ class Target(str):
# Perform the 'it-compiles' check # Perform the 'it-compiles' check
def check(self, binary): def check(self, binary):
if self.requires_nightly(): if self.requires_nightly():
args = [ "cargo", "+nightly", "check", args = [
"-p", f"uu_{binary}", "--bin", binary, "cargo",
f"--target={self}"] "+nightly",
"check",
"-p",
f"uu_{binary}",
"--bin",
binary,
f"--target={self}",
]
else: else:
args = ["cargo", "check", args = [
"-p", f"uu_{binary}", "--bin", binary, "cargo",
f"--target={self}"] "check",
"-p",
f"uu_{binary}",
"--bin",
binary,
f"--target={self}",
]
res = subprocess.run(args, capture_output=True) res = subprocess.run(args, capture_output=True)
return res.returncode return res.returncode
@ -100,31 +114,44 @@ class Target(str):
if "ios" in self: if "ios" in self:
res = subprocess.run(["which", "xcrun"], capture_output=True) res = subprocess.run(["which", "xcrun"], capture_output=True)
if len(res.stdout) == 0: if len(res.stdout) == 0:
raise Exception("Error: IOS sdk does not seem to be installed. Please do that manually") raise Exception(
"Error: IOS sdk does not seem to be installed. Please do that manually"
)
if not self.requires_nightly(): if not self.requires_nightly():
# check std toolchains are installed # check std toolchains are installed
toolchains = subprocess.run(["rustup", "target", "list"], capture_output=True) toolchains = subprocess.run(
toolchains = toolchains.stdout.decode('utf-8').split("\n") ["rustup", "target", "list"], capture_output=True
)
toolchains = toolchains.stdout.decode("utf-8").split("\n")
if "installed" not in next(filter(lambda x: self in x, toolchains)): if "installed" not in next(filter(lambda x: self in x, toolchains)):
raise Exception(f"Error: the {t} target is not installed. Please do that manually") raise Exception(
f"Error: the {t} target is not installed. Please do that manually"
)
else: else:
# check nightly toolchains are installed # check nightly toolchains are installed
toolchains = subprocess.run(["rustup", "+nightly", "target", "list"], capture_output=True) toolchains = subprocess.run(
toolchains = toolchains.stdout.decode('utf-8').split("\n") ["rustup", "+nightly", "target", "list"], capture_output=True
)
toolchains = toolchains.stdout.decode("utf-8").split("\n")
if "installed" not in next(filter(lambda x: self in x, toolchains)): if "installed" not in next(filter(lambda x: self in x, toolchains)):
raise Exception(f"Error: the {t} nightly target is not installed. Please do that manually") raise Exception(
f"Error: the {t} nightly target is not installed. Please do that manually"
)
return True return True
def install_targets(): def install_targets():
cmd = ["rustup", "target", "add"] + TARGETS cmd = ["rustup", "target", "add"] + TARGETS
print(" ".join(cmd)) print(" ".join(cmd))
ret = subprocess.run(cmd) ret = subprocess.run(cmd)
assert(ret.returncode == 0) assert ret.returncode == 0
def get_all_bins(): def get_all_bins():
bins = map(lambda x: x.name, BINS_PATH.iterdir()) bins = map(lambda x: x.name, BINS_PATH.iterdir())
return sorted(list(bins)) return sorted(list(bins))
def get_targets(selection): def get_targets(selection):
if "all" in selection: if "all" in selection:
return list(map(Target, TARGETS)) return list(map(Target, TARGETS))
@ -132,11 +159,13 @@ def get_targets(selection):
# preserve the same order as in TARGETS # preserve the same order as in TARGETS
return list(map(Target, filter(lambda x: x in selection, TARGETS))) return list(map(Target, filter(lambda x: x in selection, TARGETS)))
def test_helper(tup): def test_helper(tup):
bin, target = tup bin, target = tup
retcode = target.check(bin) retcode = target.check(bin)
return (target, bin, retcode) return (target, bin, retcode)
def test_all_targets(targets, bins): def test_all_targets(targets, bins):
pool = multiprocessing.Pool() pool = multiprocessing.Pool()
inputs = [(b, t) for b in bins for t in targets] inputs = [(b, t) for b in bins for t in targets]
@ -148,6 +177,7 @@ def test_all_targets(targets, bins):
table[t][b] = r table[t][b] = r
return table return table
def save_csv(file, table): def save_csv(file, table):
targets = get_targets(table.keys()) # preserve order in CSV targets = get_targets(table.keys()) # preserve order in CSV
bins = list(list(table.values())[0].keys()) bins = list(list(table.values())[0].keys())
@ -156,17 +186,18 @@ def save_csv(file, table):
writer = csv.DictWriter(csvfile, fieldnames=header) writer = csv.DictWriter(csvfile, fieldnames=header)
writer.writeheader() writer.writeheader()
for t in targets: for t in targets:
d = {"target" : t} d = {"target": t}
d.update(table[t]) d.update(table[t])
writer.writerow(d) writer.writerow(d)
def load_csv(file): def load_csv(file):
table = {} table = {}
cols = [] cols = []
rows = [] rows = []
with open(file, "r") as csvfile: with open(file, "r") as csvfile:
reader = csv.DictReader(csvfile) reader = csv.DictReader(csvfile)
cols = list(filter(lambda x: x!="target", reader.fieldnames)) cols = list(filter(lambda x: x != "target", reader.fieldnames))
for row in reader: for row in reader:
t = Target(row["target"]) t = Target(row["target"])
rows += [t] rows += [t]
@ -174,26 +205,30 @@ def load_csv(file):
table[t] = dict([k, int(v)] for k, v in row.items()) table[t] = dict([k, int(v)] for k, v in row.items())
return (table, rows, cols) return (table, rows, cols)
def merge_tables(old, new): def merge_tables(old, new):
from copy import deepcopy from copy import deepcopy
tmp = deepcopy(old) tmp = deepcopy(old)
tmp.update(deepcopy(new)) tmp.update(deepcopy(new))
return tmp return tmp
def render_md(fd, table, headings: str, row_headings: Target): def render_md(fd, table, headings: str, row_headings: Target):
def print_row(lst, lens=[]): def print_row(lst, lens=[]):
lens = lens + [0] * (len(lst) - len(lens)) lens = lens + [0] * (len(lst) - len(lens))
for e, l in zip(lst, lens): for e, l in zip(lst, lens):
fmt = '|{}' if l == 0 else '|{:>%s}' % len(header[0]) fmt = "|{}" if l == 0 else "|{:>%s}" % len(header[0])
fd.write(fmt.format(e)) fd.write(fmt.format(e))
fd.write("|\n") fd.write("|\n")
def cell_render(target, bin): def cell_render(target, bin):
return "y" if table[target][bin] == 0 else " " return "y" if table[target][bin] == 0 else " "
# add some 'hard' padding to specific columns # add some 'hard' padding to specific columns
lens = [ lens = [
max(map(lambda x: len(x.os), row_headings)) + 2, max(map(lambda x: len(x.os), row_headings)) + 2,
max(map(lambda x: len(x.arch), row_headings)) + 2 max(map(lambda x: len(x.arch), row_headings)) + 2,
] ]
header = Target.get_heading() header = Target.get_heading()
header[0] = ("{:#^%d}" % lens[0]).format(header[0]) header[0] = ("{:#^%d}" % lens[0]).format(header[0])
@ -201,7 +236,7 @@ def render_md(fd, table, headings: str, row_headings: Target):
header += headings header += headings
print_row(header) print_row(header)
lines = list(map(lambda x: '-'*len(x), header)) lines = list(map(lambda x: "-" * len(x), header))
print_row(lines) print_row(lines)
for t in row_headings: for t in row_headings:
@ -209,18 +244,32 @@ def render_md(fd, table, headings: str, row_headings: Target):
row = t.get_row_heading() + row row = t.get_row_heading() + row
print_row(row) print_row(row)
if __name__ == "__main__": if __name__ == "__main__":
# create the top-level parser # create the top-level parser
parser = argparse.ArgumentParser(prog='compiles_table.py') parser = argparse.ArgumentParser(prog="compiles_table.py")
subparsers = parser.add_subparsers(help='sub-command to execute', required=True, dest="cmd") subparsers = parser.add_subparsers(
help="sub-command to execute", required=True, dest="cmd"
)
# create the parser for the "check" command # create the parser for the "check" command
parser_a = subparsers.add_parser('check', help='run cargo check on specified targets and update csv cache') parser_a = subparsers.add_parser(
parser_a.add_argument("targets", metavar="TARGET", type=str, nargs='+', choices=["all"]+TARGETS, "check", help="run cargo check on specified targets and update csv cache"
help="target-triple to check, as shown by 'rustup target list'") )
parser_a.add_argument(
"targets",
metavar="TARGET",
type=str,
nargs="+",
choices=["all"] + TARGETS,
help="target-triple to check, as shown by 'rustup target list'",
)
# create the parser for the "render" command # create the parser for the "render" command
parser_b = subparsers.add_parser('render', help='print a markdown table to stdout') parser_b = subparsers.add_parser("render", help="print a markdown table to stdout")
parser_b.add_argument("--equidistant", action="store_true", parser_b.add_argument(
help="NOT IMPLEMENTED: render each column with an equal width (in plaintext)") "--equidistant",
action="store_true",
help="NOT IMPLEMENTED: render each column with an equal width (in plaintext)",
)
args = parser.parse_args() args = parser.parse_args()
if args.cmd == "render": if args.cmd == "render":
@ -231,7 +280,7 @@ if __name__ == "__main__":
targets = get_targets(args.targets) targets = get_targets(args.targets)
bins = get_all_bins() bins = get_all_bins()
assert(all(map(Target.is_installed, targets))) assert all(map(Target.is_installed, targets))
table = test_all_targets(targets, bins) table = test_all_targets(targets, bins)
prev_table, _, _ = load_csv(CACHE_PATH) prev_table, _, _ = load_csv(CACHE_PATH)