home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2422 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.1 KB  |  187 lines

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