config.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """distutils.pypirc
  2. Provides the PyPIRCCommand class, the base class for the command classes
  3. that uses .pypirc in the distutils.command package.
  4. """
  5. import os
  6. from configparser import RawConfigParser
  7. from distutils.cmd import Command
  8. DEFAULT_PYPIRC = """\
  9. [distutils]
  10. index-servers =
  11. pypi
  12. [pypi]
  13. username:%s
  14. password:%s
  15. """
  16. class PyPIRCCommand(Command):
  17. """Base command that knows how to handle the .pypirc file
  18. """
  19. DEFAULT_REPOSITORY = 'https://upload.pypi.org/legacy/'
  20. DEFAULT_REALM = 'pypi'
  21. repository = None
  22. realm = None
  23. user_options = [
  24. ('repository=', 'r',
  25. "url of repository [default: %s]" % \
  26. DEFAULT_REPOSITORY),
  27. ('show-response', None,
  28. 'display full response text from server')]
  29. boolean_options = ['show-response']
  30. def _get_rc_file(self):
  31. """Returns rc file path."""
  32. return os.path.join(os.path.expanduser('~'), '.pypirc')
  33. def _store_pypirc(self, username, password):
  34. """Creates a default .pypirc file."""
  35. rc = self._get_rc_file()
  36. with os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
  37. f.write(DEFAULT_PYPIRC % (username, password))
  38. def _read_pypirc(self):
  39. """Reads the .pypirc file."""
  40. rc = self._get_rc_file()
  41. if os.path.exists(rc):
  42. self.announce('Using PyPI login from %s' % rc)
  43. repository = self.repository or self.DEFAULT_REPOSITORY
  44. config = RawConfigParser()
  45. config.read(rc)
  46. sections = config.sections()
  47. if 'distutils' in sections:
  48. # let's get the list of servers
  49. index_servers = config.get('distutils', 'index-servers')
  50. _servers = [server.strip() for server in
  51. index_servers.split('\n')
  52. if server.strip() != '']
  53. if _servers == []:
  54. # nothing set, let's try to get the default pypi
  55. if 'pypi' in sections:
  56. _servers = ['pypi']
  57. else:
  58. # the file is not properly defined, returning
  59. # an empty dict
  60. return {}
  61. for server in _servers:
  62. current = {'server': server}
  63. current['username'] = config.get(server, 'username')
  64. # optional params
  65. for key, default in (('repository',
  66. self.DEFAULT_REPOSITORY),
  67. ('realm', self.DEFAULT_REALM),
  68. ('password', None)):
  69. if config.has_option(server, key):
  70. current[key] = config.get(server, key)
  71. else:
  72. current[key] = default
  73. # work around people having "repository" for the "pypi"
  74. # section of their config set to the HTTP (rather than
  75. # HTTPS) URL
  76. if (server == 'pypi' and
  77. repository in (self.DEFAULT_REPOSITORY, 'pypi')):
  78. current['repository'] = self.DEFAULT_REPOSITORY
  79. return current
  80. if (current['server'] == repository or
  81. current['repository'] == repository):
  82. return current
  83. elif 'server-login' in sections:
  84. # old format
  85. server = 'server-login'
  86. if config.has_option(server, 'repository'):
  87. repository = config.get(server, 'repository')
  88. else:
  89. repository = self.DEFAULT_REPOSITORY
  90. return {'username': config.get(server, 'username'),
  91. 'password': config.get(server, 'password'),
  92. 'repository': repository,
  93. 'server': server,
  94. 'realm': self.DEFAULT_REALM}
  95. return {}
  96. def _read_pypi_response(self, response):
  97. """Read and decode a PyPI HTTP response."""
  98. import cgi
  99. content_type = response.getheader('content-type', 'text/plain')
  100. encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii')
  101. return response.read().decode(encoding)
  102. def initialize_options(self):
  103. """Initialize options."""
  104. self.repository = None
  105. self.realm = None
  106. self.show_response = 0
  107. def finalize_options(self):
  108. """Finalizes options."""
  109. if self.repository is None:
  110. self.repository = self.DEFAULT_REPOSITORY
  111. if self.realm is None:
  112. self.realm = self.DEFAULT_REALM