Skip to content

Commit

Permalink
explicitly control stderr vs stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed May 22, 2024
1 parent 2c0c00b commit 98a4962
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions scripts/rust_ci_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
import subprocess
import os
from collections import defaultdict
import sys


def stderr(s):
print(s, file=sys.stderr)


def stdout(s):
print(s)


def run(command):
assert isinstance(command, str)
print(f"Running: {command}")
stderr(f"Running: {command}")
exit_code = subprocess.check_call(command, shell=True, text=True)
assert exit_code == 0


def run_with_stdout(command):
assert isinstance(command, str)
print(f"Running: {command}")
stderr(f"Running: {command}")
cmd_output = subprocess.check_output(command, shell=True, text=True)
return cmd_output

Expand All @@ -28,6 +37,7 @@ def predicate(package):

return [p for p in workspace_crates if predicate(p)]


def find_unsupported_platform_crates(*, host_platform, workspace_crates):
def predicate(package):
tmp = package.get("metadata") or {}
Expand Down Expand Up @@ -68,10 +78,10 @@ def build_all_crates(*, cargo_profile, targets):


def run_cargo_deb(*, out_dir, cargo_profile, targets, crate):
crate_name = crate['name']
crate_name = crate["name"]
out = os.path.join(out_dir, crate_name)
os.makedirs(out, exist_ok=True)
print(f"Creating .deb packages for {crate_name} and copying to {out}:")
stderr(f"Creating .deb packages for {crate_name} and copying to {out}:")
for t in targets:
run(
f"cargo deb --no-build --no-strip "
Expand All @@ -81,22 +91,24 @@ def run_cargo_deb(*, out_dir, cargo_profile, targets, crate):
f"-o {out}/{crate_name}_{t}.deb"
)


def get_binaries(*, workspace_crates):
"""returns map of crate name to set of binaries for that crate"""
binaries = defaultdict(lambda: [])
for c in workspace_crates:
for t in c['targets']:
if t['kind'] != ['bin']:
for t in c["targets"]:
if t["kind"] != ["bin"]:
continue
binaries[c['name']].append(t['name'])
return {k: set(v) for k, v in binaries.items() }
binaries[c["name"]].append(t["name"])
return {k: set(v) for k, v in binaries.items()}


def copy_cargo_binaries(*, out_dir, cargo_profile, targets, workspace_crates):
wksp_binaries = get_binaries(workspace_crates=workspace_crates)
for crate_name, binaries in wksp_binaries.items():
out = os.path.join(out_dir, crate_name)
os.makedirs(out, exist_ok=True)
print(f"Copying binaries for {crate_name} to {out}:")
stderr(f"Copying binaries for {crate_name} to {out}:")
for t in targets:
target_dir = f"target/{t}-unknown-linux-gnu/{cargo_profile}"
for b in binaries:
Expand All @@ -106,6 +118,7 @@ def copy_cargo_binaries(*, out_dir, cargo_profile, targets, workspace_crates):
f"{out}/{b}_{t}"
)


def main():
parser = argparse.ArgumentParser(description="Scripts for Rust in CI")
subparsers = parser.add_subparsers()
Expand Down Expand Up @@ -136,20 +149,25 @@ def main():
def subcmd_build_linux_artifacts(args):
"""entry point for `build_linux_artifacts` subcommand"""
targets = ["aarch64", "x86_64"]
print("building all crates")
stderr("building all crates")
build_all_crates(cargo_profile=args.cargo_profile, targets=targets)

wksp_crates = workspace_crates()
deb_crates = find_cargo_deb_crates(workspace_crates=wksp_crates)
print(f"Running cargo deb for: {[c['name'] for c in deb_crates]}")
stderr(f"Running cargo deb for: {[c['name'] for c in deb_crates]}")
for crate in deb_crates:
run_cargo_deb(
out_dir=args.out_dir,
cargo_profile=args.cargo_profile,
targets=targets,
crate=crate,
)
copy_cargo_binaries(workspace_crates=wksp_crates, targets=targets, out_dir=args.out_dir, cargo_profile=args.cargo_profile)
copy_cargo_binaries(
workspace_crates=wksp_crates,
targets=targets,
out_dir=args.out_dir,
cargo_profile=args.cargo_profile,
)


def subcmd_excludes(args):
Expand All @@ -159,7 +177,7 @@ def subcmd_excludes(args):
excludes = find_unsupported_platform_crates(
host_platform=host, workspace_crates=wksp_crates
)
print(" ".join(sorted([c for c in excludes])))
stdout(" ".join(sorted([c for c in excludes])))


if __name__ == "__main__":
Expand Down

0 comments on commit 98a4962

Please sign in to comment.