|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | +import subprocess |
| 7 | +from argparse import Namespace |
| 8 | +from subprocess import CalledProcessError |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | + |
| 12 | +def resync_specs(directory: pathlib.Path, errored: dict[str, str]) -> None: |
| 13 | + """Actually sync the specs""" |
| 14 | + print("Beginning to sync specs") # noqa: T201 |
| 15 | + for spec in os.scandir(directory): |
| 16 | + if not spec.is_dir(): |
| 17 | + continue |
| 18 | + |
| 19 | + if spec.name in ["asynchronous"]: |
| 20 | + continue |
| 21 | + try: |
| 22 | + subprocess.run( |
| 23 | + ["bash", "./.evergreen/resync-specs.sh", spec.name], # noqa: S603, S607 |
| 24 | + capture_output=True, |
| 25 | + text=True, |
| 26 | + check=True, |
| 27 | + ) |
| 28 | + except CalledProcessError as exc: |
| 29 | + errored[spec.name] = exc.stderr |
| 30 | + print("Done syncing specs") # noqa: T201 |
| 31 | + |
| 32 | + |
| 33 | +def apply_patches(): |
| 34 | + print("Beginning to apply patches") # noqa: T201 |
| 35 | + subprocess.run(["bash", "./.evergreen/remove-unimplemented-tests.sh"], check=True) # noqa: S603, S607 |
| 36 | + subprocess.run(["git apply -R --allow-empty ./.evergreen/spec-patch/*"], shell=True, check=True) # noqa: S602, S607 |
| 37 | + |
| 38 | + |
| 39 | +def check_new_spec_directories(directory: pathlib.Path) -> list[str]: |
| 40 | + """Check to see if there are any directories in the spec repo that don't exist in pymongo/test""" |
| 41 | + spec_dir = pathlib.Path(os.environ["MDB_SPECS"]) / "source" |
| 42 | + spec_set = { |
| 43 | + entry.name.replace("-", "_") |
| 44 | + for entry in os.scandir(spec_dir) |
| 45 | + if entry.is_dir() |
| 46 | + and (pathlib.Path(entry.path) / "tests").is_dir() |
| 47 | + and len(list(os.scandir(pathlib.Path(entry.path) / "tests"))) > 1 |
| 48 | + } |
| 49 | + test_set = {entry.name.replace("-", "_") for entry in os.scandir(directory) if entry.is_dir()} |
| 50 | + known_mappings = { |
| 51 | + "ocsp_support": "ocsp", |
| 52 | + "client_side_operations_timeout": "csot", |
| 53 | + "mongodb_handshake": "handshake", |
| 54 | + "load_balancers": "load_balancer", |
| 55 | + "atlas_data_lake_testing": "atlas", |
| 56 | + "connection_monitoring_and_pooling": "connection_monitoring", |
| 57 | + "command_logging_and_monitoring": "command_logging", |
| 58 | + "initial_dns_seedlist_discovery": "srv_seedlist", |
| 59 | + "server_discovery_and_monitoring": "sdam_monitoring", |
| 60 | + } |
| 61 | + |
| 62 | + for k, v in known_mappings.items(): |
| 63 | + if k in spec_set: |
| 64 | + spec_set.remove(k) |
| 65 | + spec_set.add(v) |
| 66 | + return list(spec_set - test_set) |
| 67 | + |
| 68 | + |
| 69 | +def write_summary(errored: dict[str, str], new: list[str], filename: Optional[str]) -> None: |
| 70 | + """Generate the PR description""" |
| 71 | + pr_body = "" |
| 72 | + process = subprocess.run( |
| 73 | + ["git diff --name-only | awk -F'/' '{print $2}' | sort | uniq"], # noqa: S607 |
| 74 | + shell=True, # noqa: S602 |
| 75 | + capture_output=True, |
| 76 | + text=True, |
| 77 | + check=True, |
| 78 | + ) |
| 79 | + succeeded = process.stdout.strip().split() |
| 80 | + if len(succeeded) > 0: |
| 81 | + pr_body += "The following specs were changed:\n -" |
| 82 | + pr_body += "\n -".join(succeeded) |
| 83 | + pr_body += "\n" |
| 84 | + if len(errored) > 0: |
| 85 | + pr_body += "\n\nThe following spec syncs encountered errors:\n -" |
| 86 | + for k, v in errored.items(): |
| 87 | + pr_body += f"\n -{k}\n```{v}\n```" |
| 88 | + pr_body += "\n" |
| 89 | + if len(new) > 0: |
| 90 | + pr_body += "\n\nThe following directories are in the specification repository and not in our test directory:\n -" |
| 91 | + pr_body += "\n -".join(new) |
| 92 | + pr_body += "\n" |
| 93 | + if pr_body != "": |
| 94 | + if filename is None: |
| 95 | + print(f"\n{pr_body}") # noqa: T201 |
| 96 | + else: |
| 97 | + with open(filename, "w") as f: |
| 98 | + # replacements made for proper json |
| 99 | + f.write(pr_body.replace("\n", "\\n").replace("\t", "\\t")) |
| 100 | + |
| 101 | + |
| 102 | +def main(args: Namespace): |
| 103 | + directory = pathlib.Path("./test") |
| 104 | + errored: dict[str, str] = {} |
| 105 | + resync_specs(directory, errored) |
| 106 | + apply_patches() |
| 107 | + new = check_new_spec_directories(directory) |
| 108 | + write_summary(errored, new, args.filename) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + parser = argparse.ArgumentParser( |
| 113 | + description="Python Script to resync all specs and generate summary for PR." |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "--filename", help="Name of file for the summary to be written into.", default=None |
| 117 | + ) |
| 118 | + args = parser.parse_args() |
| 119 | + main(args) |
0 commit comments