Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 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 | import sys |
| 8 | from configparser import ConfigParser |
| 9 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 10 | from distutils.cmd import Command |
Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 11 | |
| 12 | DEFAULT_PYPIRC = """\ |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 13 | [distutils] |
| 14 | index-servers = |
Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 15 | pypi |
| 16 | |
| 17 | [pypi] |
| 18 | username:%s |
| 19 | password:%s |
| 20 | """ |
| 21 | |
| 22 | class PyPIRCCommand(Command): |
| 23 | """Base command that knows how to handle the .pypirc file |
| 24 | """ |
| 25 | DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi' |
| 26 | DEFAULT_REALM = 'pypi' |
| 27 | repository = None |
| 28 | realm = None |
| 29 | |
| 30 | user_options = [ |
| 31 | ('repository=', 'r', |
| 32 | "url of repository [default: %s]" % \ |
| 33 | DEFAULT_REPOSITORY), |
| 34 | ('show-response', None, |
| 35 | 'display full response text from server')] |
| 36 | |
| 37 | boolean_options = ['show-response'] |
| 38 | |
| 39 | def _get_rc_file(self): |
| 40 | """Returns rc file path.""" |
| 41 | return os.path.join(os.path.expanduser('~'), '.pypirc') |
| 42 | |
| 43 | def _store_pypirc(self, username, password): |
| 44 | """Creates a default .pypirc file.""" |
| 45 | rc = self._get_rc_file() |
| 46 | f = open(rc, 'w') |
| 47 | try: |
| 48 | f.write(DEFAULT_PYPIRC % (username, password)) |
| 49 | finally: |
| 50 | f.close() |
| 51 | try: |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 52 | os.chmod(rc, 0o600) |
Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 53 | except OSError: |
| 54 | # should do something better here |
| 55 | pass |
| 56 | |
| 57 | def _read_pypirc(self): |
| 58 | """Reads the .pypirc file.""" |
| 59 | rc = self._get_rc_file() |
| 60 | if os.path.exists(rc): |
Georg Brandl | 1b466f2 | 2008-05-26 17:01:57 +0000 | [diff] [blame] | 61 | self.announce('Using PyPI login from %s' % rc) |
Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 62 | repository = self.repository or self.DEFAULT_REPOSITORY |
| 63 | realm = self.realm or self.DEFAULT_REALM |
| 64 | |
| 65 | config = ConfigParser() |
| 66 | config.read(rc) |
| 67 | sections = config.sections() |
| 68 | if 'distutils' in sections: |
| 69 | # let's get the list of servers |
| 70 | index_servers = config.get('distutils', 'index-servers') |
| 71 | _servers = [server.strip() for server in |
| 72 | index_servers.split('\n') |
| 73 | if server.strip() != ''] |
| 74 | if _servers == []: |
| 75 | # nothing set, let's try to get the default pypi |
| 76 | if 'pypi' in sections: |
| 77 | _servers = ['pypi'] |
| 78 | else: |
| 79 | # the file is not properly defined, returning |
| 80 | # an empty dict |
| 81 | return {} |
| 82 | for server in _servers: |
| 83 | current = {'server': server} |
| 84 | current['username'] = config.get(server, 'username') |
Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 85 | |
| 86 | # optional params |
| 87 | for key, default in (('repository', |
| 88 | self.DEFAULT_REPOSITORY), |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 89 | ('realm', self.DEFAULT_REALM), |
| 90 | ('password', None)): |
Alexandre Vassalotti | fc02aef | 2008-05-15 02:14:05 +0000 | [diff] [blame] | 91 | if config.has_option(server, key): |
| 92 | current[key] = config.get(server, key) |
| 93 | else: |
| 94 | current[key] = default |
| 95 | if (current['server'] == repository or |
| 96 | current['repository'] == repository): |
| 97 | return current |
| 98 | elif 'server-login' in sections: |
| 99 | # old format |
| 100 | server = 'server-login' |
| 101 | if config.has_option(server, 'repository'): |
| 102 | repository = config.get(server, 'repository') |
| 103 | else: |
| 104 | repository = self.DEFAULT_REPOSITORY |
| 105 | return {'username': config.get(server, 'username'), |
| 106 | 'password': config.get(server, 'password'), |
| 107 | 'repository': repository, |
| 108 | 'server': server, |
| 109 | 'realm': self.DEFAULT_REALM} |
| 110 | |
| 111 | return {} |
| 112 | |
| 113 | def initialize_options(self): |
| 114 | """Initialize options.""" |
| 115 | self.repository = None |
| 116 | self.realm = None |
| 117 | self.show_response = 0 |
| 118 | |
| 119 | def finalize_options(self): |
| 120 | """Finalizes options.""" |
| 121 | if self.repository is None: |
| 122 | self.repository = self.DEFAULT_REPOSITORY |
| 123 | if self.realm is None: |
| 124 | self.realm = self.DEFAULT_REALM |