Adjust code scanner warnings

- Replace map+filter and list+filter with list comprehension expression
- Use try/except for next(Iterator)
- Add test case for altlink that cause exception on next() in get_altlink
- Avoid exit but use sys.exit

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2022-11-20 15:13:32 +09:00
parent 452f8d74b2
commit 1dbd73450a
7 changed files with 56 additions and 15 deletions

View File

@@ -23,7 +23,7 @@ import posixpath
from dataclasses import dataclass, field
from logging import getLogger
from typing import Dict, Iterable, List, Optional, Tuple
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import Element # noqa
from defusedxml import ElementTree

View File

@@ -239,13 +239,10 @@ def altlink(url: str, alt: str) -> str:
else:
# Return first priority item which is not blacklist in mirrors list,
# if not found then return alt in default
return next(
filter(
lambda mirror: not any(mirror.startswith(b) for b in Settings.blacklist),
mirrors,
),
alt,
)
try:
return next(mirror for mirror in mirrors if not any(mirror.startswith(b) for b in Settings.blacklist))
except StopIteration:
return alt
class MyQueueListener(QueueListener):

View File

@@ -600,8 +600,8 @@ class MetadataFactory:
versions_extensions = self.get_versions_extensions(
self.fetch_http(self.archive_id.to_url(), False), self.archive_id.category
)
opt_versions = map(lambda _tuple: _tuple[0], filter(filter_by, versions_extensions))
versions: List[Version] = sorted(filter(None, opt_versions))
opt_versions = [_tuple[0] for _tuple in filter(filter_by, versions_extensions)]
versions: List[Version] = sorted([x for x in filter(None, opt_versions)])
iterables = cast(Iterable[Tuple[int, Iterable[Version]]], itertools.groupby(versions, lambda version: version.minor))
return Versions(iterables)
@@ -637,7 +637,7 @@ class MetadataFactory:
return None
# Remove items that don't conform to simple_spec
tools_versions = list(filter(lambda tool_item: tool_item[2] in simple_spec, tools_versions))
tools_versions = [tool_item for tool_item in tools_versions if tool_item[2] in simple_spec]
try:
# Return the conforming item with the highest version.
@@ -899,7 +899,7 @@ class MetadataFactory:
return "gcc_64"
elif self.archive_id.host == "mac":
return "clang_64"
arches = list(filter(lambda arch: QtRepoProperty.MINGW_ARCH_PATTERN.match(arch), self.fetch_arches(version)))
arches = [arch for arch in self.fetch_arches(version) if QtRepoProperty.MINGW_ARCH_PATTERN.match(arch)]
selected_arch = QtRepoProperty.select_default_mingw(arches, is_dir=False)
if not selected_arch:
raise EmptyMetadata("No default desktop architecture available")

View File

@@ -3,6 +3,7 @@
import argparse
import json
import logging
import sys
from pathlib import Path
from typing import Dict, Generator, Iterable, List, Optional, Tuple, Union
@@ -206,6 +207,6 @@ if __name__ == "__main__":
tqdm = get_tqdm(args.no_tqdm)
exit(
sys.exit(
main(filename=json_filename, is_write_file=args.write, is_verbose=args.verbose)
)

View File

@@ -207,7 +207,10 @@ def test_tools_variants(monkeypatch, tool_name, tool_variant_name, is_expect_fai
return
expect_json = json.loads((Path(__file__).parent / "data" / f"{datafile}-expect.json").read_text("utf-8"))
expect = next(filter(lambda x: x["Name"] == tool_variant_name, expect_json["variants_metadata"]))
try:
expect = next(x for x in expect_json["variants_metadata"] if x["Name"] == tool_variant_name)
except StopIteration:
raise Exception("Wrong test data.")
expected_7z_files = set(expect["DownloadableArchives"])
qt_pkgs = ToolArchives(host, target, tool_name, base, arch=tool_variant_name).archives
url_begin = f"online/qtsdkrepository/mac_x64/{target}/{tool_name}"

View File

@@ -63,6 +63,46 @@ def test_helper_altlink(monkeypatch):
assert newurl.startswith("http://ftp.jaist.ac.jp/")
def test_helper_altlink_black(monkeypatch):
class Message:
headers = {"content-type": "text/plain", "length": 300}
text = """<?xml version="1.0" encoding="UTF-8"?>
<metalink xmlns="urn:ietf:params:xml:ns:metalink">
<generator>MirrorBrain/2.17.0</generator>
<origin dynamic="true">http://download.example.io/boo.7z.meta4</origin>
<published>2020-03-04T01:11:48Z</published>
<publisher>
<name>Example Project</name>
<url>https://download.example.io</url>
</publisher>
<file name="boo.7z">
<size>651</size>
<hash type="md5">d49eba3937fb063caa48769e8f28377c</hash>
<hash type="sha-1">25d3a33d00c1e5880679a17fd4b8b831134cfa6f</hash>
<hash type="sha-256">37e50248cf061109e2cb92105cd2c36a6e271701d6d4a72c4e73c6d82aad790a</hash>
<pieces length="262144" type="sha-1">
<hash>bec628a149ed24a3a9b83747776ecca5a1fad11c</hash>
<hash>98b1dee3f741de51167a9428b0560cd2d1f4d945</hash>
<hash>8717a0cb3d14c1958de5981635c9b90b146da165</hash>
<hash>78cd2ae3ae37ca7c080a56a2b34eb33ec44a9ef1</hash>
</pieces>
<url location="cn" priority="1">http://mirrors.geekpie.club/boo.7z</url>
</file>
</metalink>
"""
def mock_return(url):
return Message()
monkeypatch.setattr(helper, "_get_meta", mock_return)
url = "http://foo.baz/qtproject/boo.7z"
alt = "http://ftp.yz.yamagata-u.ac.jp/pub/boo.7z"
newurl = helper.altlink(url, alt)
assert newurl.startswith("http://ftp.yz.yamagata-u.ac.jp/pub/boo.7z")
@pytest.mark.load_default_settings(False)
def test_settings(tmp_path):
helper.Settings.load_settings(os.path.join(os.path.dirname(__file__), "data", "settings.ini"))

View File

@@ -310,7 +310,7 @@ def test_list_archives(
else:
expected_mod_metadata = expect["modules_metadata_by_arch"][arch]
if "all" not in modules_to_query:
expected_mod_metadata = filter(lambda mod: mod["Name"].split(".")[-2] in modules_to_query, expected_mod_metadata)
expected_mod_metadata = [mod for mod in expected_mod_metadata if mod["Name"].split(".")[-2] in modules_to_query]
expected = set([arc.split("-")[0] for mod in expected_mod_metadata for arc in mod["DownloadableArchives"]])
archives_query = [version, arch, *modules_to_query]