distribute / setuptools / command /

The branch 'default' does not exist.

Implements the Distutils 'upload' subcommand (upload package to PyPI)."""

from distutils.errors import *
from distutils.core import Command
from distutils.spawn import spawn
from distutils import log
    from hashlib import md5
except ImportError:
    from md5 import md5
import os
import sys
import socket
import platform
import ConfigParser
import httplib
import base64
import urlparse
import cStringIO as StringIO

class _upload(Command):

    description = "upload binary package to PyPI"


    user_options = [
        ('repository=', 'r',
         "url of repository [default: %s]" % DEFAULT_REPOSITORY),
        ('show-response', None,
         'display full response text from server'),
        ('sign', 's',
         'sign files to upload using gpg'),
        ('identity=', 'i', 'GPG identity used to sign files'),
    boolean_options = ['show-response', 'sign']

    def initialize_options(self):
        self.username = ''
        self.password = ''
        self.repository = ''
        self.show_response = 0
        self.sign = False
        self.identity = None

    def finalize_options(self):
        if self.identity and not self.sign:
            raise DistutilsOptionError(
                "Must use --sign for --identity to have meaning"
        if os.environ.has_key('HOME'):
            rc = os.path.join(os.environ['HOME'], '.pypirc')
            if os.path.exists(rc):
                self.announce('Using PyPI login from %s' % rc)
                config = ConfigParser.ConfigParser({
                if not self.repository:
                    self.repository = config.get('server-login', 'repository')
                if not self.username:
                    self.username = config.get('server-login', 'username')
                if not self.password:
                    self.password = config.get('server-login', 'password')
        if not self.repository:
            raise ValueError('%s is missing a repository value in .pypirc' % self._section_name)

    def run(self):
        if not self.distribution.dist_files:
            raise DistutilsOptionError("No dist file created in earlier command")
        for command, pyversion, filename in self.distribution.dist_files:
            self.upload_file(command, pyversion, filename)

    def upload_file(self, command, pyversion, filename):
        # Sign if requested
        if self.sign:
            gpg_args = ["gpg", "--detach-sign", "-a", filename]
            if self.identity:
                gpg_args[2:2] = ["--local-user", self.identity]

        # Fill in the data
        f = open(filename,'rb')
        content =
        basename = os.path.basename(filename)
        comment = ''
        if command=='bdist_egg' and self.distribution.has_ext_modules():
            comment = "built on %s" % platform.platform(terse=1)
        data = {
        if command == 'bdist_rpm':
            dist, version, id = platform.dist()
            if dist:
                comment = 'built for %s %s' % (dist, version)
        elif command == 'bdist_dumb':
            comment = 'built for %s' % platform.platform(terse=1)
        data['comment'] = comment

        if self.sign:
            data['gpg_signature'] = (os.path.basename(filename) + ".asc",

        # set up the authentication
        auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()

        # Build up the MIME payload for the POST data
        boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
        sep_boundary = '\n--' + boundary
        end_boundary = sep_boundary + '--'
        body = StringIO.StringIO()
        for key, value in data.items():
            # handle multiple entries for the same name
            if type(value) != type([]):
                value = [value]
            for value in value:
                if type(value) is tuple:
                    fn = ';filename="%s"' % value[0]
                    value = value[1]
                    fn = ""
                value = str(value)
                body.write('\nContent-Disposition: form-data; name="%s"'%key)
                if value and value[-1] == '\r':
                    body.write('\n')  # write an extra newline (lurve Macs)
        body = body.getvalue()

        self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)

        # build the Request
        # We can't use urllib2 since we need to send the Basic
        # auth right with the first request
        schema, netloc, url, params, query, fragments = \
        assert not params and not query and not fragments
        if schema == 'http':
            http = httplib.HTTPConnection(netloc)
        elif schema == 'https':
            http = httplib.HTTPSConnection(netloc)
            raise AssertionError, "unsupported schema "+schema

        data = ''
        loglevel = log.INFO
            http.putrequest("POST", url)
                           'multipart/form-data; boundary=%s'%boundary)
            http.putheader('Content-length', str(len(body)))
            http.putheader('Authorization', auth)
        except socket.error, e:
            self.announce(str(e), log.ERROR)

        r = http.getresponse()
        if r.status == 200:
            self.announce('Server response (%s): %s' % (r.status, r.reason),
            self.announce('Upload failed (%s): %s' % (r.status, r.reason),
        if self.show_response:
            print '-'*75,, '-'*75

if sys.version >= "2.5":
    from distutils.command.upload import upload as distutils_upload
    class upload(distutils_upload):

        def run(self):
            self._section_name = self.repository

        def _read_pypirc(self):
            """Reads the .pypirc file."""
            self._section_name = self.repository
            rc = self._get_rc_file()
            if os.path.exists(rc):
                repository = self.repository
                config = ConfigParser.ConfigParser()
                sections = config.sections()
                if 'distutils' in sections:
                    # let's get the list of servers
                    index_servers = config.get('distutils', 'index-servers')
                    _servers = [server.strip() for server in
                                if server.strip() != '']
                    if _servers == []:
                        # nothing set, let's try to get the default pypi
                        if 'pypi' in sections:
                            _servers = ['pypi']
                            # the file is not properly defined, returning
                            # an empty dict
                            return {}
                    for server in _servers:
                        current = {'server': server}
                        current['username'] = config.get(server, 'username')

                        # optional params
                        for key, default in (('repository',
                                             ('realm', self.DEFAULT_REALM),
                                             ('password', None)):
                            if config.has_option(server, key):
                                current[key] = config.get(server, key)
                                current[key] = default
                        # Issue #246, handling url ambiguity
                        if (current['server'] == repository or
                            current['repository'] == repository or
                            (current['repository'] == "" and repository == self.DEFAULT_REPOSITORY)):
                            return current
                elif 'server-login' in sections:
                    # old format
                    server = 'server-login'
                    if config.has_option(server, 'repository'):
                        repository = config.get(server, 'repository')
                        repository = None
                    return {'username': config.get(server, 'username'),
                            'password': config.get(server, 'password'),
                            'repository': repository,
                            'server': server,
                            'realm': self.DEFAULT_REALM}

            return {}

        def finalize_options(self):
            if not self.repository:
                raise ValueError('%s is missing a repository value in .pypirc' % self._section_name)

    upload = _upload
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.