/Lib/distutils/config.py

http://unladen-swallow.googlecode.com/ · Python · 124 lines · 92 code · 13 blank · 19 comment · 23 complexity · 25995f9376917f9b68e02a454561ae1c MD5 · raw file

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