upload_2_3.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. def upload_file(self, command, pyversion, filename):
  2. # Makes sure the repository URL is compliant
  3. schema, netloc, url, params, query, fragments = \
  4. urlparse(self.repository)
  5. if params or query or fragments:
  6. raise AssertionError("Incompatible url %s" % self.repository)
  7. if schema not in ('http', 'https'):
  8. raise AssertionError("unsupported schema " + schema)
  9. # Sign if requested
  10. if self.sign:
  11. gpg_args = ["gpg", "--detach-sign", "-a", filename]
  12. if self.identity:
  13. gpg_args[2:2] = ["--local-user", self.identity]
  14. spawn(gpg_args,
  15. dry_run=self.dry_run)
  16. # Fill in the data - send all the meta-data in case we need to
  17. # register a new release
  18. with open(filename, 'rb') as f:
  19. content = f.read()
  20. meta = self.distribution.metadata
  21. data = {
  22. # action
  23. ':action': 'file_upload',
  24. 'protocol_version': '1',
  25. # identify release
  26. 'name': meta.get_name(),
  27. 'version': meta.get_version(),
  28. # file content
  29. 'content': (os.path.basename(filename), content),
  30. 'filetype': command,
  31. 'pyversion': pyversion,
  32. 'md5_digest': hashlib.md5(content).hexdigest(),
  33. # additional meta-data
  34. 'metadata_version': str(meta.get_metadata_version()),
  35. 'summary': meta.get_description(),
  36. 'home_page': meta.get_url(),
  37. 'author': meta.get_contact(),
  38. 'author_email': meta.get_contact_email(),
  39. 'license': meta.get_licence(),
  40. 'description': meta.get_long_description(),
  41. 'keywords': meta.get_keywords(),
  42. 'platform': meta.get_platforms(),
  43. 'classifiers': meta.get_classifiers(),
  44. 'download_url': meta.get_download_url(),
  45. # PEP 314
  46. 'provides': meta.get_provides(),
  47. 'requires': meta.get_requires(),
  48. 'obsoletes': meta.get_obsoletes(),
  49. }
  50. data['comment'] = ''
  51. if self.sign:
  52. data['gpg_signature'] = (os.path.basename(filename) + ".asc",
  53. open(filename+".asc", "rb").read())
  54. # set up the authentication
  55. user_pass = (self.username + ":" + self.password).encode('ascii')
  56. # The exact encoding of the authentication string is debated.
  57. # Anyway PyPI only accepts ascii for both username or password.
  58. auth = "Basic " + standard_b64encode(user_pass).decode('ascii')
  59. # Build up the MIME payload for the POST data
  60. boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
  61. sep_boundary = b'\r\n--' + boundary.encode('ascii')
  62. end_boundary = sep_boundary + b'--\r\n'
  63. body = io.BytesIO()
  64. for key, value in data.items():
  65. title = '\r\nContent-Disposition: form-data; name="%s"' % key
  66. # handle multiple entries for the same name
  67. if not isinstance(value, list):
  68. value = [value]
  69. for value in value:
  70. if type(value) is tuple:
  71. title += '; filename="%s"' % value[0]
  72. value = value[1]
  73. else:
  74. value = str(value).encode('utf-8')
  75. body.write(sep_boundary)
  76. body.write(title.encode('utf-8'))
  77. body.write(b"\r\n\r\n")
  78. body.write(value)
  79. body.write(end_boundary)
  80. body = body.getvalue()
  81. msg = "Submitting %s to %s" % (filename, self.repository)
  82. self.announce(msg, log.INFO)
  83. # build the Request
  84. headers = {
  85. 'Content-type': 'multipart/form-data; boundary=%s' % boundary,
  86. 'Content-length': str(len(body)),
  87. 'Authorization': auth,
  88. }
  89. request = Request(self.repository, data=body,
  90. headers=headers)
  91. # send the data
  92. try:
  93. result = urlopen(request)
  94. status = result.getcode()
  95. reason = result.msg
  96. except HTTPError as e:
  97. status = e.code
  98. reason = e.msg
  99. except OSError as e:
  100. self.announce(str(e), log.ERROR)
  101. raise
  102. if status == 200:
  103. self.announce('Server response (%s): %s' % (status, reason),
  104. log.INFO)
  105. if self.show_response:
  106. text = getattr(self, '_read_pypi_response',
  107. lambda x: None)(result)
  108. if text is not None:
  109. msg = '\n'.join(('-' * 75, text, '-' * 75))
  110. self.announce(msg, log.INFO)
  111. else:
  112. msg = 'Upload failed (%s): %s' % (status, reason)
  113. self.announce(msg, log.ERROR)
  114. raise DistutilsError(msg)