David Fernandez Gonzalez has proposed merging ~litios/ubuntu-qa-tools:mozilla-cookies-issue into ubuntu-qa-tools:master.
Requested reviews: Emilia Torino (emitorino) Eduardo Barretto (ebarretto) For more details, see: https://code.launchpad.net/~litios/ubuntu-qa-tools/+git/ubuntu-qa-tools/+merge/432306 New customer-ppa-related scripts rely on the opener_with_cookie capability to get the changelog of the packages. I added a check to ensure that the LP cookies work and tried to implement an in-memory files approach as this was a TODO. Let me know of any improvements! -- 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 744e588..2d35bf0 100644 --- a/common/lpl_common.py +++ b/common/lpl_common.py @@ -11,6 +11,8 @@ import os, sys, tempfile, time, shutil, launchpadlib from launchpadlib.launchpad import Launchpad from launchpadlib.credentials import Credentials import launchpadlib.errors +import io +import webbrowser try: import progressbar @@ -130,44 +132,61 @@ def get_archive(name, lp, verbose=False, distribution=None): def opener_with_cookie(cookie_file): - # TODO: For a more secure approach, consider handling files in memory - # rather than creating temporary files. import sqlite3 as sqlite old_umask = os.umask(0o077) - - # Work around Firefox 3.5's dumb sqlite locking problems by copying cookies out: - tmp = None 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) + try: + src = sqlite.connect(cookie_file) + db_dump = io.StringIO() + for line in src.iterdump(): + db_dump.write('%s\n' % line) + src.close() + con = sqlite.connect(':memory:') + con.cursor().executescript(db_dump.getvalue()) + db_dump.close() + except sqlite.OperationalError: + # Work around Firefox 3.5's dumb sqlite locking problems by copying cookies out + # We cannot make this an in-memory file as sqlite3 has no capabilities to load them. + with tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.sqlite') as sql_handle: + sql = sql_handle.name + shutil.copyfile(cookie_file, sql) + con = sqlite.connect(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") + cookie_file_dump = io.StringIO() + + cookie_file_dump.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) + cookie_file_dump.write(str) sql = None - tmp.flush() + cookie_file_dump.flush() + cookie_file_dump.seek(0) + con.close() cj = MozillaCookieJar() try: - cj.load(cookie_file) + cj._really_load(cookie_file_dump, '', False, False) except LoadError as e: print("Failed to load cookie from file (%s): %s - continuing anyway..." % (cookie_file, e.strerror)) opener = build_opener(HTTPCookieProcessor(cj)) - tmp = None + os.umask(old_umask) + cookie_file_dump.close() + # Ensure that the lp token (if any) is a valid token + response = open_url(opener, 'https://launchpad.net') + if 'Log in' in response.read().decode(): + print('User is not logged in. Please log in...') + webbrowser.get('firefox').open('https://launchpad.net/ubuntu/+login') + input('Press any key after logging in. (Firefox must be closed in order to grab the updated cookies)') + return opener_with_cookie(cookie_file) + return opener def open_url(opener, url):
_______________________________________________ Mailing list: https://launchpad.net/~ubuntu-bugcontrol Post to : ubuntu-bugcontrol@lists.launchpad.net Unsubscribe : https://launchpad.net/~ubuntu-bugcontrol More help : https://help.launchpad.net/ListHelp