WIP: Project Tools: Remove Requests Module from studio scripts #279
@ -4,15 +4,15 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
import hashlib
|
import hashlib
|
||||||
import subprocess
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import sys
|
import sys
|
||||||
import requests
|
from urllib.request import Request, urlopen
|
||||||
|
from urllib.error import HTTPError
|
||||||
|
from http.client import HTTPResponse
|
||||||
import json
|
import json
|
||||||
from requests import Response
|
|
||||||
|
|
||||||
BASE_PATH = "https://projects.blender.org/api/v1"
|
BASE_PATH = "https://projects.blender.org/api/v1"
|
||||||
REPO_PATH = '/studio/blender-studio-pipeline'
|
REPO_PATH = '/TinyNick/blender-studio-pipeline'
|
||||||
RELEASE_PATH = BASE_PATH + f'/repos{REPO_PATH}/releases'
|
RELEASE_PATH = BASE_PATH + f'/repos{REPO_PATH}/releases'
|
||||||
TAG_PATH = BASE_PATH + f'/repos{REPO_PATH}/tags'
|
TAG_PATH = BASE_PATH + f'/repos{REPO_PATH}/tags'
|
||||||
API_TOKEN = None
|
API_TOKEN = None
|
||||||
@ -46,7 +46,7 @@ def remove_existing_release_assets(release_id: int) -> None:
|
|||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
all_assets = send_get_request(RELEASE_PATH + f"/{release_id}/assets").json()
|
all_assets = send_get_request(RELEASE_PATH + f"/{release_id}/assets")
|
||||||
for asset in all_assets:
|
for asset in all_assets:
|
||||||
if asset["name"] == ZIP_NAME + ".zip" or asset["name"] == ZIP_NAME + ".zip.sha256":
|
if asset["name"] == ZIP_NAME + ".zip" or asset["name"] == ZIP_NAME + ".zip.sha256":
|
||||||
send_delete_request(RELEASE_PATH + f"/{release_id}/assets/{asset['id']}")
|
send_delete_request(RELEASE_PATH + f"/{release_id}/assets/{asset['id']}")
|
||||||
@ -64,23 +64,29 @@ def upload_asset_to_release(release_id: int, file: str) -> None:
|
|||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
file_name = Path(file.name).name
|
file_name = Path(file).name
|
||||||
payload = open(file, 'rb')
|
zip_file = open(file, 'rb')
|
||||||
file_content = [
|
zip_data = zip_file.read()
|
||||||
('attachment', (file_name, payload, 'application/zip')),
|
|
||||||
]
|
url = f"{RELEASE_PATH}/{release_id}/assets?name={file_name}&token={API_TOKEN}"
|
||||||
|
req = Request(url, data=zip_data)
|
||||||
|
|
||||||
print(f"Uploading '{file_name}'......", end="")
|
print(f"Uploading '{file_name}'......", end="")
|
||||||
response = requests.post(
|
# TODO DEBUG WHY THIS RETURNS 500 ERROR
|
||||||
url=f"{RELEASE_PATH}/{release_id}/assets?name={file_name}&token={API_TOKEN}",
|
with urlopen(req) as response:
|
||||||
files=file_content,
|
response_data = json.loads(response.read().decode("utf-8"))
|
||||||
)
|
print(response, response_data)
|
||||||
|
|
||||||
response.raise_for_status()
|
# response = requests.post(
|
||||||
|
# url=f"{RELEASE_PATH}/{release_id}/assets?name={file_name}&token={API_TOKEN}",
|
||||||
|
# files=file_content,
|
||||||
|
# )
|
||||||
|
# response.raise_for_status()
|
||||||
|
|
||||||
if not response.status_code == 201:
|
# if not response.status_code == 201:
|
||||||
print(f"Failed to upload.")
|
# print(f"Failed to upload.")
|
||||||
else:
|
# else:
|
||||||
print(f"Completed")
|
# print(f"Completed")
|
||||||
|
|
||||||
|
|
||||||
def get_release() -> dict:
|
def get_release() -> dict:
|
||||||
@ -94,7 +100,7 @@ def get_release() -> dict:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Remove Previous Release so Release is always based on Current Commit
|
# Remove Previous Release so Release is always based on Current Commit
|
||||||
for release in send_get_request(RELEASE_PATH).json():
|
for release in send_get_request(RELEASE_PATH):
|
||||||
if release["name"] == RELEASE_TITLE and release["tag_name"] == RELEASE_VERSION:
|
if release["name"] == RELEASE_TITLE and release["tag_name"] == RELEASE_VERSION:
|
||||||
send_delete_request(RELEASE_PATH + f"/{release['id']}")
|
send_delete_request(RELEASE_PATH + f"/{release['id']}")
|
||||||
send_delete_request(TAG_PATH + f"/{release['tag_name']}")
|
send_delete_request(TAG_PATH + f"/{release['tag_name']}")
|
||||||
@ -114,7 +120,7 @@ def create_new_release() -> dict:
|
|||||||
"""
|
"""
|
||||||
# Create New Tag
|
# Create New Tag
|
||||||
existing_tag = send_get_request(TAG_PATH + f'/{RELEASE_VERSION}')
|
existing_tag = send_get_request(TAG_PATH + f'/{RELEASE_VERSION}')
|
||||||
if existing_tag.status_code == 404:
|
if not existing_tag:
|
||||||
tag_content = {
|
tag_content = {
|
||||||
"message": RELEASE_DESCRIPTION,
|
"message": RELEASE_DESCRIPTION,
|
||||||
"tag_name": RELEASE_VERSION,
|
"tag_name": RELEASE_VERSION,
|
||||||
@ -133,7 +139,7 @@ def create_new_release() -> dict:
|
|||||||
"target_commitish": "string", # will default to latest
|
"target_commitish": "string", # will default to latest
|
||||||
}
|
}
|
||||||
|
|
||||||
return send_post_request(RELEASE_PATH, release_content).json()
|
return send_post_request(RELEASE_PATH, release_content)
|
||||||
|
|
||||||
|
|
||||||
def get_api_token() -> None:
|
def get_api_token() -> None:
|
||||||
@ -150,10 +156,10 @@ def get_api_token() -> None:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
API_TOKEN = open(api_token_file, 'r').read()
|
API_TOKEN = open(api_token_file, 'r').read()
|
||||||
# Don't use send_get_request() so we can print custom error message to user
|
# Don't use send_get_request() so we can print custom error message to user
|
||||||
response = requests.get(url=f"{BASE_PATH}/settings/api?token={API_TOKEN}")
|
response_data = urlopen(url=f"{BASE_PATH}/settings/api?token={API_TOKEN}")
|
||||||
if response.status_code != 200:
|
if response_data.status != 200:
|
||||||
print("API Token is invalid")
|
print("API Token is invalid")
|
||||||
print(f"Error: {response.status_code}: '{response.reason}'")
|
print(f"Error: {response_data.status}: '{response_data.msg}'")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
@ -217,36 +223,32 @@ def generate_checksum(archive_path: Path) -> str:
|
|||||||
return digest.hexdigest()
|
return digest.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def send_delete_request(url) -> Response:
|
def send_delete_request(url) -> HTTPResponse:
|
||||||
response = requests.delete(url=f"{url}?token={API_TOKEN}")
|
response = urlopen(Request(url=f"{url}?token={API_TOKEN}", method="DELETE"))
|
||||||
if response.status_code != 204:
|
if response.status != 204:
|
||||||
print(f"Error: {response.status_code}: '{response.reason}'")
|
print(f"Error: {response.status}: '{response.msg}'")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def send_get_request(url: str) -> Response:
|
def send_get_request(url: str):
|
||||||
response = requests.get(url=f"{url}?token={API_TOKEN}")
|
try:
|
||||||
if not (response.status_code == 200 or response.status_code == 404):
|
response = urlopen(f"{url}?token={API_TOKEN}")
|
||||||
print(f"Error: {response.status_code}: '{response.reason}'")
|
except HTTPError as e:
|
||||||
|
if e.code == 404:
|
||||||
|
return
|
||||||
|
print(f"Error: {e.code}: '{e.reason}'")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
return response
|
return json.loads(response.read().decode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
def send_post_request(url: str, data: dict) -> Response:
|
def send_post_request(url: str, data: dict):
|
||||||
header_cont = {
|
data = json.dumps(data)
|
||||||
'Content-type': 'application/json',
|
req = Request(url=f"{url}?token={API_TOKEN}", data=bytes(data.encode("utf-8")), method="POST")
|
||||||
}
|
req.add_header("Content-type", "application/json; charset=UTF-8")
|
||||||
response = requests.post(
|
resp = urlopen(req)
|
||||||
url=f"{url}?token={API_TOKEN}",
|
response_data = json.loads(resp.read().decode("utf-8"))
|
||||||
headers=header_cont,
|
return response_data
|
||||||
data=json.dumps(data),
|
|
||||||
)
|
|
||||||
response_json = response.json()
|
|
||||||
if response.status_code != 201:
|
|
||||||
print(response_json["message"])
|
|
||||||
sys.exit(1)
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from urllib.request import urlretrieve
|
from urllib.request import urlretrieve, urlopen
|
||||||
import sys
|
import sys
|
||||||
import requests
|
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ def update_blender_studio_addons(download_folder_path: Path):
|
|||||||
url_zip = "https://projects.blender.org/studio/blender-studio-pipeline/releases/download/latest/blender_studio_add-ons_latest.zip"
|
url_zip = "https://projects.blender.org/studio/blender-studio-pipeline/releases/download/latest/blender_studio_add-ons_latest.zip"
|
||||||
|
|
||||||
# Check current sha and early return if match
|
# Check current sha and early return if match
|
||||||
web_sha = requests.get(url_sha).text.strip().lower()
|
web_sha = urlopen(url_sha).read().decode().strip().lower()
|
||||||
if sha_file.exists() & zip_file.exists():
|
if sha_file.exists() & zip_file.exists():
|
||||||
if shasum_matches(zip_file, web_sha):
|
if shasum_matches(zip_file, web_sha):
|
||||||
print(f"{zip_file.name} already up to date, canceling update")
|
print(f"{zip_file.name} already up to date, canceling update")
|
||||||
@ -67,13 +66,7 @@ def download_file(url, out_folder, filename):
|
|||||||
for file in prev_downloaded_files:
|
for file in prev_downloaded_files:
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
|
|
||||||
# NOTE the stream=True parameter below
|
urlretrieve(url, str(local_filename))
|
||||||
with requests.get(url, stream=True) as r:
|
|
||||||
r.raise_for_status()
|
|
||||||
with open(local_filename, 'wb') as f:
|
|
||||||
for chunk in r.iter_content(chunk_size=None):
|
|
||||||
if chunk:
|
|
||||||
f.write(chunk)
|
|
||||||
|
|
||||||
local_hash_filename = local_filename.with_suffix(".zip.sha256")
|
local_hash_filename = local_filename.with_suffix(".zip.sha256")
|
||||||
with open(local_filename, "rb") as f:
|
with open(local_filename, "rb") as f:
|
||||||
|
@ -5,7 +5,7 @@ import glob
|
|||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import requests
|
from urllib.request import urlretrieve, urlopen
|
||||||
import shutil
|
import shutil
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@ -18,13 +18,7 @@ BLENDER_BRANCH = "main"
|
|||||||
def download_file(url, out_folder):
|
def download_file(url, out_folder):
|
||||||
print("Downloading: " + url)
|
print("Downloading: " + url)
|
||||||
local_filename = out_folder / url.split('/')[-1]
|
local_filename = out_folder / url.split('/')[-1]
|
||||||
# NOTE the stream=True parameter below
|
urlretrieve(url, str(local_filename))
|
||||||
with requests.get(url, stream=True) as r:
|
|
||||||
r.raise_for_status()
|
|
||||||
with open(local_filename, 'wb') as f:
|
|
||||||
for chunk in r.iter_content(chunk_size=None):
|
|
||||||
if chunk:
|
|
||||||
f.write(chunk)
|
|
||||||
return local_filename
|
return local_filename
|
||||||
|
|
||||||
|
|
||||||
@ -60,8 +54,8 @@ platforms_dict = {
|
|||||||
|
|
||||||
download_info = []
|
download_info = []
|
||||||
branch_string = "+" + BLENDER_BRANCH
|
branch_string = "+" + BLENDER_BRANCH
|
||||||
reqs = requests.get(BUILDS_INDEX)
|
reqs = urlopen(BUILDS_INDEX).read().decode()
|
||||||
available_downloads = json.loads(reqs.text)
|
available_downloads = json.loads(reqs)
|
||||||
for download in available_downloads:
|
for download in available_downloads:
|
||||||
if download["branch"] != BLENDER_BRANCH:
|
if download["branch"] != BLENDER_BRANCH:
|
||||||
continue
|
continue
|
||||||
@ -81,7 +75,7 @@ for info in download_info:
|
|||||||
file_extension = platforms_dict[platform]
|
file_extension = platforms_dict[platform]
|
||||||
url = info[1]
|
url = info[1]
|
||||||
url_sha = url + ".sha256"
|
url_sha = url + ".sha256"
|
||||||
sha = requests.get(url_sha).text.strip().lower()
|
sha = urlopen(url_sha).read().decode().strip().lower()
|
||||||
arch = info[2]
|
arch = info[2]
|
||||||
|
|
||||||
current_platform_file = glob.glob(f"{download_folder_path}/*{platform}.{arch}*{file_extension}")
|
current_platform_file = glob.glob(f"{download_folder_path}/*{platform}.{arch}*{file_extension}")
|
||||||
|
Loading…
Reference in New Issue
Block a user