Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/distutils/command/upload.py

    r2 r391  
    22
    33Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
     4import os
     5import socket
     6import platform
     7from urllib2 import urlopen, Request, HTTPError
     8from base64 import standard_b64encode
     9import urlparse
     10import cStringIO as StringIO
     11from hashlib import md5
    412
    5 from distutils.errors import *
     13from distutils.errors import DistutilsOptionError
    614from distutils.core import PyPIRCCommand
    715from distutils.spawn import spawn
    816from distutils import log
    9 from hashlib import md5
    10 import os
    11 import socket
    12 import platform
    13 import httplib
    14 from base64 import standard_b64encode
    15 import urlparse
    16 import cStringIO as StringIO
    17 from ConfigParser import ConfigParser
    18 
    1917
    2018class upload(PyPIRCCommand):
     
    5149            self.realm = config['realm']
    5250
     51        # getting the password from the distribution
     52        # if previously set by the register command
     53        if not self.password and self.distribution.password:
     54            self.password = self.distribution.password
     55
    5356    def run(self):
    5457        if not self.distribution.dist_files:
     
    5861
    5962    def upload_file(self, command, pyversion, filename):
     63        # Makes sure the repository URL is compliant
     64        schema, netloc, url, params, query, fragments = \
     65            urlparse.urlparse(self.repository)
     66        if params or query or fragments:
     67            raise AssertionError("Incompatible url %s" % self.repository)
     68
     69        if schema not in ('http', 'https'):
     70            raise AssertionError("unsupported schema " + schema)
     71
    6072        # Sign if requested
    6173        if self.sign:
     
    6880        # Fill in the data - send all the meta-data in case we need to
    6981        # register a new release
    70         content = open(filename,'rb').read()
     82        f = open(filename,'rb')
     83        try:
     84            content = f.read()
     85        finally:
     86            f.close()
    7187        meta = self.distribution.metadata
    7288        data = {
     
    126142        for key, value in data.items():
    127143            # handle multiple entries for the same name
    128             if type(value) != type([]):
     144            if not isinstance(value, list):
    129145                value = [value]
    130146            for value in value:
    131                 if type(value) is tuple:
     147                if isinstance(value, tuple):
    132148                    fn = ';filename="%s"' % value[0]
    133149                    value = value[1]
     
    149165
    150166        # build the Request
    151         # We can't use urllib2 since we need to send the Basic
    152         # auth right with the first request
    153         schema, netloc, url, params, query, fragments = \
    154             urlparse.urlparse(self.repository)
    155         assert not params and not query and not fragments
    156         if schema == 'http':
    157             http = httplib.HTTPConnection(netloc)
    158         elif schema == 'https':
    159             http = httplib.HTTPSConnection(netloc)
    160         else:
    161             raise AssertionError, "unsupported schema "+schema
     167        headers = {'Content-type':
     168                        'multipart/form-data; boundary=%s' % boundary,
     169                   'Content-length': str(len(body)),
     170                   'Authorization': auth}
    162171
    163         data = ''
    164         loglevel = log.INFO
     172        request = Request(self.repository, data=body,
     173                          headers=headers)
     174        # send the data
    165175        try:
    166             http.connect()
    167             http.putrequest("POST", url)
    168             http.putheader('Content-type',
    169                            'multipart/form-data; boundary=%s'%boundary)
    170             http.putheader('Content-length', str(len(body)))
    171             http.putheader('Authorization', auth)
    172             http.endheaders()
    173             http.send(body)
     176            result = urlopen(request)
     177            status = result.getcode()
     178            reason = result.msg
     179            if self.show_response:
     180                msg = '\n'.join(('-' * 75, r.read(), '-' * 75))
     181                self.announce(msg, log.INFO)
    174182        except socket.error, e:
    175183            self.announce(str(e), log.ERROR)
    176184            return
     185        except HTTPError, e:
     186            status = e.code
     187            reason = e.msg
    177188
    178         r = http.getresponse()
    179         if r.status == 200:
    180             self.announce('Server response (%s): %s' % (r.status, r.reason),
     189        if status == 200:
     190            self.announce('Server response (%s): %s' % (status, reason),
    181191                          log.INFO)
    182192        else:
    183             self.announce('Upload failed (%s): %s' % (r.status, r.reason),
     193            self.announce('Upload failed (%s): %s' % (status, reason),
    184194                          log.ERROR)
    185         if self.show_response:
    186             print '-'*75, r.read(), '-'*75
Note: See TracChangeset for help on using the changeset viewer.