Skip to content

Commit

Permalink
Add flag for ignoring entries in check pypi script
Browse files Browse the repository at this point in the history
Added argument flag for ignoring package_name/package_version from getting upgrade suggestions using regex.
  • Loading branch information
jonathan-eq committed Sep 5, 2023
1 parent 968c6c6 commit 7fcfe34
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 52 deletions.
64 changes: 50 additions & 14 deletions komodo/check_up_to_date_pypi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import copy
import pathlib
import re
import sys

import requests
Expand Down Expand Up @@ -142,9 +143,14 @@ def run_check_up_to_date(
f"{sys.version_info.micro}"
),
propose_upgrade=False,
ignore=None,
):
yaml = yaml_parser()
releases = load_from_file(yaml, release_file)
releases: dict = load_from_file(yaml, release_file)
if ignore:
for package_name, package_version in list(releases.items()):
if re.search(ignore, f"{package_name} {package_version}"):
del releases[package_name]
repository = load_from_file(yaml, repository_file)
upgrade_proposals_from_pypi = get_upgrade_proposals_from_pypi(
releases,
Expand All @@ -165,20 +171,44 @@ def run_check_up_to_date(
major_upgrades, minor_upgrades, patch_upgrades, other_upgrades = [], [], [], []
for name, versions in upgrade_proposals_from_pypi.items():
pypi_latest = get_version.Version(versions["suggested"])
current_version = get_version.Version(versions["previous"])
if (pypi_latest.major > current_version.major):
major_upgrades.append(f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}")
elif (pypi_latest.minor > current_version.minor):
minor_upgrades.append(f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}")
elif (pypi_latest.micro > current_version.micro):
patch_upgrades.append(f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}")
try:
current_version = get_version.Version(versions["previous"])
except get_version.InvalidVersion:
print(f"Could not parse version {versions['previous']}")
continue
if pypi_latest.major > current_version.major:
major_upgrades.append(
f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}"
)
elif pypi_latest.minor > current_version.minor:
minor_upgrades.append(
f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}"
)
elif pypi_latest.micro > current_version.micro:
patch_upgrades.append(
f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}"
)
else:
other_upgrades.append(f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}")
sys.exit("\n".join(["\nMajor upgrades:"]+major_upgrades +["\nMinor upgrades:"]+ minor_upgrades + ["\nPatch upgrades:"] +patch_upgrades + ["\nOTHER UPGRADES:"] + other_upgrades) + "\n\n\nFound out of date packages!")
other_upgrades.append(
f"{name} not at latest pypi version: {pypi_latest}, "
f"is at: {current_version}"
)
sys.exit(
"\n".join(
["\nMajor upgrades:"]
+ major_upgrades
+ ["\nMinor upgrades:"]
+ minor_upgrades
+ ["\nPatch upgrades:"]
+ patch_upgrades
+ ["\nOTHER UPGRADES:"]
+ other_upgrades
)
+ "\n\n\nFound out of date packages!"
)

else:
print("All packages up to date!!!")
Expand Down Expand Up @@ -232,6 +262,11 @@ def get_args() -> argparse.Namespace:
),
)

parser.add_argument(
"--ignore",
help=("If given, will ignore packages that matches regex expression"),
)

return parser.parse_args()


Expand All @@ -248,4 +283,5 @@ def main():
args.repository_file,
args.python_version,
args.propose_upgrade,
args.ignore,
)
183 changes: 145 additions & 38 deletions tests/test_up_to_date_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,39 +432,28 @@ def test_run_up_to_date(
assert result == expected



@pytest.mark.parametrize(
"release_file_content", [
pytest.param(
"""
"release_file_content",
[
pytest.param(
"""
dummy_package_patch: 1.0.0
dummy_package_minor: 1.0.0
dummy_package_major: 1.0.0
""",
id="patch_minor_major"
),
pytest.param(
"""
id="ignore_main_version",
),
pytest.param(
"""
dummy_package_patch: 1.0.0
dummy_package_major: 1.0.0
dummy_package_minor: 1.0.0
""",
id="patch_major_minor"
),
pytest.param(
"""
dummy_package_minor: 1.0.0
dummy_package_major: 1.0.0
dummy_package_patch: 1.0.0
""",
id="minor_major_patch"
)
]
id="ignore_semantic_version",
),
],
)

def test_upgrade_type_grouping(release_file_content, tmpdir, monkeypatch):


repository_file_content = """
dummy_package_patch:
1.0.0:
Expand All @@ -486,54 +475,172 @@ def test_upgrade_type_grouping(release_file_content, tmpdir, monkeypatch):
"""

with tmpdir.as_cwd():

with open("repository_file.yml", "w") as fout:
fout.write(repository_file_content)
with open("release_file.yml", "w") as fout:
fout.write(release_file_content)
folder_name = os.getcwd()




def side_effect(url: str, timeout):
if "dummy_package_patch" in url.split("/"):
request_mock = MagicMock()
request_json = {
request_json = {
"releases": {"1.0.1": []},
}
request_mock.json.return_value = request_json
return request_mock
elif "dummy_package_minor" in url.split("/"):
request_mock = MagicMock()
request_json = {
request_json = {
"releases": {"1.1.0": []},
}
request_mock.json.return_value = request_json
return request_mock
elif "dummy_package_major" in url.split("/"):
request_mock = MagicMock()
request_json = {
request_json = {
"releases": {"2.0.0": []},
}
request_mock.json.return_value = request_json
return request_mock
else:
raise ValueError("INCORRECT PACKAGE NAME ENTERED! " + str(url) )

raise ValueError("INCORRECT PACKAGE NAME ENTERED! " + str(url))

arguments = [
"script_name",
f"{folder_name}/release_file.yml",
f"{folder_name}/repository_file.yml",
"--propose-upgrade",
"new_file",
]
"script_name",
f"{folder_name}/release_file.yml",
f"{folder_name}/repository_file.yml",
]
monkeypatch.setattr(sys, "argv", arguments)
monkeypatch.setattr(requests, "get", MagicMock(side_effect=side_effect))
with pytest.raises(SystemExit) as system_exit_message:
check_up_to_date_pypi.main()
system_exit_message_list = str(system_exit_message.value).split("\n")
assert "dummy_package_major" in system_exit_message_list[2].split()
assert "dummy_package_minor" in system_exit_message_list[5].split()
assert "dummy_package_patch" in system_exit_message_list[8].split()
assert "dummy_package_patch" in system_exit_message_list[8].split()


@pytest.mark.parametrize(
"release_file_content, ignore_argument",
[
pytest.param(
"""
dummy_package_patch: 1.0.0
dummy_package_minor: 1.0.0
dummy_package_major: 1.0.0
dummy_package_should_not_update: main
""",
"main",
id="ignore_main_version",
),
pytest.param(
"""
dummy_package_patch: 1.0.0
dummy_package_minor: 1.0.0
dummy_package_major: 1.0.0
dummy_package_should_not_update: 1.0.0
""",
"should_not_update",
id="ignore_semantic_version",
),
],
)
def test_upgrade_ignore_flag(
release_file_content, ignore_argument, tmpdir, monkeypatch
):
repository_file_content = """
dummy_package_patch:
1.0.0:
source: pypi
make: pip
maintainer: scout
dummy_package_minor:
1.0.0:
source: pypi
make: pip
maintainer: scout
dummy_package_major:
1.0.0:
source: pypi
make: pip
maintainer: scout
dummy_package_should_not_update:
1.0.0:
source: pypi
make: pip
maintainer: scout
main:
source: pypi
make: pip
maintainer: scout
"""

with tmpdir.as_cwd():
with open("repository_file.yml", "w") as fout:
fout.write(repository_file_content)
with open("release_file.yml", "w") as fout:
fout.write(release_file_content)
folder_name = os.getcwd()

def side_effect(url: str, timeout):
if "dummy_package_patch" in url.split("/"):
request_mock = MagicMock()
request_json = {
"releases": {"1.0.1": []},
}
request_mock.json.return_value = request_json
return request_mock
elif "dummy_package_minor" in url.split("/"):
request_mock = MagicMock()
request_json = {
"releases": {"1.1.0": []},
}
request_mock.json.return_value = request_json
return request_mock
elif "dummy_package_major" in url.split("/"):
request_mock = MagicMock()
request_json = {
"releases": {"2.0.0": []},
}
request_mock.json.return_value = request_json
return request_mock
elif "dummy_package_main_should_not_update" in url.split("/"):
request_mock = MagicMock()
request_json = {
"releases": {"4.4.3": []},
}
request_mock.json.return_value = request_json
return request_mock
elif "dummy_package_should_not_update" in url.split("/"):
request_mock = MagicMock()
request_json = {
"releases": {"2.0.0": []},
}
request_mock.json.return_value = request_json
return request_mock
else:
raise ValueError("INCORRECT PACKAGE NAME ENTERED! " + str(url))

arguments = [
"",
f"{folder_name}/release_file.yml",
f"{folder_name}/repository_file.yml",
"--ignore",
ignore_argument,
]
monkeypatch.setattr(sys, "argv", arguments)
monkeypatch.setattr(requests, "get", MagicMock(side_effect=side_effect))
with pytest.raises(SystemExit) as system_exit_message:
check_up_to_date_pypi.main()
assert (
"dummy_package_should_not_update" not in str(system_exit_message.value).split()
)
assert (
"dummy_package_main_should_not_update"
not in str(system_exit_message.value).split()
)

0 comments on commit 7fcfe34

Please sign in to comment.