Last active
January 25, 2021 23:57
-
-
Save ifnull/66395e89e70939ee5a60887c58f8e8a4 to your computer and use it in GitHub Desktop.
Github Download Exported Migrations — This will download all exported migrations that are not yet downloaded to the current folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import requests | |
import enlighten | |
import os.path | |
from os import path | |
from github import Github | |
from prettytable import PrettyTable | |
ORG_NAME = 'REPLACE_WITH_ORG_NAME' | |
TOKEN = 'REPLACE_WITH_PERSONAL_TOKEN' | |
screen = enlighten.get_manager() | |
g = Github(TOKEN) | |
org = g.get_organization(ORG_NAME) | |
for migration in org.get_migrations(): | |
# Only migrations with a status of export are downloadable | |
if migration.state == 'exported': | |
# Generate an output filename for the archive | |
file = "{}_migrations_{}_{}.tar.gz".format( | |
ORG_NAME, | |
migration.id, | |
migration.repositories[0].name) | |
# Check if we already downloaded the file and if so, we | |
# skip the download. | |
# Note: This does not verify if the file is extractable. | |
# Use `gunzip -t *.tar.gz` to check for failed downloads. | |
if path.exists(file): | |
print("{} - {} skipped because {} already exists.".format( | |
migration.id, migration.repositories[0].name, file)) | |
else: | |
print("{} - {} starting download.".format( | |
migration.id, migration.repositories[0].name)) | |
# The URL returned contains AWS token and headers as query | |
# parameters. The URL authentication expires after 300 ms. | |
url = migration.get_archive_url() | |
# Download file with stream enabled so we can track progress. | |
req = requests.get(url, allow_redirects=True, stream = True) | |
assert req.status_code == 200, req.status_code | |
dlen = int(req.headers.get('Content-Length', '0')) or None | |
# Update the progress bar and write chucks to disk. | |
with screen.counter( | |
color = 'green', | |
total = dlen and math.ceil(dlen / 2 ** 20), | |
unit = 'MiB', leave = False) as ctr, \ | |
open(file, 'wb', buffering = 2 ** 24) as f: | |
for chunk in req.iter_content(chunk_size = 2 ** 20): | |
# print(chunk[-16:].hex().upper()) | |
f.write(chunk) | |
ctr.update() | |
print("{} - {} finished download with {} response code.".format( | |
migration.id, migration.repositories[0].name, req.status_code)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enlighten==1.7.2 | |
prettytable==2.0.0 | |
PyGithub==1.54.1 | |
requests==2.24.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment