Created
July 9, 2012 11:08
-
-
Save piranha/3075846 to your computer and use it in GitHub Desktop.
Github upload script. Pretty much useless now.
This file contains 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
#!/usr/bin/env python | |
# | |
# (c) 2012 Alexander Solovyov under terms of ISC License | |
# to use, install dependencies: | |
# pip install opster requests | |
import os, sys, json | |
from subprocess import check_output | |
import requests | |
from requests.packages.urllib3.filepost import encode_multipart_formdata | |
from opster import command | |
TOKEN_NAME = 'GITHUB_UPLOAD_TOKEN' | |
def delete_and_retry(filename, repo): | |
url = 'https://api.github.com/repos/%s/downloads' % repo | |
files = requests.get(url).json | |
id = next(f['id'] for f in files if f['name'] == filename) | |
url = 'https://api.github.com/repos/%s/downloads/%s' % (repo, id) | |
headers = {'Authorization': 'bearer %s' % os.environ[TOKEN_NAME]} | |
res = requests.delete(url, headers=headers) | |
if res.status_code == 204: | |
print 'Previously uploaded file deleted' | |
else: | |
print 'Something went wrong', res | |
return upload(filename, repo=repo) | |
@command() | |
def upload(filename, | |
repo=('', 'origin', 'remote name or repo name in user/repo format')): | |
'''Upload a file to Github off-repo downloads | |
''' | |
if not os.path.exists(filename): | |
print 'File %s does not exist!' % filename | |
sys.exit(1) | |
if '/' not in repo: | |
repo = check_output(['git', 'config', 'remote.%s.url' % repo]) | |
repo = repo.split(':')[1].rsplit('.', 1)[0] | |
url = 'https://api.github.com/repos/%s/downloads' % repo | |
headers = {'Authorization': 'bearer %s' % os.environ[TOKEN_NAME]} | |
data = { | |
'name': filename, | |
'size': os.stat(filename).st_size | |
} | |
res = requests.post(url, headers=headers, data=json.dumps(data)) | |
if 'errors' in res.json: | |
if next((err for err in res.json['errors'] | |
if err['code'] == 'already_exists'), None): | |
return delete_and_retry(filename, repo) | |
else: | |
print 'Something went wrong:' | |
print res.json | |
sys.exit(1) | |
url = 'https://github.s3.amazonaws.com/' | |
# list because order of arguments is important for S3 | |
s3data = [ | |
('key', res.json['path']), | |
('acl', 'public-read'), | |
('success_action_status', '201'), | |
('Filename', res.json['name']), | |
('AWSAccessKeyId', res.json['accesskeyid']), | |
('Policy', res.json['policy']), | |
('Signature', res.json['signature']), | |
('Content-Type', res.json['mime_type']), | |
('file', open(filename, 'rb').read()) | |
] | |
body, ctype = encode_multipart_formdata(s3data) | |
res = requests.post(url, data=body, headers={'Content-Type': ctype}) | |
print 'Done! Go to your repo to check:' | |
print 'https://github.com/%s' % repo | |
if __name__ == '__main__': | |
if not TOKEN_NAME in os.environ: | |
print "set %s to auth token from github" % TOKEN_NAME | |
print ''' curl -u "$USER:$PASSWORD" -d '{"scopes": ["repo"], "note": "uploda script"}' https://api.github.com/authorizations''' | |
sys.exit(1) | |
upload.command() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment