upload_docs_6.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. def upload_file(self, filename):
  2. with open(filename, 'rb') as f:
  3. content = f.read()
  4. meta = self.distribution.metadata
  5. data = {
  6. ':action': 'doc_upload',
  7. 'name': meta.get_name(),
  8. 'content': (os.path.basename(filename), content),
  9. }
  10. # set up the authentication
  11. credentials = _encode(self.username + ':' + self.password)
  12. credentials = standard_b64encode(credentials)
  13. if six.PY3:
  14. credentials = credentials.decode('ascii')
  15. auth = "Basic " + credentials
  16. body, ct = self._build_multipart(data)
  17. self.announce("Submitting documentation to %s" % (self.repository),
  18. log.INFO)
  19. # build the Request
  20. # We can't use urllib2 since we need to send the Basic
  21. # auth right with the first request
  22. schema, netloc, url, params, query, fragments = \
  23. urllib.parse.urlparse(self.repository)
  24. assert not params and not query and not fragments
  25. if schema == 'http':
  26. conn = http_client.HTTPConnection(netloc)
  27. elif schema == 'https':
  28. conn = http_client.HTTPSConnection(netloc)
  29. else:
  30. raise AssertionError("unsupported schema " + schema)
  31. data = ''
  32. try:
  33. conn.connect()
  34. conn.putrequest("POST", url)
  35. content_type = ct
  36. conn.putheader('Content-type', content_type)
  37. conn.putheader('Content-length', str(len(body)))
  38. conn.putheader('Authorization', auth)
  39. conn.endheaders()
  40. conn.send(body)
  41. except socket.error as e:
  42. self.announce(str(e), log.ERROR)
  43. return
  44. r = conn.getresponse()
  45. if r.status == 200:
  46. self.announce('Server response (%s): %s' % (r.status, r.reason),
  47. log.INFO)
  48. elif r.status == 301:
  49. location = r.getheader('Location')
  50. if location is None:
  51. location = 'https://pythonhosted.org/%s/' % meta.get_name()
  52. self.announce('Upload successful. Visit %s' % location,
  53. log.INFO)
  54. else:
  55. self.announce('Upload failed (%s): %s' % (r.status, r.reason),
  56. log.ERROR)
  57. if self.show_response:
  58. print('-' * 75, r.read(), '-' * 75)