ubuntu-bugcontrol team mailing list archive
-
ubuntu-bugcontrol team
-
Mailing list archive
-
Message #04559
[Merge] ~techalchemy/ubuntu-qa-tools:feature/in-memory-cookiedb into ubuntu-qa-tools:master
Dan Ryan has proposed merging ~techalchemy/ubuntu-qa-tools:feature/in-memory-cookiedb into ubuntu-qa-tools:master.
Commit message:
Use in-memory sqlite database for cookie storage and cookiejar generation
- Also addresses an issue converting launchpadlib.__version__ to and from integers due to byte/unicode encoding changes which prevented importing on python 3
Requested reviews:
Mike Salvatore (mikesalvatore)
For more details, see:
https://code.launchpad.net/~techalchemy/ubuntu-qa-tools/+git/ubuntu-qa-tools/+merge/378348
--
Your team Ubuntu Bug Control is subscribed to branch ubuntu-qa-tools:master.
diff --git a/common/lpl_common.py b/common/lpl_common.py
index de9281f..6b57854 100644
--- a/common/lpl_common.py
+++ b/common/lpl_common.py
@@ -7,7 +7,7 @@
from __future__ import print_function
-import os, sys, tempfile, time, shutil, launchpadlib
+import os, sys, tempfile, time, shutil, launchpadlib, io
from launchpadlib.launchpad import Launchpad
from launchpadlib.credentials import Credentials
import launchpadlib.errors
@@ -20,23 +20,26 @@ except ImportError:
# as of 16.04, launchpadlib supports python3
# so make code support both python2 and python3
if sys.version_info > (3, 0):
+ string_types = (str,)
from http.cookiejar import LoadError, MozillaCookieJar
+ from io import StringIO
from urllib.request import HTTPCookieProcessor, HTTPError, build_opener
from urllib.parse import unquote
else:
+ string_types = (basestring,)
from cookielib import LoadError, MozillaCookieJar
+ from cStringIO import StringIO
from urllib2 import HTTPCookieProcessor, HTTPError, build_opener
from urllib import unquote
version_min = [1,5,7]
-if sys.version_info > (3, 0):
- if [int(x) for x in launchpadlib.__version__.split('.')] < version_min:
- raise ValueError("Requires launchpadlib version %s or later (%s in use)" \
- % (".".join(version_min), launchpadlib.__version__))
-else:
- if [int(x) for x in launchpadlib.__version__.decode().split('.')] < version_min:
- raise ValueError("Requires launchpadlib version %s or later (%s in use)" \
- % (".".join(version_min), launchpadlib.__version__))
+lp_version = launchpadlib.__version__
+if not isinstance(lp_version, string_types):
+ lp_version = lp_version.decode()
+
+if version_min > list(map(int, lp_version.split('.'))):
+ raise ValueError("Requires launchpadlib version %s or later (%s in use)" \
+ % (".".join(map(str, version_min)), lp_version))
def connect(use_edge=False, beta=False, version=None, uri=None, bot=None):
@@ -136,36 +139,47 @@ def opener_with_cookie(cookie_file):
old_umask = os.umask(0o077)
# Work around Firefox 3.5's dumb sqlite locking problems by copying cookies out:
- tmp = None
+ opener = None
+ cj = MozillaCookieJar()
if cookie_file.endswith('.sqlite'):
- (cookie_path, cookie_name) = os.path.split(cookie_file)
- with tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.sqlite') as sql_handle:
- sql = sql_handle.name
- shutil.copyfile(cookie_file, sql)
- match = '%launchpad.net'
- con = sqlite.connect(sql)
- cur = con.cursor()
- cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies where host like ?", [match])
- ftstr = ["FALSE","TRUE"]
- tmp = tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.mozilla', mode='w+')
- cookie_file = tmp.name
- tmp.write("# 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])
- tmp.write(str)
- sql = None
- tmp.flush()
+ sql_script = StringIO()
+ with sqlite.connect(cookie_file) as cookie_db:
+ for line in cookie_db.iterdump():
+ sql_script.write("{0}\n".format(line))
+ sql_script.seek(0)
- cj = MozillaCookieJar()
- try:
- cj.load(cookie_file)
- except LoadError as e:
- print("Failed to load cookie from file (%s): %s - continuing anyway..." % (cookie_file, e.strerror))
+ match = '%launchpad.net'
+ query = "select host, path, isSecure, expiry, name, value from moz_cookies where host like ?"
+ with tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.mozilla', mode='w+') as cookie_jar:
+ with sqlite.connect(":memory:") as con:
+ cur = con.cursor()
+ try:
+ cur.executescript(sql_script.read())
+ cur.commit()
+ except sqlite.Error:
+ print("Failed loading cookied DB...")
+ raise
+
+ try:
+ cur.execute(query, [match])
+ except sqlite.Error:
+ print("Failed reading database...")
+ raise
+ ftstr = ["FALSE","TRUE"]
+ cookie_jar.write("# 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])
+ cookie_jar.write(str_)
+
+ cookie_jar.flush()
+ try:
+ cj.load(cookie_jar.name)
+ except LoadError as e:
+ print("Failed to load cookie from file ({0}): {1!s} - continuing anyway...".format(cookie_jar.name, e))
opener = build_opener(HTTPCookieProcessor(cj))
- tmp = None
os.umask(old_umask)
return opener
Follow ups