Skip to content

Commit 33d32df

Browse files
authored
Merge pull request #274 from tekktrik/dev/cli-tools
Turn some tools into CLI tools
2 parents a476ee6 + 416312d commit 33d32df

File tree

4 files changed

+122
-13
lines changed

4 files changed

+122
-13
lines changed

tools/ci_status.py

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"""
1616

1717
from typing import Optional
18+
import argparse
1819
from github.Repository import Repository
1920
from github.Workflow import Workflow
2021
from github.GithubException import GithubException
21-
from tools.library_functions import StrPath
22-
from tools.iterate_libraries import (
22+
from library_functions import StrPath
23+
from iterate_libraries import (
2324
iter_remote_bundle_with_func,
2425
RemoteLibFunc_IterResult,
2526
)
@@ -28,22 +29,27 @@
2829
def run_gh_rest_check(
2930
lib_repo: Repository,
3031
user: Optional[str] = None,
32+
branch: Optional[str] = None,
3133
workflow_filename: Optional[str] = "build.yml",
3234
) -> str:
3335
"""Uses ``PyGithub`` to check the CI status of a repository
3436
3537
:param Repository lib_repo: The repo as a github.Repository.Repository object
3638
:param str|None user: The user that triggered the run; if `None` is
3739
provided, any user is acceptable
40+
:param str|None branch: The branch name to specifically check; if `None` is
41+
provided, all branches are allowed; this is the default
3842
:param str|None workflow_filename: The filename of the workflow; if `None` is
39-
provided, any workflow name is acceptable; the default is `"build.yml"`
43+
provided, any workflow name is acceptable; the default is ``"build.yml"``
4044
:return: The requested runs conclusion
4145
:rtype: str
4246
"""
4347

4448
arg_dict = {}
4549
if user is not None:
4650
arg_dict["actor"] = user
51+
if branch is not None:
52+
arg_dict["branch"] = branch
4753

4854
workflow: Workflow = lib_repo.get_workflow(workflow_filename)
4955
workflow_runs = workflow.get_runs(**arg_dict)
@@ -53,6 +59,7 @@ def run_gh_rest_check(
5359
def check_build_status(
5460
lib_repo: Repository,
5561
user: Optional[str] = None,
62+
branch: Optional[str] = None,
5663
workflow_filename: Optional[str] = "build.yml",
5764
debug: bool = False,
5865
) -> Optional[str]:
@@ -62,6 +69,8 @@ def check_build_status(
6269
:param Repository lib_repo: The repo as a github.Repository.Repository object
6370
:param str|None user: The user that triggered the run; if `None` is
6471
provided, any user is acceptable
72+
:param str|None branch: The branch name to specifically check; if `None` is
73+
provided, all branches are allowed; this is the default
6574
:param str|None workflow_filename: The filename of the workflow; if `None`
6675
is provided, any workflow name is acceptable; the defail is `"build.yml"`
6776
:param bool debug: Whether debug statements should be printed to the standard
@@ -74,8 +83,13 @@ def check_build_status(
7483
if debug:
7584
print("Checking", lib_repo.name)
7685

86+
if lib_repo.archived:
87+
return True
88+
7789
try:
78-
result = run_gh_rest_check(lib_repo, user, workflow_filename) == "success"
90+
result = (
91+
run_gh_rest_check(lib_repo, user, branch, workflow_filename) == "success"
92+
)
7993
if debug and not result:
8094
print("***", "Library", lib_repo.name, "failed the patch!", "***")
8195
return result
@@ -94,6 +108,7 @@ def check_build_status(
94108
def check_build_statuses(
95109
gh_token: str,
96110
user: Optional[str] = None,
111+
branch: Optional[str] = "main",
97112
workflow_filename: Optional[str] = "build.yml",
98113
*,
99114
debug: bool = False,
@@ -104,6 +119,8 @@ def check_build_statuses(
104119
:param str gh_token: The Github token to be used for with the Github API
105120
:param str|None user: The user that triggered the run; if `None` is
106121
provided, any user is acceptable
122+
:param str|None branch: The branch name to specifically check; if `None` is
123+
provided, all branches are allowed; this is the default
107124
:param str|None workflow_filename: The filename of the workflow; if `None` is
108125
provided, any workflow name is acceptable; the defail is `"build.yml"`
109126
:param bool debug: Whether debug statements should be printed to
@@ -113,9 +130,10 @@ def check_build_statuses(
113130
:rtype: list
114131
"""
115132

116-
args = (user, workflow_filename)
117-
kwargs = {"debug": debug}
118-
return iter_remote_bundle_with_func(gh_token, [(check_build_status, args, kwargs)])
133+
return iter_remote_bundle_with_func(
134+
gh_token,
135+
[(check_build_status, (user, branch, workflow_filename), {"debug": debug})],
136+
)
119137

120138

121139
def save_build_statuses(
@@ -138,3 +156,60 @@ def save_build_statuses(
138156
with open(failures_filepath, mode="w", encoding="utf-8") as outputfile:
139157
for build in bad_builds:
140158
outputfile.write(build + "\n")
159+
160+
161+
if __name__ == "__main__":
162+
163+
parser = argparse.ArgumentParser(
164+
description="Check the CI status of the Bundle libraries"
165+
)
166+
parser.add_argument(
167+
"gh_token", metavar="GH_TOKEN", type=str, help="GitHub token with proper scopes"
168+
)
169+
parser.add_argument(
170+
"--user",
171+
metavar="U",
172+
type=str,
173+
dest="user",
174+
default=None,
175+
help="Select a specific user that triggered the workflow",
176+
)
177+
parser.add_argument(
178+
"--branch",
179+
metavar="B",
180+
type=str,
181+
dest="branch",
182+
default=None,
183+
help='Branch name; default is "main"',
184+
)
185+
parser.add_argument(
186+
"--workflow",
187+
metavar="W",
188+
type=str,
189+
dest="workflow",
190+
default="build.yml",
191+
help='Workflow name; default is "build.yml"',
192+
)
193+
parser.add_argument(
194+
"--debug", action="store_true", help="Print debug text during execution"
195+
)
196+
197+
args = parser.parse_args()
198+
199+
results = check_build_statuses(
200+
args.gh_token, args.user, args.branch, args.workflow, debug=args.debug
201+
)
202+
fail_list = [
203+
repo_name.name for repo_name, repo_results in results if not repo_results[0]
204+
]
205+
206+
if fail_list:
207+
print(f'Failures for CI workflow "{args.workflow}":')
208+
for failure in fail_list:
209+
print(failure)
210+
RETURN_CODE = 1
211+
else:
212+
print(f"No failures for CI workflow: {args.workflow}!")
213+
RETURN_CODE = 0
214+
215+
raise SystemExit(RETURN_CODE)

tools/docs_status.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"""
1616

1717
from typing import Any, Optional
18+
import argparse
1819
import parse
1920
import requests
2021
from github.Repository import Repository
2122
from github.ContentFile import ContentFile
22-
from tools.iterate_libraries import (
23+
from iterate_libraries import (
2324
iter_remote_bundle_with_func,
2425
RemoteLibFunc_IterResult,
2526
)
@@ -87,6 +88,39 @@ def check_docs_statuses(
8788
:rtype: list
8889
"""
8990

90-
args = (rtd_token,)
91-
kwargs = {}
92-
return iter_remote_bundle_with_func(gh_token, [(check_docs_status, args, kwargs)])
91+
return iter_remote_bundle_with_func(
92+
gh_token, [(check_docs_status, (rtd_token,), {})]
93+
)
94+
95+
96+
if __name__ == "__main__":
97+
98+
parser = argparse.ArgumentParser(
99+
description="Check the RTD docs build status of the Bundle libraries"
100+
)
101+
parser.add_argument(
102+
"gh_token", metavar="GH_TOKEN", type=str, help="GitHub token with proper scopes"
103+
)
104+
parser.add_argument(
105+
"rtd_token", metavar="RTD_TOKEN", type=str, help="ReadTheDocs token"
106+
)
107+
108+
args = parser.parse_args()
109+
110+
results = check_docs_statuses(args.gh_token, args.rtd_token)
111+
fail_list = [
112+
repo_name.name
113+
for repo_name, repo_results in results
114+
if repo_results[0] == False # pylint: disable=singleton-comparison
115+
]
116+
117+
if fail_list:
118+
print("Failures for RTD builds:")
119+
for failure in fail_list:
120+
print(failure)
121+
RETURN_CODE = 1
122+
else:
123+
print("No failures for RTD builds!")
124+
RETURN_CODE = 0
125+
126+
raise SystemExit(RETURN_CODE)

tools/git_functionality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import git
2020
import git.repo
2121
import git.index.base
22-
from tools.library_functions import StrPath
22+
from library_functions import StrPath
2323

2424

2525
def _get_repo_and_remote(

tools/iterate_libraries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from github import Github
2424
from github.Repository import Repository
2525
from github.ContentFile import ContentFile
26-
from tools.library_functions import StrPath, LocalLibFunc, RemoteLibFunc
26+
from library_functions import StrPath, LocalLibFunc, RemoteLibFunc
2727

2828
# Helpful type annotapython generic type aliastion definitions
2929

0 commit comments

Comments
 (0)