+++ /dev/null
-"""distutils.pypirc\r
-\r
-Provides the PyPIRCCommand class, the base class for the command classes\r
-that uses .pypirc in the distutils.command package.\r
-"""\r
-import os\r
-from ConfigParser import ConfigParser\r
-\r
-from distutils.cmd import Command\r
-\r
-DEFAULT_PYPIRC = """\\r
-[distutils]\r
-index-servers =\r
- pypi\r
-\r
-[pypi]\r
-username:%s\r
-password:%s\r
-"""\r
-\r
-class PyPIRCCommand(Command):\r
- """Base command that knows how to handle the .pypirc file\r
- """\r
- DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'\r
- DEFAULT_REALM = 'pypi'\r
- repository = None\r
- realm = None\r
-\r
- user_options = [\r
- ('repository=', 'r',\r
- "url of repository [default: %s]" % \\r
- DEFAULT_REPOSITORY),\r
- ('show-response', None,\r
- 'display full response text from server')]\r
-\r
- boolean_options = ['show-response']\r
-\r
- def _get_rc_file(self):\r
- """Returns rc file path."""\r
- return os.path.join(os.path.expanduser('~'), '.pypirc')\r
-\r
- def _store_pypirc(self, username, password):\r
- """Creates a default .pypirc file."""\r
- rc = self._get_rc_file()\r
- f = open(rc, 'w')\r
- try:\r
- f.write(DEFAULT_PYPIRC % (username, password))\r
- finally:\r
- f.close()\r
- try:\r
- os.chmod(rc, 0600)\r
- except OSError:\r
- # should do something better here\r
- pass\r
-\r
- def _read_pypirc(self):\r
- """Reads the .pypirc file."""\r
- rc = self._get_rc_file()\r
- if os.path.exists(rc):\r
- self.announce('Using PyPI login from %s' % rc)\r
- repository = self.repository or self.DEFAULT_REPOSITORY\r
- config = ConfigParser()\r
- config.read(rc)\r
- sections = config.sections()\r
- if 'distutils' in sections:\r
- # let's get the list of servers\r
- index_servers = config.get('distutils', 'index-servers')\r
- _servers = [server.strip() for server in\r
- index_servers.split('\n')\r
- if server.strip() != '']\r
- if _servers == []:\r
- # nothing set, let's try to get the default pypi\r
- if 'pypi' in sections:\r
- _servers = ['pypi']\r
- else:\r
- # the file is not properly defined, returning\r
- # an empty dict\r
- return {}\r
- for server in _servers:\r
- current = {'server': server}\r
- current['username'] = config.get(server, 'username')\r
-\r
- # optional params\r
- for key, default in (('repository',\r
- self.DEFAULT_REPOSITORY),\r
- ('realm', self.DEFAULT_REALM),\r
- ('password', None)):\r
- if config.has_option(server, key):\r
- current[key] = config.get(server, key)\r
- else:\r
- current[key] = default\r
- if (current['server'] == repository or\r
- current['repository'] == repository):\r
- return current\r
- elif 'server-login' in sections:\r
- # old format\r
- server = 'server-login'\r
- if config.has_option(server, 'repository'):\r
- repository = config.get(server, 'repository')\r
- else:\r
- repository = self.DEFAULT_REPOSITORY\r
- return {'username': config.get(server, 'username'),\r
- 'password': config.get(server, 'password'),\r
- 'repository': repository,\r
- 'server': server,\r
- 'realm': self.DEFAULT_REALM}\r
-\r
- return {}\r
-\r
- def initialize_options(self):\r
- """Initialize options."""\r
- self.repository = None\r
- self.realm = None\r
- self.show_response = 0\r
-\r
- def finalize_options(self):\r
- """Finalizes options."""\r
- if self.repository is None:\r
- self.repository = self.DEFAULT_REPOSITORY\r
- if self.realm is None:\r
- self.realm = self.DEFAULT_REALM\r