home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) ''' TODO (thekorn20080709): * auto fetching of edge cookie is not working - no idea why an edge cookies is not created when accessing edge.launchpad.net * cookie lifetime (auto extend?) * adjust almost all testcases and connector ''' import urllib2 import multipartpost_handler import cookielib import cStringIO as StringIO import time import urlparse import cPickle import base64 from tempfile import mkstemp try: import sqlite3 as sqlite except ImportError: try: from pysqlite2 import dbapi2 as sqlite except ImportError: raise ImportError, 'no module named sqlite3 or pysqlite2.dbapi2' except: None<EXCEPTION MATCH>ImportError None<EXCEPTION MATCH>ImportError import os import exceptions import utils from lpconstants import BASEURL, HTTPCONNECTION from config import Config class _result(object): ''' represents an object returned by HTTPConnection. ''' def __init__(self, contenttype = None, text = None, url = None): if not contenttype and text and url: raise AssertionError, 'at least one argument needed' self.contenttype = contenttype self.text = text self.url = url class LPCookieProcessor(urllib2.HTTPCookieProcessor): ''' CookieProcessor with special functionality regarding launchpad.net cookies. ''' def sqlite_to_txt(cookiefile, domain): match = '%%%s%%' % domain con = sqlite.connect(cookiefile) cur = con.cursor() cur.execute('select host, path, isSecure, expiry, name, value from moz_cookies where host like ?', [ match]) ftstr = [ 'FALSE', 'TRUE'] (tmp, tmpname) = mkstemp() os.write(tmp, '# HTTP Cookie File\n') for item in cur.fetchall(): str = '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % (item[0], ftstr[item[0].startswith('.')], item[1], ftstr[item[2]], item[3], item[4], item[5]) os.write(tmp, str) os.close(tmp) return tmpname sqlite_to_txt = staticmethod(sqlite_to_txt) def __nonzero__(self): return [] in [ i.domain for i in self.cookiejar ] def load_file(self, cookiefile): if cookiefile: cj = cookielib.MozillaCookieJar() if cookiefile[-6:] == 'sqlite': cookies = self.sqlite_to_txt(cookiefile, 'launchpad.net') try: cj.load(cookies) except: os.unlink(cookies) raise IOError, 'Invalid cookie file' os.unlink(cookies) else: cj.load(cookiefile) for cookie in cj: self.cookiejar.set_cookie(cookie) return True raise IOError, 'No cookie file given' def get_cookie(self, name, domain = None): if not domain: pass domain = '.launchpad.net' for i in self.cookiejar: if i.name == name and i.domain == domain: return i return False def change_inhibit_beta_redirect(self, mode): ''' cj.clear(".launchpad.net", "/", "inhibit_beta_redirect") name inhibit_beta_redirect domain .launchpad.net path / Cookie(version=0, name=\'inhibit_beta_redirect\', value=\'1\', port=None, port_specified=False, domain=\'.launchpad.net\', domain_specified=True, domain_initial_dot=True, path=\'/\', path_specified=False, secure=True, expires=1202139678, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False) ''' if mode in (HTTPCONNECTION.MODE.DEFAULT, HTTPCONNECTION.MODE.EDGE): try: self.cookiejar.clear('.launchpad.net', '/', 'inhibit_beta_redirect') except KeyError: pass except: None<EXCEPTION MATCH>KeyError None<EXCEPTION MATCH>KeyError if mode == HTTPCONNECTION.MODE.STABLE: c_expires = int(time.time()) + 7200 c = self.get_cookie('inhibit_beta_redirect') if not c: try: c = cookielib.Cookie(version = 0, name = 'inhibit_beta_redirect', value = '1', port = None, port_specified = False, domain = '.launchpad.net', domain_specified = True, domain_initial_dot = True, path = '/', path_specified = False, secure = True, expires = c_expires, discard = False, comment = None, comment_url = None, rest = { }, rfc2109 = False) except TypeError: c = cookielib.Cookie(version = 0, name = 'inhibit_beta_redirect', value = '1', port = None, port_specified = False, domain = '.launchpad.net', domain_specified = True, domain_initial_dot = True, path = '/', path_specified = False, secure = True, expires = c_expires, discard = False, comment = None, comment_url = None, rest = { }) self.cookiejar.set_cookie(c) return self.get_cookie('inhibit_beta_redirect') def save_cookie(self, filename): cj = cookielib.MozillaCookieJar() for cookie in self.cookiejar: cj.set_cookie(cookie) cj.save(filename) def pickle_cookie_load(cookie_string): ''' load a cookie instance from the value of the config file ''' try: cookie_string = base64.decodestring(cookie_string) except Exception: pass try: c = cPickle.loads(cookie_string) if isinstance(c, cookielib.Cookie): return c return None except Exception: return None def pickle_cookie_dump(cookie_obj): ''' dump a cookie object and encode the resulting string ''' if not isinstance(cookie_obj, cookielib.Cookie): if cookie_obj is None: return '' raise TypeError isinstance(cookie_obj, cookielib.Cookie) return base64.encodestring(cPickle.dumps(cookie_obj)) class HTTPConnectionConfig(Config): ''' read the configuration of the HTTPConnection from a config file. ''' Config.MAPPING['cookies'] = { 'lp': (pickle_cookie_load, pickle_cookie_dump), 'edge': (pickle_cookie_load, pickle_cookie_dump), 'staging': (pickle_cookie_load, pickle_cookie_dump), 'inhibit_beta_redirect': (pickle_cookie_load, pickle_cookie_dump) } class HTTPConnection(object): ''' class to manage a https connection to launchpad.net. ''' def __init__(self, cookiefile = None, attempts = 5, content_types = None, config_file = None, config_ignore = None): self._HTTPConnection__config = HTTPConnectionConfig(config_file, config_ignore) self._HTTPConnection__username = None version = 'python-launchpad-bugs/%s (Python-urllib2/%s) (user: %s)' % (utils.find_version_number(show_nick = True), urllib2.__version__, self.user) self._HTTPConnection__cookiefile = cookiefile self._HTTPConnection__cookie_handler = LPCookieProcessor() self._HTTPConnection__opener = urllib2.build_opener(self._HTTPConnection__cookie_handler) self._HTTPConnection__opener.addheaders = [ ('User-agent', version)] self._HTTPConnection__poster = urllib2.build_opener(self._HTTPConnection__cookie_handler, multipartpost_handler.MultipartPostHandler) self._HTTPConnection__poster.addheaders = [ ('User-agent', version)] self._HTTPConnection__attempts = attempts if not content_types: pass self.content_types = [ 'text/html', 'text/plain'] self._HTTPConnection__progress_hook = None self._HTTPConnection__mode = HTTPCONNECTION.MODE.DEFAULT self._HTTPConnection__cookies_to_dump = [] for key, value in self._HTTPConnection__config['cookies'].iteritems(): if value is None: self._HTTPConnection__cookies_to_dump.append(key) continue self._HTTPConnection__cookie_handler.cookiejar.set_cookie(value) def get_auth(self): return self._HTTPConnection__cookie_handler def set_auth(self, auth): if isinstance(auth, str): self._HTTPConnection__cookiefile = auth try: return self._HTTPConnection__cookie_handler.load_file(self._HTTPConnection__cookiefile) except IOError: e = None raise exceptions.PythonLaunchpadBugsIOError(str(e)) except: None<EXCEPTION MATCH>IOError None<EXCEPTION MATCH>IOError if isinstance(auth, dict): try: email = auth['email'] password = auth['password'] except KeyError: raise ValueError, "The argument of .set_auth() needs to be either a path to a valid mozilla cookie-file or a dict like {'email':<lp-email-address>,'password':<password>}, but it is %s" % auth self._do_login(email, password) else: raise ValueError, "The argument of .set_auth() needs to be either a path to a valid mozilla cookie-file or a dict like {'email':<lp-email-address>,'password':<password>}, but it is %s" % auth return isinstance(auth, dict) def _do_login(self, email, password, server = None): if server is None: server = ('https://bugs.launchpad.net/+login', 'https://bugs.edge.launchpad.net/+login', 'https://bugs.staging.launchpad.net/+login') for url in server: try: r = self.post(url, { 'loginpage_email': email, 'loginpage_password': password, 'loginpage_submit_login': 'Log In' }) continue except exceptions.LaunchpadLoginError: raise exceptions.LaunchpadLoginFailed(url) continue except exceptions.LaunchpadError: e = None raise exceptions.LaunchpadLoginError(url, 'Connection failed with %s' % e) continue return None def user(self): if not self._HTTPConnection__username and self._HTTPConnection__config['user']['lplogin']: pass return 'unknown' user = property(user) def set_username(self, user): self._HTTPConnection__username = user def needs_login(self, url = None): ''' checks if authentication is needed. this considers connection-mode and cookies ''' if not url: pass url = self._change_url('https://launchpad.net/people/+me') url = self._HTTPConnection__opener.open(url).geturl() return url.endswith('+login') def get(self, url): return self._safe_urlopen(url, None, False) def post(self, url, data): return self._safe_urlopen(url, data, True) def set_mode(self, mode): if not mode in (HTTPCONNECTION.MODE.DEFAULT, HTTPCONNECTION.MODE.EDGE, HTTPCONNECTION.MODE.STABLE, HTTPCONNECTION.MODE.STAGING): raise AssertionError, 'Unknown mode' self._HTTPConnection__mode = mode c = self._HTTPConnection__cookie_handler.change_inhibit_beta_redirect(self._HTTPConnection__mode) if not c: pass self._HTTPConnection__config['cookies']['inhibit_beta_redirect'] = None if 'inhibit_beta_redirect' in self._HTTPConnection__cookies_to_dump: self._HTTPConnection__cookies_to_dump.remove('inhibit_beta_redirect') self._HTTPConnection__config.save() def _change_url(self, url): u = list(urlparse.urlsplit(url)) if self._HTTPConnection__mode == HTTPCONNECTION.MODE.EDGE: u[1] = u[1].replace('staging.', '') u[1] = u[1].replace('edge.', '') u[1] = u[1].replace('launchpad.net', 'edge.launchpad.net') elif self._HTTPConnection__mode == HTTPCONNECTION.MODE.STAGING: u[1] = u[1].replace('staging.', '') u[1] = u[1].replace('edge.', '') u[1] = u[1].replace('launchpad.net', 'staging.launchpad.net') u[1] = u[1].replace('launchpadlibrarian.net', 'staging.launchpadlibrarian.net') elif self._HTTPConnection__mode == HTTPCONNECTION.MODE.STABLE: u[1] = u[1].replace('staging.', '') u[1] = u[1].replace('edge.', '') return urlparse.urlunsplit(u) def _safe_urlopen(self, url, data, post): url = self._change_url(url) count = 0 text = None contenttype = None geturl = None sock = None if post: opener = self._HTTPConnection__poster else: opener = self._HTTPConnection__opener while count < self._HTTPConnection__attempts: try: if url[:4] != 'http': url_old = url url = 'https://' + url print 'wrong url <%s>, try <%s>' % (url_old, url) if data: sock = opener.open(url, data) else: sock = opener.open(url) contenttype = sock.info()['Content-type'] if sock.geturl().endswith('+login'): raise exceptions.LaunchpadLoginError(url) sock.geturl().endswith('+login') for ct in self.content_types: if contenttype.startswith(ct): if self._HTTPConnection__progress_hook is None: text = sock.read() geturl = sock.geturl() sock.close() else: tmp_text = StringIO.StringIO() i = 0 counter = 0 size = int(sock.info()['Content-Length']) while i < size: tmp_text.write(sock.read(self._HTTPConnection__block_size)) i += self._HTTPConnection__block_size counter += 1 self._HTTPConnection__progress_hook(counter, self._HTTPConnection__block_size, size) text = tmp_text.getvalue() geturl = sock.geturl() sock.close() tmp_text.close() x = self._HTTPConnection__cookies_to_dump[:] for i in x: c = self._HTTPConnection__cookie_handler.get_cookie(i) if c: self._HTTPConnection__config['cookies'][i] = c self._HTTPConnection__cookies_to_dump.remove(i) continue if not self._HTTPConnection__cookies_to_dump == x: self._HTTPConnection__config.save() return _result(contenttype = contenttype, text = text, url = geturl) sock.close() raise IOError, 'unsupported contenttype (contenttype=%s, url=%s)' % (contenttype, url) continue except urllib2.URLError: e = None try: error = e.code except AttributeError: error = e.reason count += 1 continue None<EXCEPTION MATCH>urllib2.URLError raise exceptions.choose_LaunchpadError(error, url) def set_progress_hook(self, hook_func, blocksize = 4096): if not blocksize: raise AssertionError, 'blocksize needs to be an integer greater than 0' self._HTTPConnection__block_size = blocksize if not callable(hook_func): raise AssertionError, 'hook_func needs to be callable with three arguments' self._HTTPConnection__progress_hook = hook_func def save_cookie(self, filename): self._HTTPConnection__cookie_handler.save_cookie(filename) if __name__ == '__main__': c = HTTPConnection() print repr(c.user), c.user def example_hook(counter, block_size, size): print (counter, block_size, size) c.set_progress_hook(example_hook) x = c.get('https://bugs.edge.launchpad.net/ubuntu/+source/linux/+bug/200500/') print len(x.text)