-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker_readme.py
executable file
·77 lines (58 loc) · 1.95 KB
/
docker_readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
from argparse import ArgumentParser, Namespace
from json import dumps, loads
from os import environ
from shutil import get_terminal_size
from urllib.request import Request, urlopen
def big_print(msg: str) -> None:
_, cols = get_terminal_size()
print(cols * "-")
print(msg)
print(cols * "-")
def slurp(path: str) -> str:
with open(path) as fd:
return fd.read()
def parse_args() -> Namespace:
parser = ArgumentParser()
username = environ.get("DOCKER_USERNAME")
parser.add_argument("--username", required=not username, default=username)
password = environ.get("DOCKER_PASSWORD")
parser.add_argument("--password", required=not password, default=password)
parser.add_argument("--readme", required=True)
parser.add_argument("--repo", required=True)
args = parser.parse_args()
return args
def login(username: str, password: str) -> str:
uri = "https://hub.docker.com/v2/users/login/"
data = {"username": username, "password": password}
req = Request(
uri,
method="POST",
headers={"Content-Type": "application/json"},
data=dumps(data).encode(),
)
with urlopen(req) as resp:
msg = resp.read().decode()
return loads(msg)["token"]
def set_repo(token: str, repo: str, readme: str) -> str:
uri = f"https://hub.docker.com/v2/repositories/{repo}/"
data = {"full_description": readme}
req = Request(
uri,
method="PATCH",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
},
data=dumps(data).encode(),
)
with urlopen(req) as resp:
msg = resp.read().decode()
return loads(msg)["full_description"]
def main() -> None:
args = parse_args()
readme = slurp(args.readme)
token = login(args.username, args.password)
desc = set_repo(token=token, repo=args.repo, readme=readme,)
big_print(desc)
main()