home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / launchpadbugs / storeblob.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.2 KB  |  87 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Temporarily store a blob in Launchpad and get a ticket for it.
  5.  
  6. Copyright (C) 2007 Canonical Ltd.
  7. Author: Martin Pitt <martin.pitt@ubuntu.com>
  8.  
  9. This program is free software; you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the
  11. Free Software Foundation; either version 2 of the License, or (at your
  12. option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
  13. the full text of the license.
  14. '''
  15. import multipartpost_handler
  16. import urllib2
  17. import time
  18. import httplib
  19. import socket
  20. _https_upload_callback = None
  21.  
  22. class HTTPSProgressConnection(httplib.HTTPSConnection):
  23.     '''Implement a HTTPSConnection with an optional callback function for
  24.     upload progress.'''
  25.     
  26.     def send(self, data):
  27.         if not _https_upload_callback:
  28.             httplib.HTTPSConnection.send(self, data)
  29.             return None
  30.         sent = 0
  31.         total = len(data)
  32.         chunksize = 1024
  33.         while sent < total:
  34.             _https_upload_callback(sent, total)
  35.             t1 = time.time()
  36.             httplib.HTTPSConnection.send(self, data[sent:sent + chunksize])
  37.             sent += chunksize
  38.             t2 = time.time()
  39.             if t2 - t1 < 0.5:
  40.                 chunksize *= 2
  41.                 continue
  42.             _https_upload_callback
  43.             if t2 - t1 > 2:
  44.                 chunksize /= 2
  45.                 continue
  46.  
  47.  
  48.  
  49. class HTTPSProgressHandler(urllib2.HTTPSHandler):
  50.     
  51.     def https_open(self, req):
  52.         return self.do_open(HTTPSProgressConnection, req)
  53.  
  54.  
  55.  
  56. def upload(blob, progress_callback = None, staging = False):
  57.     '''Upload given blob (file-like object) to Malone and return the ticket for
  58.     it.
  59.  
  60.     progress_callback can be set to a function(sent, total) which is regularly
  61.     called with the number of bytes already sent and total number of bytes to
  62.     send. It is called every 0.5 to 2 seconds (dynamically adapted to upload
  63.     bandwidth).
  64.  
  65.     Return None on error.
  66.  
  67.     By default this uses the production Launchpad instance. Set staging=True to
  68.     use staging.launchpad.net (for testing).
  69.     '''
  70.     global _https_upload_callback
  71.     old_timeout = socket.getdefaulttimeout()
  72.     socket.setdefaulttimeout(30)
  73.     ticket = None
  74.     _https_upload_callback = progress_callback
  75.     opener = urllib2.build_opener(HTTPSProgressHandler, multipartpost_handler.MultipartPostHandler)
  76.     if staging:
  77.         url = 'https://staging.launchpad.net/+storeblob'
  78.     else:
  79.         url = 'https://launchpad.net/+storeblob'
  80.     result = opener.open(url, {
  81.         'FORM_SUBMIT': '1',
  82.         'field.blob': blob })
  83.     ticket = result.info().get('X-Launchpad-Blob-Token')
  84.     socket.setdefaulttimeout(old_timeout)
  85.     return ticket
  86.  
  87.