config.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 ConfigParser
  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://pypi.python.org/pypi'
  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. f = os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0600), 'w')
  37. try:
  38. f.write(DEFAULT_PYPIRC % (username, password))
  39. finally:
  40. f.close()
  41. def _read_pypirc(self):
  42. """Reads the .pypirc file."""
  43. rc = self._get_rc_file()
  44. if os.path.exists(rc):
  45. self.announce('Using PyPI login from %s' % rc)
  46. repository = self.repository or self.DEFAULT_REPOSITORY
  47. config = ConfigParser()
  48. config.read(rc)
  49. sections = config.sections()
  50. if 'distutils' in sections:
  51. # let's get the list of servers
  52. index_servers = config.get('distutils', 'index-servers')
  53. _servers = [server.strip() for server in
  54. index_servers.split('\n')
  55. if server.strip() != '']
  56. if _servers == []:
  57. # nothing set, let's try to get the default pypi
  58. if 'pypi' in sections:
  59. _servers = ['pypi']
  60. else:
  61. # the file is not properly defined, returning
  62. # an empty dict
  63. return {}
  64. for server in _servers:
  65. current = {'server': server}
  66. current['username'] = config.get(server, 'username')
  67. # optional params
  68. for key, default in (('repository',
  69. self.DEFAULT_REPOSITORY),
  70. ('realm', self.DEFAULT_REALM),
  71. ('password', None)):
  72. if config.has_option(server, key):
  73. current[key] = config.get(server, key)
  74. else:
  75. current[key] = default
  76. if (current['server'] == repository or
  77. current['repository'] == repository):
  78. return current
  79. elif 'server-login' in sections:
  80. # old format
  81. server = 'server-login'
  82. if config.has_option(server, 'repository'):
  83. repository = config.get(server, 'repository')
  84. else:
  85. repository = self.DEFAULT_REPOSITORY
  86. return {'username': config.get(server, 'username'),
  87. 'password': config.get(server, 'password'),
  88. 'repository': repository,
  89. 'server': server,
  90. 'realm': self.DEFAULT_REALM}
  91. return {}
  92. def initialize_options(self):
  93. """Initialize options."""
  94. self.repository = None
  95. self.realm = None
  96. self.show_response = 0
  97. def finalize_options(self):
  98. """Finalizes options."""
  99. if self.repository is None:
  100. self.repository = self.DEFAULT_REPOSITORY
  101. if self.realm is None:
  102. self.realm = self.DEFAULT_REALM