|
4 | 4 |
|
5 | 5 | Caveat being, without an API key, you will only ever see the public repos. |
6 | 6 | """ |
7 | | -PROGRAM = 'quay-namespace-info' |
8 | | -VERSION = '1.0.0' |
9 | | -QUAY_API_URL= 'https://quay.io/api/v1/repository' |
| 7 | + |
| 8 | +PROGRAM = "quay-namespace-info" |
| 9 | +VERSION = "1.0.0" |
| 10 | +QUAY_API_URL = "https://quay.io/api/v1/repository" |
10 | 11 |
|
11 | 12 | if __name__ == "__main__": |
12 | 13 | import argparse as ap |
|
17 | 18 |
|
18 | 19 | parser = ap.ArgumentParser( |
19 | 20 | prog=PROGRAM, |
20 | | - conflict_handler='resolve', |
| 21 | + conflict_handler="resolve", |
21 | 22 | description=( |
22 | | - f'{PROGRAM} (v{VERSION}) - Check visibility of containers and optionally set to public\n' |
| 23 | + f"{PROGRAM} (v{VERSION}) - Check visibility of containers and optionally set to public\n" |
23 | 24 | ), |
24 | | - formatter_class=ap.RawDescriptionHelpFormatter |
| 25 | + formatter_class=ap.RawDescriptionHelpFormatter, |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + "--namespace", |
| 29 | + metavar="STR", |
| 30 | + type=str, |
| 31 | + default="biocontainers", |
| 32 | + help="Namespace to query (default: biocontainers)", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--changevisibility", |
| 36 | + action="store_true", |
| 37 | + help="Any private repos will be set to public, requires QUAY_OAUTH_TOKEN to be set", |
25 | 38 | ) |
26 | | - parser.add_argument('--namespace', metavar="STR", type=str, default="biocontainers", |
27 | | - help='Namespace to query (default: biocontainers)') |
28 | | - parser.add_argument('--changevisibility', action='store_true', |
29 | | - help='Any private repos will be set to public, requires QUAY_OAUTH_TOKEN to be set') |
30 | | - parser.add_argument('--version', action='version', |
31 | | - version=f'{PROGRAM} {VERSION}') |
| 39 | + parser.add_argument("--version", action="version", version=f"{PROGRAM} {VERSION}") |
32 | 40 |
|
33 | 41 | if len(sys.argv) == 1: |
34 | 42 | parser.print_help() |
|
37 | 45 | args = parser.parse_args() |
38 | 46 |
|
39 | 47 | # Set headers, include OAuth token if available |
40 | | - HEADERS = {'Content-Type': 'application/json'} |
41 | | - QUAY_OAUTH_TOKEN = os.getenv('QUAY_OAUTH_TOKEN') |
| 48 | + HEADERS = {"Content-Type": "application/json"} |
| 49 | + QUAY_OAUTH_TOKEN = os.getenv("QUAY_OAUTH_TOKEN") |
42 | 50 | if QUAY_OAUTH_TOKEN: |
43 | | - print(f'Quay Token found, using it for {args.namespace}') |
44 | | - HEADERS['Authorization'] = f'Bearer {QUAY_OAUTH_TOKEN}' |
| 51 | + print(f"Quay Token found, using it for {args.namespace}") |
| 52 | + HEADERS["Authorization"] = f"Bearer {QUAY_OAUTH_TOKEN}" |
45 | 53 |
|
46 | 54 | # Starting querying Quay |
47 | 55 | next_page = "" |
48 | 56 | has_next_page = True |
49 | 57 | repo_status = {} |
50 | 58 | change_visibility = [] |
51 | | - print(f'Starting query against {args.namespace}') |
| 59 | + print(f"Starting query against {args.namespace}") |
52 | 60 | while has_next_page: |
53 | 61 | r = requests.get( |
54 | 62 | QUAY_API_URL, |
55 | 63 | headers=HEADERS, |
56 | | - params={'namespace': args.namespace, 'next_page': next_page} if QUAY_OAUTH_TOKEN else {'public': 'true', 'namespace': args.namespace, 'next_page': next_page}, |
57 | | - timeout=10 |
| 64 | + params={"namespace": args.namespace, "next_page": next_page} |
| 65 | + if QUAY_OAUTH_TOKEN |
| 66 | + else { |
| 67 | + "public": "true", |
| 68 | + "namespace": args.namespace, |
| 69 | + "next_page": next_page, |
| 70 | + }, |
| 71 | + timeout=10, |
58 | 72 | ) |
59 | 73 | json_data = r.json() |
60 | 74 | """ |
|
81 | 95 | """ |
82 | 96 |
|
83 | 97 | # next_page is only available if there are more pages, use it to break out of while loop |
84 | | - if 'next_page' in json_data: |
85 | | - next_page = json_data['next_page'] |
| 98 | + if "next_page" in json_data: |
| 99 | + next_page = json_data["next_page"] |
86 | 100 | else: |
87 | 101 | has_next_page = False |
88 | 102 |
|
89 | 103 | # Capture public/private status for each repo |
90 | | - for repo in json_data['repositories']: |
91 | | - repo_status[repo['name']] = { |
92 | | - "namespace": repo['namespace'], |
93 | | - "name": repo['name'], |
94 | | - "is_public": repo['is_public'] |
| 104 | + for repo in json_data["repositories"]: |
| 105 | + repo_status[repo["name"]] = { |
| 106 | + "namespace": repo["namespace"], |
| 107 | + "name": repo["name"], |
| 108 | + "is_public": repo["is_public"], |
95 | 109 | } |
96 | 110 |
|
97 | 111 | # Collect repos that are private |
98 | | - if repo['is_public'] is False: |
| 112 | + if repo["is_public"] is False: |
99 | 113 | change_visibility.append(repo["name"]) |
100 | 114 |
|
101 | 115 | # Couldn't find a specific rate limit in the docs, so limit to max 3 per second |
102 | 116 | time.sleep(0.3) |
103 | 117 |
|
104 | 118 | # Optionally change visibility |
105 | | - print(f'Found {len(repo_status)} repos under namespace {args.namespace} ({len(change_visibility)} private)') |
| 119 | + print( |
| 120 | + f"Found {len(repo_status)} repos under namespace {args.namespace} ({len(change_visibility)} private)" |
| 121 | + ) |
106 | 122 | if args.changevisibility and QUAY_OAUTH_TOKEN: |
107 | | - print(f'Changing visibility of {len(change_visibility)} repos to public') |
108 | | - with open(f'{args.namespace}-changevisibility.txt', 'w') as fh: |
| 123 | + print(f"Changing visibility of {len(change_visibility)} repos to public") |
| 124 | + with open(f"{args.namespace}-changevisibility.txt", "w") as fh: |
109 | 125 | for repo in change_visibility: |
110 | | - |
111 | 126 | r = requests.post( |
112 | | - f'{QUAY_API_URL}/{args.namespace}/{repo}/changevisibility', |
| 127 | + f"{QUAY_API_URL}/{args.namespace}/{repo}/changevisibility", |
113 | 128 | headers=HEADERS, |
114 | | - json={'visibility': 'public'}, |
115 | | - timeout=10 |
| 129 | + json={"visibility": "public"}, |
| 130 | + timeout=10, |
116 | 131 | ) |
117 | | - repo_status[repo]['is_public'] = "True (changed by script)" |
| 132 | + repo_status[repo]["is_public"] = "True (changed by script)" |
118 | 133 | # Again, be nice to Quay |
119 | 134 | time.sleep(0.3) |
120 | | - fh.write(f'Changed visibility of {args.namespace}/{repo} to public\n') |
| 135 | + fh.write(f"Changed visibility of {args.namespace}/{repo} to public\n") |
121 | 136 |
|
122 | 137 | # Print status |
123 | | - with open(f'{args.namespace}-status.txt', 'w') as fh: |
124 | | - fh.write('namespace\tname\tis_public\n') |
125 | | - for k,v in sorted(repo_status.items()): |
126 | | - fh.write(f'{v["namespace"]}\t{v["name"]}\t{v["is_public"]}\n') |
| 138 | + with open(f"{args.namespace}-status.txt", "w") as fh: |
| 139 | + fh.write("namespace\tname\tis_public\n") |
| 140 | + for k, v in sorted(repo_status.items()): |
| 141 | + fh.write(f"{v['namespace']}\t{v['name']}\t{v['is_public']}\n") |
0 commit comments