release-upload 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import subprocess
  5. import sys
  6. import urllib.error
  7. import common
  8. from shell_helpers import LF
  9. class Main(common.LkmcCliFunction):
  10. def __init__(self):
  11. super().__init__(
  12. description='''\
  13. https://github.com/cirosantilli/linux-kernel-module-cheat#release-upload
  14. ''',
  15. )
  16. def timed_main(self):
  17. # https://stackoverflow.com/questions/3404936/show-which-git-tag-you-are-on
  18. tag = subprocess.check_output([
  19. 'git',
  20. 'describe',
  21. '--exact-match',
  22. '--tags'
  23. ]).decode().rstrip()
  24. upload_path = self.env['release_zip_file']
  25. # Check the release already exists.
  26. try:
  27. _json = self.github_make_request(path='/releases/tags/' + tag)
  28. except urllib.error.HTTPError as e:
  29. if e.code == 404:
  30. release_exists = False
  31. else:
  32. raise e
  33. else:
  34. release_exists = True
  35. release_id = _json['id']
  36. # Create release if not yet created.
  37. if not release_exists:
  38. _json = self.github_make_request(
  39. authenticate=True,
  40. data=json.dumps({
  41. 'tag_name': tag,
  42. 'name': tag,
  43. 'prerelease': True,
  44. }).encode(),
  45. path='/releases'
  46. )
  47. release_id = _json['id']
  48. asset_name = os.path.split(upload_path)[1]
  49. # Clear the prebuilts for a upload.
  50. _json = self.github_make_request(
  51. path=('/releases/' + str(release_id) + '/assets'),
  52. )
  53. for asset in _json:
  54. if asset['name'] == asset_name:
  55. _json = self.github_make_request(
  56. authenticate=True,
  57. path=('/releases/assets/' + str(asset['id'])),
  58. method='DELETE',
  59. )
  60. break
  61. # Upload the prebuilt.
  62. self.log_info('Uploading the release, this may take several seconds / a few minutes.')
  63. with open(upload_path, 'br') as myfile:
  64. content = myfile.read()
  65. _json = self.github_make_request(
  66. authenticate=True,
  67. data=content,
  68. extra_headers={'Content-Type': 'application/zip'},
  69. path=('/releases/' + str(release_id) + '/assets'),
  70. subdomain='uploads',
  71. url_params={'name': asset_name},
  72. )
  73. if __name__ == '__main__':
  74. Main().cli()