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.

112 lines
3.8 KiB

  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. import base64
  4. import json
  5. import mimetypes
  6. import netrc
  7. import optparse
  8. import os
  9. import sys
  10. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. from youtube_dl.compat import (
  12. compat_basestring,
  13. compat_input,
  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/rg3/youtube-dl/releases'
  24. _UPLOADS_URL = 'https://uploads.github.com/repos/rg3/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._username = info[0]
  35. self._password = info[2]
  36. compat_print('Using GitHub credentials found in .netrc...')
  37. return
  38. else:
  39. compat_print('No GitHub credentials found in .netrc')
  40. except (IOError, netrc.NetrcParseError):
  41. compat_print('Unable to parse .netrc')
  42. self._username = compat_input(
  43. 'Type your GitHub username or email address and press [Return]: ')
  44. self._password = compat_getpass(
  45. 'Type your GitHub password and press [Return]: ')
  46. def _call(self, req):
  47. if isinstance(req, compat_basestring):
  48. req = sanitized_Request(req)
  49. # Authorizing manually since GitHub does not response with 401 with
  50. # WWW-Authenticate header set (see
  51. # https://developer.github.com/v3/#basic-authentication)
  52. b64 = base64.b64encode(
  53. ('%s:%s' % (self._username, self._password)).encode('utf-8')).decode('ascii')
  54. req.add_header('Authorization', 'Basic %s' % b64)
  55. response = self._opener.open(req).read().decode('utf-8')
  56. return json.loads(response)
  57. def list_releases(self):
  58. return self._call(self._API_URL)
  59. def create_release(self, tag_name, name=None, body='', draft=False, prerelease=False):
  60. data = {
  61. 'tag_name': tag_name,
  62. 'target_commitish': 'master',
  63. 'name': name,
  64. 'body': body,
  65. 'draft': draft,
  66. 'prerelease': prerelease,
  67. }
  68. req = sanitized_Request(self._API_URL, json.dumps(data).encode('utf-8'))
  69. return self._call(req)
  70. def create_asset(self, release_id, asset):
  71. asset_name = os.path.basename(asset)
  72. url = self._UPLOADS_URL % (release_id, asset_name)
  73. # Our files are small enough to be loaded directly into memory.
  74. data = open(asset, 'rb').read()
  75. req = sanitized_Request(url, data)
  76. mime_type, _ = mimetypes.guess_type(asset_name)
  77. req.add_header('Content-Type', mime_type or 'application/octet-stream')
  78. return self._call(req)
  79. def main():
  80. parser = optparse.OptionParser(usage='%prog VERSION BUILDPATH')
  81. options, args = parser.parse_args()
  82. if len(args) != 2:
  83. parser.error('Expected a version and a build directory')
  84. version, build_path = args
  85. releaser = GitHubReleaser(debuglevel=0)
  86. new_release = releaser.create_release(
  87. version, name='youtube-dl %s' % version, draft=True, prerelease=True)
  88. release_id = new_release['id']
  89. for asset in os.listdir(build_path):
  90. compat_print('Uploading %s...' % asset)
  91. releaser.create_asset(release_id, os.path.join(build_path, asset))
  92. if __name__ == '__main__':
  93. main()