Merge ~litios/ubuntu-qa-tools:mozilla-cookies-issue into ubuntu-qa-tools:master

Proposed by David Fernandez Gonzalez
Status: Merged
Merge reported by: David Fernandez Gonzalez
Merged at revision: 3721e82fb7ce5a8a24dce3c9bde12db3d4a0131e
Proposed branch: ~litios/ubuntu-qa-tools:mozilla-cookies-issue
Merge into: ubuntu-qa-tools:master
Diff against target: 92 lines (+36/-17)
1 file modified
common/lpl_common.py (+36/-17)
Reviewer Review Type Date Requested Status
Alex Murray Approve
Emilia Torino Pending
Eduardo Barretto Pending
Review via email: mp+432306@code.launchpad.net

Description of the change

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!

To post a comment you must log in.
Revision history for this message
Alex Murray (alexmurray) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/common/lpl_common.py b/common/lpl_common.py
2index 744e588..2d35bf0 100644
3--- a/common/lpl_common.py
4+++ b/common/lpl_common.py
5@@ -11,6 +11,8 @@ import os, sys, tempfile, time, shutil, launchpadlib
6 from launchpadlib.launchpad import Launchpad
7 from launchpadlib.credentials import Credentials
8 import launchpadlib.errors
9+import io
10+import webbrowser
11
12 try:
13 import progressbar
14@@ -130,44 +132,61 @@ def get_archive(name, lp, verbose=False, distribution=None):
15
16
17 def opener_with_cookie(cookie_file):
18- # TODO: For a more secure approach, consider handling files in memory
19- # rather than creating temporary files.
20 import sqlite3 as sqlite
21
22 old_umask = os.umask(0o077)
23-
24- # Work around Firefox 3.5's dumb sqlite locking problems by copying cookies out:
25- tmp = None
26 if cookie_file.endswith('.sqlite'):
27- (cookie_path, cookie_name) = os.path.split(cookie_file)
28- with tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.sqlite') as sql_handle:
29- sql = sql_handle.name
30- shutil.copyfile(cookie_file, sql)
31+ try:
32+ src = sqlite.connect(cookie_file)
33+ db_dump = io.StringIO()
34+ for line in src.iterdump():
35+ db_dump.write('%s\n' % line)
36+ src.close()
37+ con = sqlite.connect(':memory:')
38+ con.cursor().executescript(db_dump.getvalue())
39+ db_dump.close()
40+ except sqlite.OperationalError:
41+ # Work around Firefox 3.5's dumb sqlite locking problems by copying cookies out
42+ # We cannot make this an in-memory file as sqlite3 has no capabilities to load them.
43+ with tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.sqlite') as sql_handle:
44+ sql = sql_handle.name
45+ shutil.copyfile(cookie_file, sql)
46+ con = sqlite.connect(sql)
47
48 match = '%launchpad.net'
49- con = sqlite.connect(sql)
50 cur = con.cursor()
51 cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies where host like ?", [match])
52 ftstr = ["FALSE","TRUE"]
53- tmp = tempfile.NamedTemporaryFile(prefix='cookies-XXXXXX', suffix='.mozilla', mode='w+')
54- cookie_file = tmp.name
55- tmp.write("# HTTP Cookie File\n")
56+ cookie_file_dump = io.StringIO()
57+
58+ cookie_file_dump.write("# HTTP Cookie File\n")
59 for item in cur.fetchall():
60 str = "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( item[0], \
61 ftstr[item[0].startswith('.')], item[1], \
62 ftstr[item[2]], item[3], item[4], item[5])
63- tmp.write(str)
64+ cookie_file_dump.write(str)
65 sql = None
66- tmp.flush()
67+ cookie_file_dump.flush()
68+ cookie_file_dump.seek(0)
69+ con.close()
70
71 cj = MozillaCookieJar()
72 try:
73- cj.load(cookie_file)
74+ cj._really_load(cookie_file_dump, '', False, False)
75 except LoadError as e:
76 print("Failed to load cookie from file (%s): %s - continuing anyway..." % (cookie_file, e.strerror))
77 opener = build_opener(HTTPCookieProcessor(cj))
78- tmp = None
79+
80 os.umask(old_umask)
81+ cookie_file_dump.close()
82+ # Ensure that the lp token (if any) is a valid token
83+ response = open_url(opener, 'https://launchpad.net')
84+ if 'Log in' in response.read().decode():
85+ print('User is not logged in. Please log in...')
86+ webbrowser.get('firefox').open('https://launchpad.net/ubuntu/+login')
87+ input('Press any key after logging in. (Firefox must be closed in order to grab the updated cookies)')
88+ return opener_with_cookie(cookie_file)
89+
90 return opener
91
92 def open_url(opener, url):

Subscribers

People subscribed via source and target branches