release-upload 2.5 KB

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