2017-02-21 04:00:54 +01:00
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import platform
|
2017-02-23 16:16:10 +01:00
|
|
|
import random
|
2017-02-21 04:00:54 +01:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-02-21 22:13:41 +01:00
|
|
|
|
2017-02-21 04:00:54 +01:00
|
|
|
|
|
|
|
import github
|
|
|
|
import requests
|
|
|
|
import uritemplate
|
|
|
|
|
|
|
|
from lbrynet.core import log_support
|
|
|
|
|
|
|
|
|
|
|
|
def main(args=None):
|
|
|
|
current_tag = None
|
|
|
|
try:
|
|
|
|
current_tag = subprocess.check_output(
|
|
|
|
['git', 'describe', '--exact-match', 'HEAD']).strip()
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
log.info('Stopping as we are not currently on a tag')
|
|
|
|
return
|
2017-02-21 19:13:44 +01:00
|
|
|
|
|
|
|
if 'GH_TOKEN' not in os.environ:
|
|
|
|
print 'Must set GH_TOKEN in order to publish assets to a release'
|
|
|
|
return
|
|
|
|
|
|
|
|
gh_token = os.environ['GH_TOKEN']
|
|
|
|
auth = github.Github(gh_token)
|
|
|
|
app_repo = auth.get_repo('lbryio/lbry-app')
|
|
|
|
# TODO: switch lbryio/lbrynet-daemon to lbryio/lbry
|
|
|
|
daemon_repo = auth.get_repo('lbryio/lbrynet-daemon')
|
|
|
|
|
2017-02-21 04:00:54 +01:00
|
|
|
if not check_repo_has_tag(app_repo, current_tag):
|
|
|
|
log.info('Tag %s is not in repo %s', current_tag, app_repo)
|
|
|
|
# TODO: maybe this should be an error
|
|
|
|
return
|
|
|
|
|
2017-02-21 22:13:41 +01:00
|
|
|
daemon = get_daemon_artifact()
|
2017-02-23 16:34:30 +01:00
|
|
|
os.rename(daemon, 'daemon')
|
2017-02-21 04:00:54 +01:00
|
|
|
release = get_release(daemon_repo, current_tag)
|
2017-02-23 16:34:30 +01:00
|
|
|
upload_asset(release, 'daemon', gh_token)
|
2017-02-21 04:00:54 +01:00
|
|
|
|
2017-02-23 00:00:19 +01:00
|
|
|
app = get_app_artifact()
|
|
|
|
release = get_release(app_repo, current_tag)
|
|
|
|
upload_asset(release, app, gh_token)
|
|
|
|
|
2017-02-21 04:00:54 +01:00
|
|
|
|
|
|
|
def check_repo_has_tag(repo, target_tag):
|
|
|
|
tags = repo.get_tags().get_page(0)
|
|
|
|
for tag in tags:
|
|
|
|
if tag.name == target_tag:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_release(current_repo, current_tag):
|
2017-02-21 04:55:53 +01:00
|
|
|
for release in current_repo.get_releases():
|
|
|
|
if release.tag_name == current_tag:
|
|
|
|
return release
|
|
|
|
raise Exception('No release for {} was found'.format(current_tag))
|
2017-02-21 04:00:54 +01:00
|
|
|
|
|
|
|
|
2017-02-21 22:13:41 +01:00
|
|
|
def get_app_artifact():
|
2017-02-21 04:00:54 +01:00
|
|
|
system = platform.system()
|
|
|
|
if system == 'Darwin':
|
|
|
|
return glob.glob('dist/mac/LBRY*.dmg')[0]
|
|
|
|
elif system == 'Linux':
|
|
|
|
return glob.glob('dist/LBRY*.deb')[0]
|
|
|
|
elif system == 'Windows':
|
|
|
|
return glob.glob('dist/LBRY*.exe')[0]
|
|
|
|
else:
|
|
|
|
raise Exception("I don't know about any artifact on {}".format(system))
|
|
|
|
|
|
|
|
|
2017-02-21 22:13:41 +01:00
|
|
|
def get_daemon_artifact():
|
|
|
|
return glob.glob('dist/*.zip')[0]
|
2017-02-21 04:00:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def upload_asset(release, asset_to_upload, token):
|
2017-02-23 15:51:59 +01:00
|
|
|
basename = os.path.basename(asset_to_upload)
|
|
|
|
if is_asset_already_uploaded(release, basename):
|
|
|
|
return
|
2017-02-22 23:47:30 +01:00
|
|
|
count = 0
|
2017-02-23 15:51:59 +01:00
|
|
|
uploaders = [_requests_uploader, _curl_uploader]
|
2017-02-22 23:47:30 +01:00
|
|
|
while count < 10:
|
2017-02-23 15:51:59 +01:00
|
|
|
uploader = uploaders[count % len(uploaders)]
|
2017-02-22 23:47:30 +01:00
|
|
|
try:
|
2017-02-23 16:16:10 +01:00
|
|
|
return _upload_asset(release, asset_to_upload, token, uploader)
|
2017-02-22 23:47:30 +01:00
|
|
|
except Exception:
|
2017-02-23 15:51:59 +01:00
|
|
|
log.exception('Failed to upload')
|
2017-02-22 23:47:30 +01:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
2017-02-23 16:16:10 +01:00
|
|
|
def _upload_asset(release, asset_to_upload, token, uploader):
|
2017-02-21 04:00:54 +01:00
|
|
|
basename = os.path.basename(asset_to_upload)
|
2017-02-23 16:16:10 +01:00
|
|
|
upload_uri = uritemplate.expand(
|
|
|
|
release.upload_url,
|
|
|
|
{'name': ''.join([random.choice('abcdef') for _ in range(10)])}
|
|
|
|
)
|
2017-02-23 15:51:59 +01:00
|
|
|
output = uploader(upload_uri, asset_to_upload, token)
|
|
|
|
if 'errors' in output:
|
|
|
|
raise Exception(output)
|
|
|
|
else:
|
|
|
|
log.info('Successfully uploaded to %s', output['browser_download_url'])
|
|
|
|
|
|
|
|
|
|
|
|
def _requests_uploader(upload_uri, asset_to_upload, token):
|
|
|
|
log.info('Using requests to upload %s to %s', asset_to_upload, upload_uri)
|
|
|
|
with open(asset_to_upload, 'rb') as f:
|
|
|
|
response = requests.post(upload_uri, data=f, auth=('', token))
|
|
|
|
output = response.json()
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def _curl_uploader(upload_uri, asset_to_upload, token):
|
2017-02-21 04:00:54 +01:00
|
|
|
# using requests.post fails miserably with SSL EPIPE errors. I spent
|
|
|
|
# half a day trying to debug before deciding to switch to curl.
|
2017-02-21 22:13:41 +01:00
|
|
|
#
|
|
|
|
# TODO: actually set the content type
|
2017-02-23 15:51:59 +01:00
|
|
|
log.info('Using curl to upload %s to %s', asset_to_upload, upload_uri)
|
2017-02-21 04:00:54 +01:00
|
|
|
cmd = [
|
2017-02-22 22:24:19 +01:00
|
|
|
'curl',
|
|
|
|
'-sS',
|
|
|
|
'-X', 'POST',
|
|
|
|
'-u', ':{}'.format(os.environ['GH_TOKEN']),
|
2017-02-23 15:51:59 +01:00
|
|
|
'--header', 'Content-Type:application/octet-stream',
|
|
|
|
'--data-binary', str('@{}'.format(asset_to_upload)),
|
2017-02-23 05:53:35 +01:00
|
|
|
str(upload_uri)
|
2017-02-21 04:00:54 +01:00
|
|
|
]
|
2017-02-22 22:11:52 +01:00
|
|
|
print 'Calling curl:'
|
|
|
|
print cmd
|
|
|
|
print
|
2017-02-22 20:33:37 +01:00
|
|
|
p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
print 'curl return code:', p.returncode
|
|
|
|
if stderr:
|
|
|
|
print 'stderr output from curl:'
|
|
|
|
print stderr
|
|
|
|
print 'stdout from curl:'
|
|
|
|
print stdout
|
2017-02-22 22:24:19 +01:00
|
|
|
output = json.loads(stdout)
|
2017-02-23 15:51:59 +01:00
|
|
|
return output
|
2017-02-21 04:00:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_asset_already_uploaded(release, basename):
|
|
|
|
for asset in release.raw_data['assets']:
|
|
|
|
if asset['name'] == basename:
|
|
|
|
log.info('File %s has already been uploaded to %s', basename, release.tag_name)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
log = logging.getLogger('release-on-tag')
|
2017-02-21 22:13:41 +01:00
|
|
|
log_support.configure_console(level='INFO')
|
2017-02-21 04:00:54 +01:00
|
|
|
sys.exit(main())
|
|
|
|
else:
|
|
|
|
log = logging.getLogger(__name__)
|