-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Tests | ||
on: | ||
push: | ||
schedule: | ||
- cron: "* * 7 * *" | ||
|
||
jobs: | ||
check-doc: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out repository code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Check README.md | ||
run: tools/is-clean --make --root=doc README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import hashlib | ||
import os | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
whoami = "is-clean" | ||
env_prefix = whoami.upper().replace("-", "_") + "_" | ||
def env(var, default=None): | ||
return os.environ.get(env_prefix + var, default) | ||
|
||
import logging | ||
logger = logging.getLogger(whoami) | ||
|
||
def is_clean(path, make=False, root=None, target=None, show_diff=False): | ||
logger.info(f"checking: {path}") | ||
|
||
digest = "sha256" | ||
with open(path, "rb") as f: | ||
current_digest = hashlib.new(digest, f.read()).hexdigest() | ||
logger.info(f"current; {digest.upper()}: {current_digest}") | ||
|
||
if make: | ||
root = root or os.path.dirname(path) or os.getcwd() | ||
logger.debug(f"root: {root}") | ||
|
||
target = target or os.path.relpath(path, root) | ||
logger.debug(f"target: {target}") | ||
|
||
cmdline = [ os.environ.get("MAKE", "make") ] | ||
cmdline += [ "-C", root ] | ||
cmdline += [ target ] | ||
logger.debug(f"running: {cmdline}") | ||
subprocess.check_call(cmdline) | ||
|
||
with open(path, "rb") as f: | ||
generated_digest = hashlib.file_digest(f, digest).hexdigest() | ||
logger.info(f"generated; {digest.upper()}: {generated_digest}") | ||
current_digest = generated_digest | ||
|
||
with tempfile.TemporaryDirectory(prefix=f"{whoami}-") as tmp: | ||
head = os.path.join(tmp, "head") | ||
|
||
with open(head, "xb") as f: | ||
rel = os.path.relpath(path, start=root) | ||
cmdline = ["git", "show", "HEAD:./" + rel] | ||
logger.debug(f"running: {cmdline}") | ||
bs = subprocess.check_output(cmdline, cwd=root) | ||
head_digest = hashlib.new(digest, bs).hexdigest() | ||
f.write(bs) | ||
|
||
logger.info(f"HEAD; {digest.upper()}: {head_digest}") | ||
|
||
if current_digest == head_digest: | ||
return True | ||
|
||
if show_diff: | ||
subprocess.run(["diff", path, head]) | ||
|
||
return False | ||
|
||
def setup_logger(level, logger=None): | ||
level = level.upper() | ||
if logger is None: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
|
||
handler = logging.StreamHandler() | ||
handler.setLevel(level) | ||
logger.addHandler(handler) | ||
|
||
fmt = logging.Formatter(fmt="%(asctime)s:%(name)s:%(levelname)s %(message)s", datefmt="%Y-%m-%dT%H:%M:%S%z") | ||
handler.setFormatter(fmt) | ||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description="Freshness checker", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
parser.add_argument("--log", default=env("LOG_LEVEL", "INFO"), help="set log level") | ||
|
||
parser.add_argument("-m", "--make", action="store_true") | ||
parser.add_argument("-r", "--root", metavar="PATH") | ||
parser.add_argument("-t", "--target", metavar="TARGET") | ||
|
||
parser.add_argument("-D", "--no-diff", action="store_true") | ||
|
||
parser.add_argument("path", metavar="PATH") | ||
|
||
return parser.parse_args() | ||
|
||
def main(): | ||
args = parse_args() | ||
setup_logger(args.log, logger) | ||
logger.debug(f"args: {args}") | ||
|
||
r = is_clean( | ||
path = args.path, | ||
make = args.make, | ||
root = args.root, | ||
target = args.target, | ||
show_diff = not args.no_diff, | ||
) | ||
|
||
if not r: | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() |