Skip to content

Commit

Permalink
feat: add support for --constraints-path flag
Browse files Browse the repository at this point in the history
test: add tests for installation of local wheels with constraints
test: generated new wheels for testing
  • Loading branch information
oll-bot committed Sep 17, 2024
1 parent 90a3c05 commit 4bfc7ae
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 2 deletions.
22 changes: 21 additions & 1 deletion upgrade/scripts/upgrade_python_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def upgrade_and_run(
cloudsmith_url=None,
update_all=False,
slack_webhook_url=None,
constraints_path = None,
*args,
):
"""
Expand All @@ -59,6 +60,7 @@ def upgrade_and_run(
cloudsmith_url,
update_all,
slack_webhook_url,
constraints_path,
)
else:
logging.info('Trying to upgrade "%s" package.', package_name)
Expand All @@ -67,6 +69,7 @@ def upgrade_and_run(
cloudsmith_url,
update_all,
slack_webhook_url,
constraints_path,
*args,
)
if not skip_post_install and (was_updated or force):
Expand Down Expand Up @@ -201,6 +204,7 @@ def install_wheel(
version_cmd=None,
update_all=False,
slack_webhook_url=None,
constraints_path=None,
*args,
):
"""
Expand Down Expand Up @@ -265,7 +269,7 @@ def install_wheel(
resp += pip("check")
except:
# try to install with constraints
constraints_file_path = get_constraints_file_path(package_name)
constraints_file_path = constraints_path or get_constraints_file_path(package_name)
try:
resp += install_with_constraints(
to_install,
Expand Down Expand Up @@ -310,6 +314,7 @@ def upgrade_from_local_wheel(
wheels_path=None,
update_all=False,
version=None,
constraints_path=None,
):
resp = ""
package_name, _ = split_package_name_and_extra(package_install_cmd)
Expand All @@ -321,6 +326,7 @@ def upgrade_from_local_wheel(
wheels_path=wheels_path,
update_all=update_all,
version_cmd=version,
constraints_path=constraints_path,
)
except Exception as e:
response_err = str(e)
Expand All @@ -337,6 +343,7 @@ def attempt_to_install_version(
cloudsmith_url=None,
update_all=False,
slack_webhook_url=None,
constraints_path=None,
):
"""
attempt to install a specific version of the given package
Expand All @@ -355,6 +362,7 @@ def attempt_to_install_version(
version,
update_all,
slack_webhook_url,
constraints_path,
*args,
)
except Exception as e:
Expand All @@ -369,6 +377,7 @@ def attempt_upgrade(
cloudsmith_url=None,
update_all=False,
slack_webhook_url=None,
constraints_path=None,
*args,
):
"""
Expand All @@ -390,6 +399,7 @@ def attempt_upgrade(
None,
update_all,
slack_webhook_url,
constraints_path,
*args,
)
was_upgraded = "Requirement already up-to-date" not in resp
Expand Down Expand Up @@ -561,6 +571,11 @@ def try_running_module(wheel, cloudsmith_url=None, slack_webhook_url=None, *args
default=None,
help="Slack webhook url string for sending slack notifications on failed upgrade",
)
parser.add_argument(
"--constraints-path",
action="store",
help="Path to constraints.txt file"
)


def upgrade_python_package(
Expand All @@ -577,6 +592,7 @@ def upgrade_python_package(
format_output=False,
update_all=False,
slack_webhook_url=None,
constraints_path=None,
*vars,
):
success = False
Expand All @@ -603,6 +619,7 @@ def upgrade_python_package(
cloudsmith_url=cloudsmith_url,
update_all=update_all,
version=version,
constraints_path=constraints_path,
*vars,
)
elif should_run_initial_post_install:
Expand All @@ -616,6 +633,7 @@ def upgrade_python_package(
cloudsmith_url,
update_all,
slack_webhook_url,
constraints_path,
*vars,
)
except Exception as e:
Expand Down Expand Up @@ -646,6 +664,7 @@ def main():
format_output = parsed_args.format_output
update_all = parsed_args.update_all
slack_webhook_url = parsed_args.slack_webhook_url
constraints_path = parsed_args.constraints_path
upgrade_python_package(
package,
wheels_path,
Expand All @@ -660,6 +679,7 @@ def main():
format_output,
update_all,
slack_webhook_url,
constraints_path,
*parsed_args.vars,
)

Expand Down
2 changes: 1 addition & 1 deletion upgrade/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def run(*command, **kwargs):
options.update(kwargs)
completed = subprocess.run(command, **options)
except subprocess.CalledProcessError as err:
logging.warning('Error occurred while running command "%s"', *command)
logging.warning('Error occurred while running command "%s"', " ".join(command))
if err.stdout:
print(err.stdout)
logging.warning(err.stdout)
Expand Down
1 change: 1 addition & 0 deletions upgrade/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

VENV_PATH = Path(__file__).parent / "venv"
REPOSITORY_WHEELS_PATH = Path(__file__).parent / "repository"
DATA_PATH = Path(__file__).parent / "data"


@pytest.fixture
Expand Down
3 changes: 3 additions & 0 deletions upgrade/tests/data/test_constraints_flag/constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
oll-test-top-level==2.0.2
oll-dependency1==2.0.0
oll-dependency2==2.0.0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from upgrade.scripts.upgrade_python_package import upgrade_from_local_wheel
from ..conftest import DATA_PATH
from pathlib import Path


def test_existing_dependencies_in_vm_when_expected_packages_are_not_installed_expect_false(
Expand Down Expand Up @@ -132,3 +134,61 @@ def test_upgrade_top_level_package_from_local_wheel_where_package_name_does_not_
expected = "Failed to install wheel"
actual = out
assert expected in actual


def test_upgrade_local_wheel_test_constraints_flag(
wheels_dir, use_pip, mocked_constraints_path
):
package = "oll-test-top-level==2.0.2"
cut = upgrade_from_local_wheel
cut(
package,
skip_post_install=True,
wheels_path=str(wheels_dir),
constraints_path=str(Path(DATA_PATH) / "test_constraints_flag/constraints.txt"),
version="~=2.0.2",
)
dependencies_from_venv = use_pip(
"list",
"--format=freeze",
"--exclude-editable",
).splitlines()

expected_packages = {
"oll-test-top-level==2.0.2",
"oll-dependency1==2.0.0",
"oll-dependency2==2.0.0",
}
actual_packages = set(dependencies_from_venv)

expected = True
actual = expected_packages.issubset(actual_packages)
assert actual == expected

def test_upgrade_local_wheel_test_no_constraints_flag(
wheels_dir, use_pip, mocked_constraints_path
):
package = "oll-test-top-level==2.0.2"
cut = upgrade_from_local_wheel
cut(
package,
skip_post_install=True,
wheels_path=str(wheels_dir),
version="~=2.0.2",
)
dependencies_from_venv = use_pip(
"list",
"--format=freeze",
"--exclude-editable",
).splitlines()

expected_packages = {
"oll-test-top-level==2.0.2",
"oll-dependency1==2.0.2",
"oll-dependency2==2.0.2",
}
actual_packages = set(dependencies_from_venv)

expected = True
actual = expected_packages.issubset(actual_packages)
assert actual == expected

0 comments on commit 4bfc7ae

Please sign in to comment.