config.py 4.8 KB

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