11#!/usr/bin/env python3
2+
3+ """
4+ Check known projects for usage of requires-python.
5+
6+ Usage:
7+
8+ ./bin/inspect_all_known_projects.py --online=$GITHUB_TOKEN
9+
10+ This will cache the results to all_known_setup.yaml; you can reprint
11+ the results without the `--online` setting.
12+ """
13+
14+
215from __future__ import annotations
316
417import ast
720
821import click
922import yaml
10- from ghapi . core import GhApi , HTTP404NotFoundError
23+ from github import Github , GithubException
1124from rich import print
1225
1326from cibuildwheel .projectfiles import Analyzer
@@ -47,33 +60,38 @@ def check_repo(name: str, contents: str) -> str:
4760
4861
4962class MaybeRemote :
50- def __init__ (self , cached_file : Path | str , * , online : bool ) -> None :
51- self .online = online
52- if self .online :
53- self .contents : dict [str , dict [str , str | None ]] = {
63+ github : Github | None
64+ contents : dict [str , dict [str , str | None ]]
65+
66+ def __init__ (self , cached_file : Path | str , * , online : str | None ) -> None :
67+ if online is not None :
68+ self .github = Github (online )
69+ self .contents = {
5470 "setup.py" : {},
5571 "setup.cfg" : {},
5672 "pyproject.toml" : {},
5773 }
5874 else :
75+ self .github = None
5976 with open (cached_file ) as f :
6077 self .contents = yaml .safe_load (f )
6178
6279 def get (self , repo : str , filename : str ) -> str | None :
63- if self .online :
80+ if self .github :
6481 try :
65- self .contents [filename ][repo ] = (
66- GhApi (* repo .split ("/" )).get_content (filename ).decode ()
67- )
68- except HTTP404NotFoundError :
82+ gh_file = self .github .get_repo (repo ).get_contents (filename )
83+ except GithubException :
6984 self .contents [filename ][repo ] = None
85+ else :
86+ assert not isinstance (gh_file , list )
87+ self .contents [filename ][repo ] = gh_file .decoded_content .decode (encoding = "utf-8" )
88+
7089 return self .contents [filename ][repo ]
7190 elif repo in self .contents [filename ]:
7291 return self .contents [filename ][repo ]
7392 else :
74- raise RuntimeError (
75- f"Trying to access { repo } :{ filename } and not in cache, rebuild cache"
76- )
93+ msg = f"Trying to access { repo } :{ filename } and not in cache, rebuild cache"
94+ raise RuntimeError (msg )
7795
7896 def save (self , filename : Path | str ) -> None :
7997 with open (filename , "w" ) as f :
@@ -87,8 +105,8 @@ def on_each(self, repos: list[str]) -> Iterator[tuple[str, str, str | None]]:
87105
88106
89107@click .command ()
90- @click .option ("--online" , is_flag = True , help = "Remember to set GITHUB_TOKEN" )
91- def main (online : bool ) -> None :
108+ @click .option ("--online" , help = "Set to $ GITHUB_TOKEN" )
109+ def main (online : str | None ) -> None :
92110 with open (DIR / "../docs/data/projects.yml" ) as f :
93111 known = yaml .safe_load (f )
94112
0 commit comments