commit: beb8b645c88c95bab512c04c1df811e7cf5b3250 Author: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> AuthorDate: Sat Oct 22 12:35:20 2022 +0000 Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> CommitDate: Wed Oct 26 18:45:13 2022 +0000 URL: https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=beb8b645
BetterCompressionCheck: detect better compression uris Resolves: https://github.com/pkgcore/pkgcheck/issues/479 Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org> src/pkgcheck/checks/codingstyle.py | 44 ++++++++++++++++++++++++++++++++++++++ tests/checks/test_codingstyle.py | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/pkgcheck/checks/codingstyle.py b/src/pkgcheck/checks/codingstyle.py index 54d57580..e8a2f45a 100644 --- a/src/pkgcheck/checks/codingstyle.py +++ b/src/pkgcheck/checks/codingstyle.py @@ -391,6 +391,50 @@ class ObsoleteUriCheck(Check): yield ObsoleteUri(lineno, uri, regexp.sub(repl, uri), pkg=pkg) +class BetterCompressionUri(results.LineResult, results.Style): + """URI provider has better compression suggestion. + + The URI used to fetch distfile doesn't use the best compression + available from the provider. Using better compression can save + bandwidth for the users and mirrors. + """ + + def __init__(self, replacement, **kwargs): + super().__init__(**kwargs) + self.replacement = replacement + + @property + def desc(self): + return (f"line {self.lineno}: better compression URI using extension " + f"{self.replacement!r} for {self.line!r}") + + +class BetterCompressionCheck(Check): + """Scan ebuild for URIs with better compression.""" + + _source = sources.EbuildFileRepoSource + known_results = frozenset([BetterCompressionUri]) + + REGEXPS = ( + (r'.*\b(?P<uri>(?P<prefix>https?://[^/]*?gitlab[^/]*?/.*/-/archive/.*?/\S*)\.(?:tar\.gz|tar|zip))', + '.tar.bz2'), + ) + + def __init__(self, *args): + super().__init__(*args) + self.regexes = tuple((re.compile(regexp), repl) for regexp, repl in self.REGEXPS) + + def feed(self, pkg): + for lineno, line in enumerate(pkg.lines, 1): + if not line.strip() or line.startswith('#'): + continue + # searching for multiple matches on a single line is too slow + for regexp, replacement in self.regexes: + if mo := regexp.match(line): + uri = mo.group('uri') + yield BetterCompressionUri(replacement, lineno=lineno, line=uri, pkg=pkg) + + class HomepageInSrcUri(results.VersionResult, results.Style): """${HOMEPAGE} is referenced in SRC_URI. diff --git a/tests/checks/test_codingstyle.py b/tests/checks/test_codingstyle.py index 0324b349..3becc919 100644 --- a/tests/checks/test_codingstyle.py +++ b/tests/checks/test_codingstyle.py @@ -315,6 +315,48 @@ class TestObsoleteUri(misc.ReportTestCase): assert uri in str(r) +class TestBetterCompression(misc.ReportTestCase): + + check_kls = codingstyle.BetterCompressionCheck + + def test_github_archive_uri(self): + uri = 'https://github.com/foo/bar/archive/${PV}.tar.gz' + fake_src = [ + f'SRC_URI="{uri} -> ${{P}}.tar.gz"\n' + ] + fake_pkg = misc.FakePkg("dev-util/diffball-0.5", lines=fake_src) + self.assertNoReport(self.check_kls(None), fake_pkg) + + def test_comment_uri(self): + uri = 'https://gitlab.com/GNOME/${PN}/-/archive/${PV}/${P}.tar' + fake_src = [ + f'#SRC_URI="{uri} -> ${{P}}.tar.gz"\n', + " ", + " ", + f'SRC_URI="{uri} -> ${{P}}.tar.gz"\n', + ] + fake_pkg = misc.FakePkg("dev-util/diffball-0.5", lines=fake_src) + r = self.assertReport(self.check_kls(None), fake_pkg) + assert r.lineno == 4 + + @pytest.mark.parametrize('uri', ( + 'https://gitlab.com/GNOME/${PN}/-/archive/${PV}/${P}.tar', + 'https://gitlab.gnome.org/GNOME/${PN}/-/archive/${PV}/${P}.tar.gz', + 'https://gitlab.gnome.org/GNOME/${PN}/-/archive/${PV}/${P}.zip', + 'https://gitlab.freedesktop.org/glvnd/${PN}/-/archive/v${PV}/${PN}-v${PV}.tar.gz', + )) + def test_gitlab_archive_uri(self, uri): + fake_src = [ + f'SRC_URI="{uri} -> ${{P}}.tar.gz"\n' + ] + fake_pkg = misc.FakePkg("dev-util/diffball-0.5", lines=fake_src) + r = self.assertReport(self.check_kls(None), fake_pkg) + assert r.lineno == 1 + assert r.line == uri + assert r.replacement == '.tar.bz2' + assert uri in str(r) + + class TestStaticSrcUri(misc.ReportTestCase): check_kls = codingstyle.MetadataVarCheck
