-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_upload.py
111 lines (95 loc) · 3.6 KB
/
build_upload.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import bot
HOST, USUARIO, REPOSITORIO, TOKEN = bot.configfile.obter_opcoes_obrigatorias("github", "host", "usuario", "repositorio", "token")
def apagar_release (id_release: int) -> None:
response = bot.http.request(
"DELETE",
f"{HOST}/repos/{USUARIO}/{REPOSITORIO}/releases/{id_release}",
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {TOKEN}"
}
)
assert response.status_code == 204
def criar_release (release: str) -> int:
response = bot.http.request(
"POST",
f"{HOST}/repos/{USUARIO}/{REPOSITORIO}/releases",
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {TOKEN}"
},
json = {
"tag_name": release,
"name": release,
}
)
assert response.status_code == 201
return response.json()["id"]
def obter_releases () -> dict[str, int]:
"""`{ Versão release: id release }`"""
response = bot.http.request(
"GET",
f"{HOST}/repos/{USUARIO}/{REPOSITORIO}/releases",
headers={
"Accept": "application/json",
"Authorization": f"Bearer {TOKEN}"
}
)
releases, erro = bot.formatos.Json.parse(response.text)
assert response.status_code == 200 and not erro and releases.validar({
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "number" },
"tag_name": { "type": "string" }
}
}
}), "Erro ao obter os releases"
return {
release["tag_name"]: release["id"]
for release in releases.valor()
if release["tag_name"]
}
def upload_asset (id_release: int, caminho_build: bot.sistema.Caminho) -> str:
"""retorna o url para a build"""
host = HOST.replace("api", "uploads")
response = bot.http.request(
"POST",
f"{host}/repos/{USUARIO}/{REPOSITORIO}/releases/{id_release}/assets",
params = { "name": caminho_build.nome },
headers = {
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/octet-stream"
},
content = open(caminho_build.string, "rb").read()
)
assert response.status_code == 201
return response.json()["browser_download_url"]
def versao_build (caminho: bot.sistema.Caminho) -> str:
versão = caminho.nome.split("-")[1]
return f"v{versão}"
def obter_ultima_build () -> bot.sistema.Caminho:
arquivos_whl = [c for c in bot.sistema.Caminho(".", "dist")
if c.arquivo() and c.nome.endswith(".whl")]
assert arquivos_whl, "Nenhuma build '.whl' encontrada em './dist'"
return sorted(arquivos_whl, key=lambda c: c.nome, reverse=True)[0]
def main () -> None:
sucesso, erro = bot.sistema.executar("build.bat")
assert sucesso, f"Falha ao executar o script de buid\n{erro}"
caminho = obter_ultima_build()
print(f"\n### Build gerada com sucesso: {caminho} ###")
release = versao_build(caminho)
print(f"### Release: {release} ###")
releases = obter_releases()
if release in releases:
id_release = releases[release]
apagar_release(id_release)
print(f"### Release existente, id {id_release}, foi apagado ###")
id_release = criar_release(release)
print(f"### Criado release id: {id_release} ###")
url_download = upload_asset(id_release, caminho)
print(f"### Build da versão {release} criada e realizado upload com sucesso para o GitHub ###")
print(f"### Url para a build: {url_download} ###\n")
if __name__ == "__main__":
main()