home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / upload.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.1 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.upload
  5.  
  6. Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
  7. from distutils.errors import *
  8. from distutils.core import PyPIRCCommand
  9. from distutils.spawn import spawn
  10. from distutils import log
  11. from hashlib import md5
  12. import os
  13. import socket
  14. import platform
  15. import httplib
  16. import base64
  17. import urlparse
  18. import cStringIO as StringIO
  19. from ConfigParser import ConfigParser
  20.  
  21. class upload(PyPIRCCommand):
  22.     description = 'upload binary package to PyPI'
  23.     user_options = PyPIRCCommand.user_options + [
  24.         ('sign', 's', 'sign files to upload using gpg'),
  25.         ('identity=', 'i', 'GPG identity used to sign files')]
  26.     boolean_options = PyPIRCCommand.boolean_options + [
  27.         'sign']
  28.     
  29.     def initialize_options(self):
  30.         PyPIRCCommand.initialize_options(self)
  31.         self.username = ''
  32.         self.password = ''
  33.         self.show_response = 0
  34.         self.sign = False
  35.         self.identity = None
  36.  
  37.     
  38.     def finalize_options(self):
  39.         PyPIRCCommand.finalize_options(self)
  40.         if self.identity and not (self.sign):
  41.             raise DistutilsOptionError('Must use --sign for --identity to have meaning')
  42.         not (self.sign)
  43.         config = self._read_pypirc()
  44.         if config != { }:
  45.             self.username = config['username']
  46.             self.password = config['password']
  47.             self.repository = config['repository']
  48.             self.realm = config['realm']
  49.         
  50.  
  51.     
  52.     def run(self):
  53.         if not self.distribution.dist_files:
  54.             raise DistutilsOptionError('No dist file created in earlier command')
  55.         self.distribution.dist_files
  56.         for command, pyversion, filename in self.distribution.dist_files:
  57.             self.upload_file(command, pyversion, filename)
  58.         
  59.  
  60.     
  61.     def upload_file(self, command, pyversion, filename):
  62.         if self.sign:
  63.             gpg_args = [
  64.                 'gpg',
  65.                 '--detach-sign',
  66.                 '-a',
  67.                 filename]
  68.             if self.identity:
  69.                 gpg_args[2:2] = [
  70.                     '--local-user',
  71.                     self.identity]
  72.             
  73.             spawn(gpg_args, dry_run = self.dry_run)
  74.         
  75.         content = open(filename, 'rb').read()
  76.         meta = self.distribution.metadata
  77.         data = {
  78.             ':action': 'file_upload',
  79.             'protcol_version': '1',
  80.             'name': meta.get_name(),
  81.             'version': meta.get_version(),
  82.             'content': (os.path.basename(filename), content),
  83.             'filetype': command,
  84.             'pyversion': pyversion,
  85.             'md5_digest': md5(content).hexdigest(),
  86.             'metadata_version': '1.0',
  87.             'summary': meta.get_description(),
  88.             'home_page': meta.get_url(),
  89.             'author': meta.get_contact(),
  90.             'author_email': meta.get_contact_email(),
  91.             'license': meta.get_licence(),
  92.             'description': meta.get_long_description(),
  93.             'keywords': meta.get_keywords(),
  94.             'platform': meta.get_platforms(),
  95.             'classifiers': meta.get_classifiers(),
  96.             'download_url': meta.get_download_url(),
  97.             'provides': meta.get_provides(),
  98.             'requires': meta.get_requires(),
  99.             'obsoletes': meta.get_obsoletes() }
  100.         comment = ''
  101.         if command == 'bdist_rpm':
  102.             (dist, version, id) = platform.dist()
  103.             if dist:
  104.                 comment = 'built for %s %s' % (dist, version)
  105.             
  106.         elif command == 'bdist_dumb':
  107.             comment = 'built for %s' % platform.platform(terse = 1)
  108.         
  109.         data['comment'] = comment
  110.         if self.sign:
  111.             data['gpg_signature'] = (os.path.basename(filename) + '.asc', open(filename + '.asc').read())
  112.         
  113.         auth = 'Basic ' + base64.encodestring(self.username + ':' + self.password).strip()
  114.         boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
  115.         sep_boundary = '\n--' + boundary
  116.         end_boundary = sep_boundary + '--'
  117.         body = StringIO.StringIO()
  118.         for key, value in data.items():
  119.             if type(value) != type([]):
  120.                 value = [
  121.                     value]
  122.             
  123.             for value in value:
  124.                 if type(value) is tuple:
  125.                     fn = ';filename="%s"' % value[0]
  126.                     value = value[1]
  127.                 else:
  128.                     fn = ''
  129.                 value = str(value)
  130.                 body.write(sep_boundary)
  131.                 body.write('\nContent-Disposition: form-data; name="%s"' % key)
  132.                 body.write(fn)
  133.                 body.write('\n\n')
  134.                 body.write(value)
  135.                 if value and value[-1] == '\r':
  136.                     body.write('\n')
  137.                     continue
  138.             
  139.         
  140.         body.write(end_boundary)
  141.         body.write('\n')
  142.         body = body.getvalue()
  143.         self.announce('Submitting %s to %s' % (filename, self.repository), log.INFO)
  144.         (schema, netloc, url, params, query, fragments) = urlparse.urlparse(self.repository)
  145.         if not not params and not query or not fragments:
  146.             raise AssertionError
  147.         if schema == 'http':
  148.             http = httplib.HTTPConnection(netloc)
  149.         elif schema == 'https':
  150.             http = httplib.HTTPSConnection(netloc)
  151.         else:
  152.             raise AssertionError, 'unsupported schema ' + schema
  153.         data = not fragments
  154.         loglevel = log.INFO
  155.         
  156.         try:
  157.             http.connect()
  158.             http.putrequest('POST', url)
  159.             http.putheader('Content-type', 'multipart/form-data; boundary=%s' % boundary)
  160.             http.putheader('Content-length', str(len(body)))
  161.             http.putheader('Authorization', auth)
  162.             http.endheaders()
  163.             http.send(body)
  164.         except socket.error:
  165.             e = None
  166.             self.announce(str(e), log.ERROR)
  167.             return None
  168.  
  169.         r = http.getresponse()
  170.         if r.status == 200:
  171.             self.announce('Server response (%s): %s' % (r.status, r.reason), log.INFO)
  172.         else:
  173.             self.announce('Upload failed (%s): %s' % (r.status, r.reason), log.ERROR)
  174.         if self.show_response:
  175.             print '-' * 75, r.read(), '-' * 75
  176.         
  177.  
  178.  
  179.