Skip to content

Instantly share code, notes, and snippets.

@Demon000
Last active March 29, 2020 21:14
Show Gist options
  • Save Demon000/2c51ee64a7650520e631c6805b0c9ac5 to your computer and use it in GitHub Desktop.
Save Demon000/2c51ee64a7650520e631c6805b0c9ac5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import hashlib
import json
import os
import sys
from github import Github
class Publisher():
def __init__(self, github_token, builds_json_path):
self.__github_token = github_token
self.__builds_json_path = builds_json_path
self.__builds_data = {}
def __upload_build(self, build_path, build_name, device):
github_handle = Github(self.__github_token)
github_user = github_handle.get_user().login
# Check if the repo exists if not create it
try:
repo = github_handle.get_user().get_repo(device)
except:
repo = github_handle.get_user().create_repo(device)
# Create an initial commit in case it doesn't exist
# so that github lets us create a release
try:
repo.create_file('README', 'initial commit', device)
except:
pass
# Check if the release already exists, if so, nuke it
try:
release = repo.get_release(build_name)
release.delete_release()
except:
pass
# Create the release
release = repo.create_git_release(build_name, build_name, build_name)
# Upload the zip
release.upload_asset(build_path)
# Format the arguments into the generated github url
github_url = 'https://github.com/{}/{}/releases/download/{}/{}' \
.format(github_user, device, build_name, build_filename)
return github_url
def __get_build_details(self, build_path):
# Get the build's filename
build_filename = os.path.basename(build_path)
# Release name will be the zip's file name without
# the '.zip' at the end.
build_name = os.path.splitext(build_filename)[0]
# Extract version, build details and device name
# from the zip's name
_, build_version, build_date, build_type, device = build_name.split('-')
return build_filename, build_name, build_version, build_date, build_type, device
def __get_build_sha256(self, build_path):
file_data = open(build_path)
file_sha256 = hashlib.sha256()
for buf in iter(lambda: file_data.read(128 * 1024), b''):
file_sha256.update(buf)
return file_sha256.hexdigest()
def __add_build(self, build_filename, build_url, build_version, build_type, build_date):
# Generate sha256 for the build
file_sha256 = self.__get_build_sha256(build_path)
# Get the file size
file_size = os.path.getsize(build_path)
# Format build date
file_date = '{}-{}-{}'.format(build_date[0:4], build_date[4:6], build_date[6:8])
build_data = {
'sha256': file_sha256,
'size': file_size,
'date': file_date,
'filename': build_filename,
'filepath': build_url,
'version': build_version,
'type': build_type.lower(),
}
# Append the build data to the builds
self.__builds_data.setdefault(device, []).append(build_data)
return build_data
def publish_build(self, build_path):
# Get build details
build_filename, build_name, build_version, \
build_date, build_type, device = self.__get_build_details(build_path)
# Upload the build to github
build_url = self.__upload_build(build_path, build_name, device)
# Add the build to the current builds list
build_data = self.__add_build(build_filename, build_url, build_version, build_type)
return build_data
def load_builds_data(self):
# Open the current builds.json file in read-only mode
# and try to parse the contents
try:
with open(self.__builds_json_path, 'r') as builds_json_file:
self.__builds_data = json.load(builds_json_file)
except:
pass
def save_builds_data(self):
for device in self.__builds_data.keys():
# Sort builds by date
sorted_builds = sorted(self.__builds_data[device], key=lambda build: build['date'])
# Keep most recent builds
self.__builds_data[device] = sorted_builds
# Write data back into the builds.json file
with open(self.__builds_json_path, 'w') as builds_json_file:
json.dump(self.__builds_data, builds_json_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment