-
Notifications
You must be signed in to change notification settings - Fork 1
Enable python 3.12 #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,60 @@ | ||
| import tempfile | ||
| import re | ||
| import subprocess | ||
| from typing import Optional | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def find_latest_version(pkg_name: str, major: Optional[int] = None) -> str: | ||
| output = subprocess.check_output( | ||
| [ | ||
| "pip", | ||
| "install", | ||
| "--no-deps", | ||
| "--ignore-installed", | ||
| "--no-cache-dir", | ||
| "--dry-run", | ||
| f"{pkg_name}{f'=={major}.*' if major is not None else ''}", | ||
| ], | ||
| stderr=subprocess.DEVNULL, | ||
| pip_log = _pretend_pip_install(pkg_name=pkg_name, major=major) | ||
| would_install_line = _extract_would_install_line(pip_log=pip_log) | ||
| package_with_version = _extract_package_with_version( | ||
| pip_log_would_install_line=would_install_line, pkg_name=pkg_name, major=major | ||
| ) | ||
| output_lines = output.decode("utf-8").splitlines() | ||
| would_install_line = [line for line in output_lines if "Would install" in line][0] | ||
| regex_rule = f"{pkg_name.replace('_', '-')}-{major if major is not None else ''}.[^ ]+" | ||
| match = re.search(regex_rule, would_install_line.replace("_", "-")) | ||
| assert match is not None | ||
| return match[0][len(f"{pkg_name}-") :] | ||
| return package_with_version[len(f"{pkg_name}-") :] | ||
|
|
||
|
|
||
| def increase_minor(version: str) -> str: | ||
| major, minor, patch = version.split(".") | ||
| assert major.isdigit() and minor.isdigit() | ||
| return f"{major}.{int(minor) + 1}.0" | ||
|
|
||
|
|
||
| def _pretend_pip_install(pkg_name: str, major: Optional[int]) -> str: | ||
| with tempfile.NamedTemporaryFile() as log_file: | ||
| result = subprocess.run( | ||
| [ | ||
| "pip", | ||
| "install", | ||
| "--no-deps", | ||
| "--ignore-installed", | ||
| "--dry-run", | ||
| f"--log={log_file.name}", | ||
| f"{pkg_name}{f'=={major}.*' if major is not None else ''}", | ||
| ], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| ) | ||
| if result.returncode != 0: | ||
| raise subprocess.SubprocessError( | ||
| f"Something went wrong while using pip to find the version of {pkg_name}{f'(major version {major})' if major else ''}.\n\nSTDOUT: {result.stdout}\n\nSTDERR: {result.stderr}" | ||
| ) | ||
| return Path(log_file.name).read_text() | ||
|
|
||
|
|
||
| def _extract_would_install_line(pip_log: str) -> str: | ||
| would_install_lines = [line for line in pip_log.splitlines() if "Would install" in line] | ||
| assert ( | ||
| len(would_install_lines) == 1 | ||
| ), f"Expected exactly one line containing 'Would install' in the pip log generated by pip installing the requested package, but found {len(would_install_lines)} lines." | ||
| return would_install_lines[0] | ||
|
|
||
|
|
||
| def _extract_package_with_version(pip_log_would_install_line: str, pkg_name: str, major: Optional[int]) -> str: | ||
| regex_rule = f"{pkg_name.replace('_', '-')}-{major if major is not None else ''}.[^ ]+" | ||
| match = re.search(regex_rule, pip_log_would_install_line.replace("_", "-")) | ||
| assert ( | ||
| match is not None and match[0] != "" | ||
| ), f"It was not possible to determine the version, because we could not find a match to the regex {regex_rule} in {pip_log_would_install_line.replace('_', '-')}" | ||
| return match[0] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,40 @@ | ||
| import subprocess | ||
| from test.data import get_pip_log_with_dash, get_pip_log_with_underscore | ||
| from unittest.mock import MagicMock | ||
| from unittest.mock import patch | ||
|
|
||
| from dac._version_management import find_latest_version | ||
| from pytest import MonkeyPatch, fixture | ||
|
|
||
|
|
||
| def test_if_pkg_name_uses_dash_separator_and_pip_log_dash_then_correct_latest_version(mock_pip_output_with_dash: None): | ||
| latest_version = find_latest_version(pkg_name="investing-algorithm-framework") | ||
| assert latest_version == "2.3.2" | ||
| def test_if_pkg_name_uses_dash_separator_and_pip_log_dash_then_correct_latest_version(): | ||
| with patch("dac._version_management._pretend_pip_install") as mock_pretend_pip_install: | ||
| mock_pretend_pip_install.return_value = get_pip_log_with_dash() | ||
|
|
||
| latest_version = find_latest_version(pkg_name="investing-algorithm-framework") | ||
|
|
||
| def test_if_pkg_name_uses_dash_separator_and_pip_log_underscore_then_correct_latest_version( | ||
| mock_pip_output_with_underscore: None, | ||
| ): | ||
| latest_version = find_latest_version(pkg_name="investing-algorithm-framework") | ||
| assert latest_version == "2.3.2" | ||
| assert latest_version == "2.3.2" | ||
|
|
||
|
|
||
| def test_if_pkg_name_uses_underscore_separator_and_pip_log_dash_then_correct_latest_version( | ||
| mock_pip_output_with_dash: None, | ||
| ): | ||
| latest_version = find_latest_version(pkg_name="investing_algorithm_framework") | ||
| assert latest_version == "2.3.2" | ||
| def test_if_pkg_name_uses_dash_separator_and_pip_log_underscore_then_correct_latest_version(): | ||
| with patch("dac._version_management._pretend_pip_install") as mock_pretend_pip_install: | ||
| mock_pretend_pip_install.return_value = get_pip_log_with_underscore() | ||
|
|
||
| latest_version = find_latest_version(pkg_name="investing-algorithm-framework") | ||
|
|
||
| def test_if_pkg_name_uses_underscore_separator_and_pip_log_underscore_then_correct_latest_version( | ||
| mock_pip_output_with_underscore: None, | ||
| ): | ||
| latest_version = find_latest_version(pkg_name="investing_algorithm_framework") | ||
| assert latest_version == "2.3.2" | ||
| assert latest_version == "2.3.2" | ||
|
|
||
|
|
||
| @fixture | ||
| def mock_pip_output_with_dash(monkeypatch: MonkeyPatch): | ||
| output = MagicMock() | ||
| output.decode.return_value = get_pip_log_with_dash() | ||
| def test_if_pkg_name_uses_underscore_separator_and_pip_log_dash_then_correct_latest_version(): | ||
| with patch("dac._version_management._pretend_pip_install") as mock_pretend_pip_install: | ||
| mock_pretend_pip_install.return_value = get_pip_log_with_dash() | ||
|
|
||
| def return_foo(*args, **kwargs) -> str: | ||
| return output | ||
| latest_version = find_latest_version(pkg_name="investing_algorithm_framework") | ||
|
|
||
| monkeypatch.setattr(subprocess, "check_output", return_foo) | ||
| assert latest_version == "2.3.2" | ||
|
|
||
|
|
||
| @fixture | ||
| def mock_pip_output_with_underscore(monkeypatch: MonkeyPatch): | ||
| output = MagicMock() | ||
| output.decode.return_value = get_pip_log_with_underscore() | ||
| def test_if_pkg_name_uses_underscore_separator_and_pip_log_underscore_then_correct_latest_version(): | ||
| with patch("dac._version_management._pretend_pip_install") as mock_pretend_pip_install: | ||
| mock_pretend_pip_install.return_value = get_pip_log_with_underscore() | ||
|
|
||
| def return_foo(*args, **kwargs) -> str: | ||
| return output | ||
| latest_version = find_latest_version(pkg_name="investing_algorithm_framework") | ||
|
|
||
| monkeypatch.setattr(subprocess, "check_output", return_foo) | ||
| assert latest_version == "2.3.2" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.