Skip to content

Commit

Permalink
Randomize author order on CI builds
Browse files Browse the repository at this point in the history
See discussion in manubot/manubot#214
  • Loading branch information
dhimmel committed Mar 6, 2020
1 parent 5e9aa01 commit 6fed939
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export TZ=Etc/UTC
# Default Python to read/write text files using UTF-8 encoding
export LC_ALL=en_US.UTF-8

# Randomize authors in metadata.yaml
python build/randomize-authors.py --shuffle --only-on-ci

# Generate reference information
echo >&2 "Retrieving and processing reference metadata"
manubot process \
Expand Down
82 changes: 82 additions & 0 deletions build/randomize-authors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import argparse
import os
import pathlib
import random
import subprocess
import sys

import yaml
from manubot.util import read_serialized_data


def parse_args():
parser = argparse.ArgumentParser(
description="Randomize metadata.authors. Ovewrites metadata.yaml"
)
parser.add_argument(
"--path", default="content/metadata.yaml", help="path to metadata.yaml"
)
parser.add_argument(
"--shuffle",
action="store_true",
help="shuffle authors using HEAD commit as random seed",
)
parser.add_argument(
"--only-on-ci",
action="store_true",
help="do nothing if CI environment variable is not true",
)
args = parser.parse_args()
vars(args)["execute"] = True
if args.only_on_ci:
vars(args)["execute"] = os.environ.get("CI", "false").lower() == "true"
return args


def shuffle(x, seed=None):
"""
shuffle x in-place using seed
"""
random.Random(seed).shuffle(x)


def get_head_commit():
args = ["git", "rev-parse", "HEAD"]
try:
return subprocess.check_output(args)
except subprocess.CalledProcessError:
return None


def dump_yaml(obj, path):
path = pathlib.Path(path)
sys.stderr.write(f"Dumping YAML to {path}\n")
with path.open("w", encoding="utf-8") as write_file:
yaml.dump(
obj,
write_file,
# default_flow_style=False,
explicit_start=True,
explicit_end=True,
width=float("inf"),
sort_keys=False,
allow_unicode=True,
)
write_file.write("\n")


if __name__ == "__main__":
"""
Alternative to https://github.com/manubot/manubot/pull/214
"""
args = parse_args()
if not args.execute:
sys.stderr.write("Exiting without doing anything due to --only-on-ci\n")
sys.exit()
metadata = read_serialized_data(args.path)
authors = metadata.get("authors", [])
if args.shuffle:
sys.stderr.write("Shuffling metadata.authors\n")
seed = get_head_commit()
shuffle(authors, seed=seed)
dump_yaml(metadata, args.path)

0 comments on commit 6fed939

Please sign in to comment.