Home | History | Annotate | Download | only in distutils
      1 """distutils.pypirc
      2 
      3 Provides the PyPIRCCommand class, the base class for the command classes
      4 that uses .pypirc in the distutils.command package.
      5 """
      6 import os
      7 from ConfigParser import ConfigParser
      8 
      9 from distutils.cmd import Command
     10 
     11 DEFAULT_PYPIRC = """\
     12 [distutils]
     13 index-servers =
     14     pypi
     15 
     16 [pypi]
     17 username:%s
     18 password:%s
     19 """
     20 
     21 class PyPIRCCommand(Command):
     22     """Base command that knows how to handle the .pypirc file
     23     """
     24     DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
     25     DEFAULT_REALM = 'pypi'
     26     repository = None
     27     realm = None
     28 
     29     user_options = [
     30         ('repository=', 'r',
     31          "url of repository [default: %s]" % \
     32             DEFAULT_REPOSITORY),
     33         ('show-response', None,
     34          'display full response text from server')]
     35 
     36     boolean_options = ['show-response']
     37 
     38     def _get_rc_file(self):
     39         """Returns rc file path."""
     40         return os.path.join(os.path.expanduser('~'), '.pypirc')
     41 
     42     def _store_pypirc(self, username, password):
     43         """Creates a default .pypirc file."""
     44         rc = self._get_rc_file()
     45         f = os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0600), 'w')
     46         try:
     47             f.write(DEFAULT_PYPIRC % (username, password))
     48         finally:
     49             f.close()
     50 
     51     def _read_pypirc(self):
     52         """Reads the .pypirc file."""
     53         rc = self._get_rc_file()
     54         if os.path.exists(rc):
     55             self.announce('Using PyPI login from %s' % rc)
     56             repository = self.repository or self.DEFAULT_REPOSITORY
     57             config = ConfigParser()
     58             config.read(rc)
     59             sections = config.sections()
     60             if 'distutils' in sections:
     61                 # let's get the list of servers
     62                 index_servers = config.get('distutils', 'index-servers')
     63                 _servers = [server.strip() for server in
     64                             index_servers.split('\n')
     65                             if server.strip() != '']
     66                 if _servers == []:
     67                     # nothing set, let's try to get the default pypi
     68                     if 'pypi' in sections:
     69                         _servers = ['pypi']
     70                     else:
     71                         # the file is not properly defined, returning
     72                         # an empty dict
     73                         return {}
     74                 for server in _servers:
     75                     current = {'server': server}
     76                     current['username'] = config.get(server, 'username')
     77 
     78                     # optional params
     79                     for key, default in (('repository',
     80                                           self.DEFAULT_REPOSITORY),
     81                                          ('realm', self.DEFAULT_REALM),
     82                                          ('password', None)):
     83                         if config.has_option(server, key):
     84                             current[key] = config.get(server, key)
     85                         else:
     86                             current[key] = default
     87                     if (current['server'] == repository or
     88                         current['repository'] == repository):
     89                         return current
     90             elif 'server-login' in sections:
     91                 # old format
     92                 server = 'server-login'
     93                 if config.has_option(server, 'repository'):
     94                     repository = config.get(server, 'repository')
     95                 else:
     96                     repository = self.DEFAULT_REPOSITORY
     97                 return {'username': config.get(server, 'username'),
     98                         'password': config.get(server, 'password'),
     99                         'repository': repository,
    100                         'server': server,
    101                         'realm': self.DEFAULT_REALM}
    102 
    103         return {}
    104 
    105     def initialize_options(self):
    106         """Initialize options."""
    107         self.repository = None
    108         self.realm = None
    109         self.show_response = 0
    110 
    111     def finalize_options(self):
    112         """Finalizes options."""
    113         if self.repository is None:
    114             self.repository = self.DEFAULT_REPOSITORY
    115         if self.realm is None:
    116             self.realm = self.DEFAULT_REALM
    117