From e9cedafca476c69ae7af4b82def339981760c89d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Fri, 28 Oct 2022 08:06:37 -0600 Subject: [PATCH 1/2] chore: reformatted and restructured modules --- github_deploy/commands/_http_utils.py | 60 +---------------- github_deploy/commands/_repo_utils.py | 93 +++++++++++++++++++++++++++ github_deploy/commands/_utils.py | 7 ++ github_deploy/commands/delete.py | 3 +- github_deploy/commands/upload.py | 65 +------------------ 5 files changed, 103 insertions(+), 125 deletions(-) create mode 100644 github_deploy/commands/_repo_utils.py diff --git a/github_deploy/commands/_http_utils.py b/github_deploy/commands/_http_utils.py index 0dd699d..539b9ec 100644 --- a/github_deploy/commands/_http_utils.py +++ b/github_deploy/commands/_http_utils.py @@ -1,9 +1,6 @@ import ssl -import certifi - -import asyncclick as click -from github_deploy.commands._constants import REPOS_URL +import certifi async def get(*, session, url, headers=None, skip_missing=False): @@ -51,58 +48,3 @@ async def delete(*, session, url, data, headers=None): ) as response: value = await response.json() return value - - -async def list_repos(*, session, org, token): - headers = { - "Authorization": "Bearer {token}".format(token=token), - "Accept": "application/vnd.github+json", - } - url = REPOS_URL.format(org=org) - click.echo("Retrieving repos at {}".format(url)) - response = await get(session=session, url=url, headers=headers) - return response - - -async def delete_content( - *, - session, - repo, - dest, - token, - semaphore, - exists, - current_sha, -): - headers = { - "Authorization": "Bearer {token}".format(token=token), - "Accept": "application/vnd.github+json", - } - - data = {"message": "Deleted {}".format(dest)} - if exists: - data["sha"] = current_sha - - url = BASE_URL.format(repo=repo, path=dest) - - async with semaphore: - response = await delete( - session=session, url=url, data=data, headers=headers - ) - - return response - - -async def check_exists(*, session, repo, dest, token, semaphore, skip_missing): - headers = {"Authorization": "Bearer {token}".format(token=token)} - url = BASE_URL.format(repo=repo, path=dest) - - async with semaphore: - response = await get( - session=session, - url=url, - headers=headers, - skip_missing=skip_missing, - ) - - return response diff --git a/github_deploy/commands/_repo_utils.py b/github_deploy/commands/_repo_utils.py new file mode 100644 index 0000000..ac6c068 --- /dev/null +++ b/github_deploy/commands/_repo_utils.py @@ -0,0 +1,93 @@ +import base64 + +import aiofiles +import asyncclick as click + +from github_deploy.commands._constants import REPOS_URL, BASE_URL +from github_deploy.commands._http_utils import get, delete, put +from github_deploy.commands._utils import get_headers + + +async def list_repos(*, session, org, token): + url = REPOS_URL.format(org=org) + click.echo("Retrieving repos at {}".format(url)) + response = await get(session=session, url=url, headers=get_headers(token=token)) + return response + + +async def delete_content( + *, + session, + repo, + dest, + token, + semaphore, + exists, + current_sha, +): + data = {"message": "Deleted {}".format(dest)} + if exists: + data["sha"] = current_sha + + url = BASE_URL.format(repo=repo, path=dest) + + async with semaphore: + response = await delete( + session=session, url=url, data=data, headers=get_headers(token=token) + ) + + return response + + +async def check_exists(*, session, repo, dest, token, semaphore, skip_missing): + url = BASE_URL.format(repo=repo, path=dest) + + async with semaphore: + response = await get( + session=session, + url=url, + headers=get_headers(token=token), + skip_missing=skip_missing, + ) + + return response + + +async def upload_content( + *, + session, + repo, + source, + dest, + token, + semaphore, + exists, + current_sha, + current_content +): + async with semaphore: + async with aiofiles.open(source, mode="rb") as f: + output = await f.read() + base64_content = base64.b64encode(output).decode("ascii") + + if current_content == base64_content: + click.echo("Skipping: Contents are the same.") + return + + data = { + "message": "Updated {}".format(dest) + if exists + else "Added {}".format(dest), + "content": base64_content, + } + if exists: + data["sha"] = current_sha + + url = BASE_URL.format(repo=repo, path=dest) + + async with semaphore: + response = await put( + session=session, url=url, data=data, headers=get_headers(token=token) + ) + + return response diff --git a/github_deploy/commands/_utils.py b/github_deploy/commands/_utils.py index 109bd3f..3289654 100644 --- a/github_deploy/commands/_utils.py +++ b/github_deploy/commands/_utils.py @@ -8,3 +8,10 @@ def can_upload(*, repo, include_private): if include_private and repo["private"] is True else not repo["private"] ) + + +def get_headers(*, token): + return { + "Authorization": "Bearer {token}".format(token=token), + "Accept": "application/vnd.github+json", + } diff --git a/github_deploy/commands/delete.py b/github_deploy/commands/delete.py index d3c2d50..9cafec6 100644 --- a/github_deploy/commands/delete.py +++ b/github_deploy/commands/delete.py @@ -3,8 +3,7 @@ import aiohttp import asyncclick as click -from github_deploy.commands._constants import BASE_URL -from github_deploy.commands._http_utils import delete, get, list_repos, delete_contents, check_exists +from github_deploy.commands._repo_utils import list_repos, delete_content, check_exists from github_deploy.commands._utils import get_repo diff --git a/github_deploy/commands/upload.py b/github_deploy/commands/upload.py index fe58e6d..3baf4a3 100644 --- a/github_deploy/commands/upload.py +++ b/github_deploy/commands/upload.py @@ -1,75 +1,12 @@ import asyncio -import base64 -import aiofiles import aiohttp import asyncclick as click -from github_deploy.commands._constants import BASE_URL -from github_deploy.commands._http_utils import put, list_repos, get +from github_deploy.commands._repo_utils import list_repos, check_exists, upload_content from github_deploy.commands._utils import get_repo, can_upload -async def upload_content( - *, - session, - repo, - source, - dest, - token, - semaphore, - exists, - current_sha, - current_content -): - headers = { - "Authorization": "token {token}".format(token=token), - "Accept": "application/vnd.github.v3+json", - } - - async with semaphore: - async with aiofiles.open(source, mode="rb") as f: - output = await f.read() - base64_content = base64.b64encode(output).decode("ascii") - - if current_content == base64_content: - click.echo("Skipping: Contents are the same.") - return - - data = { - "message": "Updated {}".format(dest) - if exists - else "Added {}".format(dest), - "content": base64_content, - } - if exists: - data["sha"] = current_sha - - url = BASE_URL.format(repo=repo, path=dest) - - async with semaphore: - response = await put( - session=session, url=url, data=data, headers=headers - ) - - return response - - -async def check_exists(*, session, repo, dest, token, semaphore, skip_missing): - headers = {"Authorization": "token {token}".format(token=token)} - url = BASE_URL.format(repo=repo, path=dest) - - async with semaphore: - response = await get( - session=session, - url=url, - headers=headers, - skip_missing=skip_missing, - ) - - return response - - async def handle_file_upload( *, repo, source, dest, overwrite, token, semaphore, session ): From 1742c2d98774c57bc57a4f6059113a203d0f253d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Fri, 28 Oct 2022 08:16:15 -0600 Subject: [PATCH 2/2] re-order imports --- github_deploy/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github_deploy/main.py b/github_deploy/main.py index c7cce59..246c985 100644 --- a/github_deploy/main.py +++ b/github_deploy/main.py @@ -1,6 +1,7 @@ -import asyncclick as click import os +import asyncclick as click + plugin_folder = os.path.join(os.path.dirname(__file__), "commands")