You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB

  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. import io
  4. import json
  5. import mimetypes
  6. import netrc
  7. import optparse
  8. import os
  9. import re
  10. import sys
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. from youtube_dl.compat import (
  13. compat_basestring,
  14. compat_getpass,
  15. compat_print,
  16. compat_urllib_request,
  17. )
  18. from youtube_dl.utils import (
  19. make_HTTPS_handler,
  20. sanitized_Request,
  21. )
  22. class GitHubReleaser(object):
  23. _API_URL = 'https://api.github.com/repos/ytdl-org/youtube-dl/releases'
  24. _UPLOADS_URL = 'https://uploads.github.com/repos/ytdl-org/youtube-dl/releases/%s/assets?name=%s'
  25. _NETRC_MACHINE = 'github.com'
  26. def __init__(self, debuglevel=0):
  27. self._init_github_account()
  28. https_handler = make_HTTPS_handler({}, debuglevel=debuglevel)
  29. self._opener = compat_urllib_request.build_opener(https_handler)
  30. def _init_github_account(self):
  31. try:
  32. info = netrc.netrc().authenticators(self._NETRC_MACHINE)
  33. if info is not None:
  34. self._token = info[2]
  35. compat_print('Using GitHub credentials found in .netrc...')
  36. return
  37. else:
  38. compat_print('No GitHub credentials found in .netrc')
  39. except (IOError, netrc.NetrcParseError):
  40. compat_print('Unable to parse .netrc')
  41. self._token = compat_getpass(
  42. 'Type your GitHub PAT (personal access token) and press [Return]: ')
  43. def _call(self, req):
  44. if isinstance(req, compat_basestring):
  45. req = sanitized_Request(req)
  46. req.add_header('Authorization', 'token %s' % self._token)
  47. response = self._opener.open(req).read().decode('utf-8')
  48. return json.loads(response)
  49. def list_releases(self):
  50. return self._call(self._API_URL)
  51. def create_release(self, tag_name, name=None, body='', draft=False, prerelease=False):
  52. data = {
  53. 'tag_name': tag_name,
  54. 'target_commitish': 'master',
  55. 'name': name,
  56. 'body': body,
  57. 'draft': draft,
  58. 'prerelease': prerelease,
  59. }
  60. req = sanitized_Request(self._API_URL, json.dumps(data).encode('utf-8'))
  61. return self._call(req)
  62. def create_asset(self, release_id, asset):
  63. asset_name = os.path.basename(asset)
  64. url = self._UPLOADS_URL % (release_id, asset_name)
  65. # Our files are small enough to be loaded directly into memory.
  66. data = open(asset, 'rb').read()
  67. req = sanitized_Request(url, data)
  68. mime_type, _ = mimetypes.guess_type(asset_name)
  69. req.add_header('Content-Type', mime_type or 'application/octet-stream')
  70. return self._call(req)
  71. def main():
  72. parser = optparse.OptionParser(usage='%prog CHANGELOG VERSION BUILDPATH')
  73. options, args = parser.parse_args()
  74. if len(args) != 3:
  75. parser.error('Expected a version and a build directory')
  76. changelog_file, version, build_path = args
  77. with io.open(changelog_file, encoding='utf-8') as inf:
  78. changelog = inf.read()
  79. mobj = re.search(r'(?s)version %s\n{2}(.+?)\n{3}' % version, changelog)
  80. body = mobj.group(1) if mobj else ''
  81. releaser = GitHubReleaser()
  82. new_release = releaser.create_release(
  83. version, name='youtube-dl %s' % version, body=body)
  84. release_id = new_release['id']
  85. for asset in os.listdir(build_path):
  86. compat_print('Uploading %s...' % asset)
  87. releaser.create_asset(release_id, os.path.join(build_path, asset))
  88. if __name__ == '__main__':
  89. main()