config.py 4.8 KB

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