|
4 | 4 | It is NOT part of the Hondana API or for public use. No support will be provided for this file. Use at your own risk. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import argparse |
7 | 8 | import asyncio |
8 | 9 | import pathlib |
9 | | -import subprocess |
| 10 | +import subprocess # noqa: S404 |
10 | 11 | import sys |
11 | 12 |
|
12 | 13 | import hondana |
13 | 14 |
|
14 | 15 | TAG_PATH = pathlib.Path("./hondana/extras/tags.json") |
15 | 16 | REPORT_PATH = pathlib.Path("./hondana/extras/report_reasons.json") |
16 | 17 |
|
17 | | -client = hondana.Client() |
18 | 18 |
|
| 19 | +class ProgramNamespace(argparse.Namespace): |
| 20 | + tags: bool |
| 21 | + reports: bool |
19 | 22 |
|
20 | | -def __update_tags() -> None: |
21 | | - asyncio.run(client.update_tags()) |
22 | | - asyncio.run(client.close()) |
| 23 | + def _parsed(self) -> bool: |
| 24 | + return any([self.tags, self.reports]) |
23 | 25 |
|
24 | | - diff = subprocess.run(["git", "diff", "--exit-code", str(TAG_PATH)], capture_output=False) |
25 | | - sys.exit(diff.returncode) |
26 | 26 |
|
| 27 | +parser = argparse.ArgumentParser(description="Small pre-flight CI script for hondana") |
| 28 | +parser.add_argument("-t", "--tags", action="store_true", dest="tags", help="Whether to run the 'update tags' action.") |
| 29 | +parser.add_argument( |
| 30 | + "-r", "--reports", action="store_true", dest="reports", help="Whether to run the 'update report reasons' action." |
| 31 | +) |
27 | 32 |
|
28 | | -def __update_report_reasons() -> None: |
29 | | - asyncio.run(client.update_report_reasons()) |
30 | | - asyncio.run(client.close()) |
31 | 33 |
|
32 | | - diff = subprocess.run(["git", "diff", "--exit-code", str(REPORT_PATH)], capture_output=False) |
33 | | - sys.exit(diff.returncode) |
| 34 | +async def __update_tags(client: hondana.Client, /) -> int: |
| 35 | + await client.update_tags() |
| 36 | + |
| 37 | + prog: asyncio.subprocess.Process = await asyncio.create_subprocess_exec("git", "diff", "--exit-code", str(REPORT_PATH)) |
| 38 | + return await prog.wait() |
| 39 | + |
| 40 | + |
| 41 | +async def __update_report_reasons(client: hondana.Client, /) -> int: |
| 42 | + await client.update_report_reasons() |
| 43 | + |
| 44 | + prog: asyncio.subprocess.Process = await asyncio.create_subprocess_exec( |
| 45 | + "git", |
| 46 | + "diff", |
| 47 | + "--exit-code", |
| 48 | + str(REPORT_PATH), |
| 49 | + ) |
| 50 | + return await prog.wait() |
| 51 | + |
| 52 | + |
| 53 | +async def main(args: ProgramNamespace) -> None: |
| 54 | + if not args._parsed(): |
| 55 | + msg = "At least one argument must be specified." |
| 56 | + raise RuntimeError(msg) |
| 57 | + |
| 58 | + client = hondana.Client() |
| 59 | + |
| 60 | + if args.tags: |
| 61 | + ret = await __update_tags(client) |
| 62 | + if ret == 1: |
| 63 | + msg = "Tags updated." |
| 64 | + raise RuntimeError(msg) |
| 65 | + if args.reports: |
| 66 | + ret = await __update_report_reasons(client) |
| 67 | + if ret == 1: |
| 68 | + msg = "Reports updated." |
| 69 | + raise RuntimeError(msg) |
| 70 | + |
| 71 | + await client.close() |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + args = parser.parse_args(namespace=ProgramNamespace()) |
| 76 | + asyncio.run(main(args)) |
0 commit comments