github.com/vipernet-xyz/tendermint-core@v0.32.0/scripts/release_management/github-upload.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Upload a file to a GitHub draft release. --id and --file are required.
     4  # Optimized for CircleCI
     5  
     6  import json
     7  import os
     8  import re
     9  import argparse
    10  import mimetypes
    11  import httplib
    12  from base64 import b64encode
    13  
    14  
    15  def request(baseurl, path, mimetype, mimeencoding, data):
    16    user_and_pass = b64encode(b"{0}:{1}".format(os.environ['GITHUB_USERNAME'], os.environ['GITHUB_TOKEN'])).decode("ascii")
    17    
    18    headers = {
    19      'User-Agent': 'tenderbot',
    20      'Accept': 'application/vnd.github.v3.raw+json',
    21      'Authorization': 'Basic %s' % user_and_pass,
    22      'Content-Type': mimetype,
    23      'Content-Encoding': mimeencoding
    24    }
    25  
    26    conn = httplib.HTTPSConnection(baseurl, timeout=5)
    27    conn.request('POST', path, data, headers)
    28    response = conn.getresponse()
    29    if response.status < 200 or response.status > 299:
    30      print(response)
    31      conn.close()
    32      raise IOError(response.reason)
    33    responsedata = response.read()
    34    conn.close()
    35    return json.loads(responsedata)
    36  
    37  
    38  if __name__ == "__main__":
    39    parser = argparse.ArgumentParser()
    40    parser.add_argument("--id", help="GitHub release ID", required=True, type=int)
    41    parser.add_argument("--file", default="/tmp/workspace/tendermint_{0}_{1}_{2}.zip".format(os.environ.get('CIRCLE_TAG'),os.environ.get('GOOS'),os.environ.get('GOARCH')), help="File to upload")
    42    parser.add_argument("--return-id-only", help="Return only the release ID after upload to GitHub.", action='store_true')
    43    args = parser.parse_args()
    44  
    45    if not os.environ.has_key('GITHUB_USERNAME'):
    46      raise parser.error('GITHUB_USERNAME not set.')
    47  
    48    if not os.environ.has_key('GITHUB_TOKEN'):
    49      raise parser.error('GITHUB_TOKEN not set.')
    50  
    51    mimetypes.init()
    52    filename = os.path.basename(args.file)
    53    mimetype,mimeencoding = mimetypes.guess_type(filename, strict=False)
    54    if mimetype is None:
    55      mimetype = 'application/zip'
    56    if mimeencoding is None:
    57      mimeencoding = 'utf8'
    58  
    59    with open(args.file,'rb') as f:
    60      asset = f.read()
    61  
    62    result = request('uploads.github.com', '/repos/tendermint/tendermint/releases/{0}/assets?name={1}'.format(args.id, filename), mimetype, mimeencoding, asset)
    63  
    64    if args.return_id_only:
    65      print(result['id'])
    66    else:
    67      print(result['browser_download_url'])
    68