release-upload 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. '''
  3. Usage: https://github.com/cirosantilli/linux-kernel-module-cheat#release-zip
  4. Implementation:
  5. * https://stackoverflow.com/questions/5207269/how-to-release-a-build-artifact-asset-on-github-with-a-script/52354732#52354732
  6. * https://stackoverflow.com/questions/38153418/can-someone-give-a-python-requests-example-of-uploading-a-release-asset-in-githu/52354681#52354681
  7. '''
  8. import json
  9. import os
  10. import sys
  11. import urllib.error
  12. import common
  13. def main():
  14. repo = kwargs['github_repo_id']
  15. tag = 'sha-{}'.format(kwargs['sha'])
  16. upload_path = kwargs['release_zip_file']
  17. # Check the release already exists.
  18. try:
  19. _json = self.github_make_request(path='/releases/tags/' + tag)
  20. except urllib.error.HTTPError as e:
  21. if e.code == 404:
  22. release_exists = False
  23. else:
  24. raise e
  25. else:
  26. release_exists = True
  27. release_id = _json['id']
  28. # Create release if not yet created.
  29. if not release_exists:
  30. _json = self.github_make_request(
  31. authenticate=True,
  32. data=json.dumps({
  33. 'tag_name': tag,
  34. 'name': tag,
  35. 'prerelease': True,
  36. }).encode(),
  37. path='/releases'
  38. )
  39. release_id = _json['id']
  40. asset_name = os.path.split(upload_path)[1]
  41. # Clear the prebuilts for a upload.
  42. _json = self.github_make_request(
  43. path=('/releases/' + str(release_id) + '/assets'),
  44. )
  45. for asset in _json:
  46. if asset['name'] == asset_name:
  47. _json = self.github_make_request(
  48. authenticate=True,
  49. path=('/releases/assets/' + str(asset['id'])),
  50. method='DELETE',
  51. )
  52. break
  53. # Upload the prebuilt.
  54. with open(upload_path, 'br') as myfile:
  55. content = myfile.read()
  56. _json = self.github_make_request(
  57. authenticate=True,
  58. data=content,
  59. extra_headers={'Content-Type': 'application/zip'},
  60. path=('/releases/' + str(release_id) + '/assets'),
  61. subdomain='uploads',
  62. url_params={'name': asset_name},
  63. )
  64. if __name__ == '__main__':
  65. main()