Merge ~bdrung/ddeb-retriever/+git/main:pylint into ddeb-retriever:main

Proposed by Benjamin Drung
Status: Needs review
Proposed branch: ~bdrung/ddeb-retriever/+git/main:pylint
Merge into: ddeb-retriever:main
Diff against target: 204 lines (+32/-34)
3 files modified
archive_tools.py (+17/-17)
ddeb_retriever.py (+5/-6)
lpinfo.py (+10/-11)
Reviewer Review Type Date Requested Status
Steve Langasek Pending
Review via email: mp+437384@code.launchpad.net

Description of the change

This merge request contains some fixes/improvement for things that pylint or other linters complain about.

1. fix: Iterate over apt_pkg.TagFile

pylint complains about E1136: Value 'parser.section' is unsubscriptable
(unsubscriptable-object). `apt_pkg.TagFile` claims to be an iterator
over `apt_pkg.TagSection`.

So iterate over `apt_pkg.TagFile` for the sections instead of calling
`step()` and accessing the `section` property.

This change was tested to also work on Ubuntu 16.04 "xenial" with
Python 3.5.2 and python3-apt 1.1.0~beta1ubuntu0.16.04.12.

2. Use `functools.cache` for `_get_lp()` to avoid using a global variable
and make the code more readable.

3. refactor: Mark unused variables as such

4. fix: Use lazy % formatting in logging functions

5. Use `timestamp()` method of datetime instead of calculating it manually.

To post a comment you must log in.

Unmerged commits

6eb54c2... by Benjamin Drung

refactor: Use datetime.timestamp()

Use `timestamp()` method of datetime instead of calculating it manually.

Signed-off-by: Benjamin Drung <email address hidden>

f47a5a5... by Benjamin Drung

fix: Use lazy % formatting in logging functions

Signed-off-by: Benjamin Drung <email address hidden>

608b211... by Benjamin Drung

refactor: Mark unused variables as such

Name unused variables `_` to indicate to pylint and other linters that
these variables are not used.

Signed-off-by: Benjamin Drung <email address hidden>

8639da0... by Benjamin Drung

refactor: Use functools.cache for _get_lp()

Use `functools.cache` for `_get_lp()` to avoid using a global variable
and make the code more readable.

Signed-off-by: Benjamin Drung <email address hidden>

e207297... by Benjamin Drung

fix: Iterate over apt_pkg.TagFile

pylint complains about E1136: Value 'parser.section' is unsubscriptable
(unsubscriptable-object). `apt_pkg.TagFile` claims to be an iterator
over `apt_pkg.TagSection`.

So iterate over `apt_pkg.TagFile` for the sections instead of calling
`step()` and accessing the `section` property.

This change was tested to also work on Ubuntu 16.04 "xenial" with
Python 3.5.2 and python3-apt 1.1.0~beta1ubuntu0.16.04.12.

Signed-off-by: Benjamin Drung <email address hidden>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/archive_tools.py b/archive_tools.py
index c8cf1aa..ecfb57f 100644
--- a/archive_tools.py
+++ b/archive_tools.py
@@ -66,13 +66,13 @@ def debcontrol_map(url, map_key='Package'):
6666
67 map = {}67 map = {}
68 parser = apt_pkg.TagFile(tagfile)68 parser = apt_pkg.TagFile(tagfile)
69 while parser.step():69 for section in parser:
70 pkg = {}70 pkg = {}
71 for key in parser.section.keys():71 for key in section.keys():
72 if key != map_key:72 if key != map_key:
73 pkg[key] = parser.section.get(key)73 pkg[key] = section.get(key)
74 if pkg:74 if pkg:
75 map[parser.section.get(map_key)] = pkg75 map[section.get(map_key)] = pkg
76 tagfile.close()76 tagfile.close()
77 return map77 return map
7878
@@ -156,9 +156,9 @@ def debcontrol_dominate(infile, outfile, group_by='Package',
156 '''156 '''
157 max = {} # group_by -> (sort_key_value, text)157 max = {} # group_by -> (sort_key_value, text)
158 parser = apt_pkg.TagFile(infile)158 parser = apt_pkg.TagFile(infile)
159 while parser.step():159 for section in parser:
160 if cmp(max.get(parser.section[group_by], ('', ''))[0], parser.section[sort_key]) < 0:160 if cmp(max.get(section[group_by], ('', ''))[0], section[sort_key]) < 0:
161 max[parser.section[group_by]] = (parser.section[sort_key], str(parser.section))161 max[section[group_by]] = (section[sort_key], str(section))
162162
163 ks = sorted(max)163 ks = sorted(max)
164 first = True164 first = True
@@ -292,7 +292,7 @@ Tree "dists/%s" {
292 listfile = open(os.path.join('lists', '%s-%s' % (component, arch)), 'w')292 listfile = open(os.path.join('lists', '%s-%s' % (component, arch)), 'w')
293 for f in glob.glob(os.path.join(pooldir, '*/*/*%s%s' % (arch, ext))):293 for f in glob.glob(os.path.join(pooldir, '*/*/*%s%s' % (arch, ext))):
294 try:294 try:
295 (pkg, ver, suffix) = os.path.basename(f).split('_')295 (pkg, ver, _) = os.path.basename(f).split('_')
296 except ValueError:296 except ValueError:
297 logging.warning('Cannot split file name %s, unknown format; ignoring', f)297 logging.warning('Cannot split file name %s, unknown format; ignoring', f)
298 continue298 continue
@@ -350,8 +350,8 @@ def _packages_files(path, files):
350350
351 with open(path) as f:351 with open(path) as f:
352 parser = apt_pkg.TagFile(f)352 parser = apt_pkg.TagFile(f)
353 while parser.step():353 for section in parser:
354 f = parser.section['Filename']354 f = section['Filename']
355 if f.startswith('./'):355 if f.startswith('./'):
356 f = f[2:]356 f = f[2:]
357 files.add(f)357 files.add(f)
@@ -363,9 +363,9 @@ def _sources_files(path, files):
363363
364 with open(path) as f:364 with open(path) as f:
365 parser = apt_pkg.TagFile(f)365 parser = apt_pkg.TagFile(f)
366 while parser.step():366 for section in parser:
367 d = parser.section.get('Directory')367 d = section.get('Directory')
368 for l in parser.section['Files'].splitlines():368 for l in section['Files'].splitlines():
369 fields = l.split()369 fields = l.split()
370 assert len(fields) == 3370 assert len(fields) == 3
371 if d:371 if d:
@@ -390,7 +390,7 @@ def archive_cleanup(archive_root, keep_days=0):
390 distroot = '.'390 distroot = '.'
391391
392 indexed_files = set([])392 indexed_files = set([])
393 for root, dirs, files in os.walk(distroot):393 for root, _, files in os.walk(distroot):
394 if root == '.':394 if root == '.':
395 root = ''395 root = ''
396 elif root.startswith('./'):396 elif root.startswith('./'):
@@ -424,7 +424,7 @@ def archive_cleanup(archive_root, keep_days=0):
424 poolroot = 'pool'424 poolroot = 'pool'
425 if not os.path.isdir(poolroot):425 if not os.path.isdir(poolroot):
426 poolroot = '.'426 poolroot = '.'
427 for root, dirs, files in os.walk(poolroot):427 for root, _, files in os.walk(poolroot):
428 if root == '.':428 if root == '.':
429 root = ''429 root = ''
430 if root.startswith('./'):430 if root.startswith('./'):
@@ -475,7 +475,7 @@ def archive_dominate(archive_root, keep_days=0):
475 if not os.path.isdir(distroot):475 if not os.path.isdir(distroot):
476 distroot = archive_root476 distroot = archive_root
477477
478 for root, dirs, files in os.walk(distroot):478 for root, _, files in os.walk(distroot):
479 if 'Packages' in files:479 if 'Packages' in files:
480 path = os.path.join(root, 'Packages')480 path = os.path.join(root, 'Packages')
481 with open(path) as old, open(path + '.new', 'w') as new:481 with open(path) as old, open(path + '.new', 'w') as new:
@@ -751,7 +751,7 @@ def _find_files(dir, strip_prefix=None):
751 else:751 else:
752 plen = 0752 plen = 0
753 result = set([])753 result = set([])
754 for root, dirs, files in os.walk(dir):754 for root, _, files in os.walk(dir):
755 for f in files:755 for f in files:
756 result.add(os.path.join(root, f)[plen:])756 result.add(os.path.join(root, f)[plen:])
757 return result757 return result
diff --git a/ddeb_retriever.py b/ddeb_retriever.py
index 2f1ac4d..5bc38b0 100644
--- a/ddeb_retriever.py
+++ b/ddeb_retriever.py
@@ -79,7 +79,7 @@ def install_from_librarian(pub, url, ddeb_archive_root):
79 '''79 '''
80 ddeb = urllib.parse.unquote(os.path.basename(url))80 ddeb = urllib.parse.unquote(os.path.basename(url))
81 try:81 try:
82 (dbgsymname, version, arch) = ddeb.split('_')82 (dbgsymname, _, arch) = ddeb.split('_')
83 assert arch.endswith('.ddeb')83 assert arch.endswith('.ddeb')
84 (arch, _) = arch.split('.')84 (arch, _) = arch.split('.')
85 assert dbgsymname.endswith('-dbgsym')85 assert dbgsymname.endswith('-dbgsym')
@@ -163,7 +163,7 @@ def main(argv=None):
163 # order during long transactions.163 # order during long transactions.
164 real_threshold = lp_threshold - datetime.timedelta(hours=1)164 real_threshold = lp_threshold - datetime.timedelta(hours=1)
165 logging.info(165 logging.info(
166 'Retrieving Launchpad publications since %s' % real_threshold)166 'Retrieving Launchpad publications since %s', real_threshold)
167 else:167 else:
168 real_threshold = None168 real_threshold = None
169 logging.info('Retrieving all Launchpad publications')169 logging.info('Retrieving all Launchpad publications')
@@ -217,8 +217,8 @@ def main(argv=None):
217 installed_pockets = set()217 installed_pockets = set()
218 for i, pub in enumerate(debug_pubs):218 for i, pub in enumerate(debug_pubs):
219 logging.info(219 logging.info(
220 'Installing %d/%d: %s' %220 'Installing %d/%d: %s',
221 (i + 1, len(debug_pubs), pub.display_name))221 i + 1, len(debug_pubs), pub.display_name)
222 already_present = False222 already_present = False
223 pub_installed = False223 pub_installed = False
224 if pub.binary_package_name in ddeb_map:224 if pub.binary_package_name in ddeb_map:
@@ -239,8 +239,7 @@ def main(argv=None):
239 installed_pockets.add(get_suite_for_publication(pub))239 installed_pockets.add(get_suite_for_publication(pub))
240240
241 if latest_date_created is not None:241 if latest_date_created is not None:
242 epoch = datetime.datetime.fromtimestamp(0, tz=UTC)242 new_threshold = latest_date_created.timestamp()
243 new_threshold = (latest_date_created - epoch).total_seconds()
244 with open(lp_threshold_path, 'w') as lp_threshold_file:243 with open(lp_threshold_path, 'w') as lp_threshold_file:
245 lp_threshold_file.write("%d\n" % new_threshold)244 lp_threshold_file.write("%d\n" % new_threshold)
246245
diff --git a/lpinfo.py b/lpinfo.py
index c5410b8..841b75a 100644
--- a/lpinfo.py
+++ b/lpinfo.py
@@ -1,18 +1,17 @@
1import functools
1import logging2import logging
2import os3import os
34
4from launchpadlib.launchpad import Launchpad5from launchpadlib.launchpad import Launchpad
56
6lp = None
7
87
8@functools.cache
9def _get_lp():9def _get_lp():
10 global lp10 return Launchpad.login_anonymously(
11 if lp is None:11 'ddeb-retriever',
12 lp = Launchpad.login_anonymously(12 os.environ.get('LAUNCHPAD_INSTANCE', 'production'),
13 'ddeb-retriever',13 version='devel'
14 os.environ.get('LAUNCHPAD_INSTANCE', 'production'),14 )
15 version='devel')
1615
1716
18def get_series(distribution):17def get_series(distribution):
@@ -20,7 +19,7 @@ def get_series(distribution):
2019
21 Return name -> (components, architectures)20 Return name -> (components, architectures)
22 '''21 '''
23 _get_lp()22 lp = _get_lp()
24 info = {}23 info = {}
25 for s in lp.distributions[distribution].series:24 for s in lp.distributions[distribution].series:
26 # FIXME: disco is still marked as supported in LP, but EOL25 # FIXME: disco is still marked as supported in LP, but EOL
@@ -38,7 +37,7 @@ def get_series(distribution):
38def get_buildds():37def get_buildds():
39 '''Return list of available buildds'''38 '''Return list of available buildds'''
4039
41 _get_lp()40 lp = _get_lp()
42 buildds = []41 buildds = []
4342
44 for builder in lp.builders:43 for builder in lp.builders:
@@ -55,7 +54,7 @@ def get_buildds():
55def get_binary_publications(distribution, created_since_date=None):54def get_binary_publications(distribution, created_since_date=None):
56 '''Get recently-published binaries for the given distribution'''55 '''Get recently-published binaries for the given distribution'''
5756
58 _get_lp()57 lp = _get_lp()
59 archive = lp.distributions[distribution].main_archive58 archive = lp.distributions[distribution].main_archive
60 # It's important to omit the status filter here, even if we later decide59 # It's important to omit the status filter here, even if we later decide
61 # that we only care about the Published status (although at the moment60 # that we only care about the Published status (although at the moment

Subscribers

People subscribed via source and target branches