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 = open(rc, 'w')
     46         try:
     47             f.write(DEFAULT_PYPIRC % (username, password))
     48         finally:
     49             f.close()
     50         try:
     51             os.chmod(rc, 0600)
     52         except OSError:
     53             # should do something better here

     54             pass
     55 
     56     def _read_pypirc(self):
     57         """Reads the .pypirc file."""
     58         rc = self._get_rc_file()
     59         if os.path.exists(rc):
     60             self.announce('Using PyPI login from %s' % rc)
     61             repository = self.repository or self.DEFAULT_REPOSITORY
     62             config = ConfigParser()
     63             config.read(rc)
     64             sections = config.sections()
     65             if 'distutils' in sections:
     66                 # let's get the list of servers

     67                 index_servers = config.get('distutils', 'index-servers')
     68                 _servers = [server.strip() for server in
     69                             index_servers.split('\n')
     70                             if server.strip() != '']
     71                 if _servers == []:
     72                     # nothing set, let's try to get the default pypi

     73                     if 'pypi' in sections:
     74                         _servers = ['pypi']
     75                     else:
     76                         # the file is not properly defined, returning

     77                         # an empty dict

     78                         return {}
     79                 for server in _servers:
     80                     current = {'server': server}
     81                     current['username'] = config.get(server, 'username')
     82 
     83                     # optional params

     84                     for key, default in (('repository',
     85                                           self.DEFAULT_REPOSITORY),
     86                                          ('realm', self.DEFAULT_REALM),
     87                                          ('password', None)):
     88                         if config.has_option(server, key):
     89                             current[key] = config.get(server, key)
     90                         else:
     91                             current[key] = default
     92                     if (current['server'] == repository or
     93                         current['repository'] == repository):
     94                         return current
     95             elif 'server-login' in sections:
     96                 # old format

     97                 server = 'server-login'
     98                 if config.has_option(server, 'repository'):
     99                     repository = config.get(server, 'repository')
    100                 else:
    101                     repository = self.DEFAULT_REPOSITORY
    102                 return {'username': config.get(server, 'username'),
    103                         'password': config.get(server, 'password'),
    104                         'repository': repository,
    105                         'server': server,
    106                         'realm': self.DEFAULT_REALM}
    107 
    108         return {}
    109 
    110     def initialize_options(self):
    111         """Initialize options."""
    112         self.repository = None
    113         self.realm = None
    114         self.show_response = 0
    115 
    116     def finalize_options(self):
    117         """Finalizes options."""
    118         if self.repository is None:
    119             self.repository = self.DEFAULT_REPOSITORY
    120         if self.realm is None:
    121             self.realm = self.DEFAULT_REALM
    122