Skip to content

Commit 786a720

Browse files
committed
Improve complexity
1 parent 25fe3d5 commit 786a720

File tree

4 files changed

+97
-79
lines changed

4 files changed

+97
-79
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ exclude =
66
build,
77
dist
88
max-line-length = 127
9+
max-complexity = 10

.github/workflows/build-and-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: Build and Release Yin-Yang
22

33
on:
4-
pull_request:
5-
branches: [master]
4+
release:
5+
types: [published]
66

77
jobs:
88
build:

.github/workflows/python-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
# stop the build if there are Python syntax errors or undefined names
5454
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
5555
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
56-
poetry run flake8 . --count --max-complexity=10 --statistics
56+
poetry run flake8 . --count --statistics
5757
- name: Test with pytest
5858
run: |
5959
poetry run pytest -v

flatpak-poetry-generator.py

Lines changed: 93 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,33 @@ def get_pypi_source(name: str, version: str, hashes: list) -> tuple:
3838
with urllib.request.urlopen(url) as response:
3939
body = json.loads(response.read().decode("utf-8"))
4040
for release, source_list in body["releases"].items():
41-
if release == version:
42-
for source in source_list:
43-
if (
44-
name == "pyside6-addons"
45-
or name == "pyside6-essentials"
46-
or name == "shiboken6"
47-
):
48-
if (
49-
source["filename"].endswith("x86_64.whl")
50-
and "manylinux" in source["filename"]
51-
):
52-
return source["url"], source["digests"]["sha256"]
53-
if (
54-
source["packagetype"] == "bdist_wheel"
55-
and "py3" in source["python_version"]
56-
and source["digests"]["sha256"] in hashes
57-
):
58-
return source["url"], source["digests"]["sha256"]
59-
for source in source_list:
41+
if release != version:
42+
continue
43+
44+
for source in source_list:
45+
if (
46+
name == "pyside6-addons"
47+
or name == "pyside6-essentials"
48+
or name == "shiboken6"
49+
):
6050
if (
61-
source["packagetype"] == "sdist"
62-
and "source" in source["python_version"]
63-
and source["digests"]["sha256"] in hashes
51+
source["filename"].endswith("x86_64.whl")
52+
and "manylinux" in source["filename"]
6453
):
6554
return source["url"], source["digests"]["sha256"]
55+
if (
56+
source["packagetype"] == "bdist_wheel"
57+
and "py3" in source["python_version"]
58+
and source["digests"]["sha256"] in hashes
59+
):
60+
return source["url"], source["digests"]["sha256"]
61+
for source in source_list:
62+
if (
63+
source["packagetype"] == "sdist"
64+
and "source" in source["python_version"]
65+
and source["digests"]["sha256"] in hashes
66+
):
67+
return source["url"], source["digests"]["sha256"]
6668
else:
6769
raise Exception("Failed to extract url and hash from {}".format(url))
6870

@@ -80,49 +82,67 @@ def get_module_sources(parsed_lockfile: dict, include_devel: bool = True) -> lis
8082
sources = []
8183
hash_re = re.compile(r"(sha1|sha224|sha384|sha256|sha512|md5):([a-f0-9]+)")
8284
for section, packages in parsed_lockfile.items():
83-
if section == "package":
84-
for package in packages:
85-
if "category" not in package or (
86-
(
87-
package.get("category") == "dev"
88-
and include_devel
89-
and not package.get("optional")
90-
)
91-
or (
92-
package.get("category") == "main"
93-
and not package.get("optional")
94-
)
95-
):
96-
hashes = []
97-
# Check for old metadata format (poetry version < 1.0.0b2)
98-
if "hashes" in parsed_lockfile["metadata"]:
99-
hashes = parsed_lockfile["metadata"]["hashes"][package["name"]]
100-
# metadata format 1.1
101-
elif "files" in parsed_lockfile["metadata"]:
102-
for package_name in parsed_lockfile["metadata"]["files"]:
103-
if package_name == package["name"]:
104-
package_files = parsed_lockfile["metadata"]["files"][
105-
package["name"]
106-
]
107-
num_files = len(package_files)
108-
for num in range(num_files):
109-
match = hash_re.search(package_files[num]["hash"])
110-
if match:
111-
hashes.append(match.group(2))
112-
# metadata format 2.0
113-
else:
114-
for file in package["files"]:
115-
match = hash_re.search(file["hash"])
116-
if match:
117-
hashes.append(match.group(2))
118-
url, hash = get_pypi_source(
119-
package["name"], package["version"], hashes
120-
)
121-
source = {"type": "file", "url": url, "sha256": hash}
122-
sources.append(source)
85+
if section != "package":
86+
continue
87+
88+
for package in packages:
89+
if "category" in package and (
90+
package.get("category") != "dev" or not include_devel or package.get("optional")) and (
91+
package.get("category") != "main" or package.get("optional")):
92+
continue
93+
94+
hashes = []
95+
# Check for old metadata format (poetry version < 1.0.0b2)
96+
if "hashes" in parsed_lockfile["metadata"]:
97+
hashes = parsed_lockfile["metadata"]["hashes"][package["name"]]
98+
# metadata format 1.1
99+
elif "files" in parsed_lockfile["metadata"]:
100+
hashes.append(get_sources_11(package, parsed_lockfile, hash_re))
101+
# metadata format 2.0
102+
else:
103+
hashes.append(get_sources_13(package, hash_re))
104+
105+
url, hash = get_pypi_source(
106+
package["name"], package["version"], hashes
107+
)
108+
source = {"type": "file", "url": url, "sha256": hash}
109+
sources.append(source)
123110
return sources
124111

125112

113+
def get_sources_11(package, parsed_lockfile, hash_re) -> list:
114+
hashes = []
115+
for package_name in parsed_lockfile["metadata"]["files"]:
116+
if package_name != package["name"]:
117+
continue
118+
119+
package_files = parsed_lockfile["metadata"]["files"][
120+
package["name"]
121+
]
122+
num_files = len(package_files)
123+
for num in range(num_files):
124+
match = hash_re.search(package_files[num]["hash"])
125+
if not match:
126+
continue
127+
128+
hashes.append(match.group(2))
129+
130+
return hashes
131+
132+
133+
def get_sources_13(package, hash_re) -> list:
134+
hashes = []
135+
136+
for file in package["files"]:
137+
match = hash_re.search(file["hash"])
138+
if not match:
139+
continue
140+
141+
hashes.append(match.group(2))
142+
143+
return hashes
144+
145+
126146
def get_dep_names(parsed_lockfile: dict, include_devel: bool = True) -> list:
127147
"""Gets the list of dependency names.
128148
@@ -135,20 +155,17 @@ def get_dep_names(parsed_lockfile: dict, include_devel: bool = True) -> list:
135155
"""
136156
dep_names = []
137157
for section, packages in parsed_lockfile.items():
138-
if section == "package":
139-
for package in packages:
140-
if "category" not in package or (
141-
(
142-
package.get("category") == "dev"
143-
and include_devel
144-
and not package.get("optional")
145-
)
146-
or (
147-
package.get("category") == "main"
148-
and not package.get("optional")
149-
)
150-
):
151-
dep_names.append(package["name"])
158+
if section != "package":
159+
continue
160+
161+
for package in packages:
162+
if "category" in package and (
163+
package.get("category") != "dev" or not include_devel or package.get("optional")) and (
164+
package.get("category") != "main" or package.get("optional")):
165+
continue
166+
167+
dep_names.append(package["name"])
168+
152169
return dep_names
153170

154171

0 commit comments

Comments
 (0)