Skip to content

Commit

Permalink
feat: Stop sorting eggs
Browse files Browse the repository at this point in the history
* Card ID: CCT-397

We know the preferred order for eggs: ENV > LAST > STABLE > RPM.
It is unlikely that the RPM egg will be newer than STABLE.
  • Loading branch information
m-horky committed Feb 29, 2024
1 parent c1cc88b commit 12a324b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 45 deletions.
40 changes: 1 addition & 39 deletions src/insights_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import sys
import tempfile

import six
import subprocess
from subprocess import Popen, PIPE
from distutils.version import LooseVersion
import logging
import logging.handlers

Expand All @@ -36,41 +34,6 @@ def log(msg):
print(msg, file=sys.stderr)


def egg_version(egg):
'''
Determine the egg version
'''
if not sys.executable:
return None
try:
proc = Popen([sys.executable, '-c', 'from insights.client import InsightsClient; print(InsightsClient(None, False).version())'],
env={'PYTHONPATH': egg, 'PATH': os.getenv('PATH')}, stdout=PIPE, stderr=PIPE)
except OSError:
return None
stdout, stderr = proc.communicate()
if six.PY3:
return stdout.decode('utf-8')
else:
return stdout


def sorted_eggs(eggs):
'''
Sort eggs to go into sys.path by highest version
'''
if len(eggs) < 2:
# nothing to sort
return eggs
# default versions to 0 so LooseVersion doesn't throw a fit
egg0_version = egg_version(eggs[0]) or '0'
egg1_version = egg_version(eggs[1]) or '0'

if LooseVersion(egg0_version) > LooseVersion(egg1_version):
return eggs
else:
return [eggs[1], eggs[0]]


def gpg_validate(path):
"""Verify an egg at given path has valid GPG signature.
Expand Down Expand Up @@ -200,8 +163,7 @@ def _main():
if an egg fails a phase never try it again
"""
# sort rpm and stable eggs after verification
validated_eggs = sorted_eggs(
list(filter(gpg_validate, [STABLE_EGG, RPM_EGG])))
validated_eggs = list(filter(gpg_validate, [STABLE_EGG, RPM_EGG]))
# if ENV_EGG was specified and it's valid, add that to front of sys.path
# so it can be loaded initially. keep it in its own var so we don't
# pass it to run_phase where we load it again
Expand Down
3 changes: 0 additions & 3 deletions src/insights_client/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pytest
# Setuptools is required because we still rely on 'distutils' which have been dropped in Python 3.12.
# setuptools package ships it vendorized as a top-level package, so for a time being we can rely on it.
setuptools
six
-r requirements-core.txt
5 changes: 2 additions & 3 deletions src/insights_client/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ def test_keyboard_interrupt(os_uid, client):
@patch('os.getuid', return_value = 0)
@patch('insights.client.phase.v1.get_phases')
@patch('insights.client.InsightsClient')
@patch('insights_client.sorted_eggs', return_value = "/var/lib/insights/newest.egg")
@patch('insights_client.subprocess.Popen')
@patch('insights_client.enumerate', return_value = [("1", "egg")])
@patch('insights_client.os.path.isfile', return_value = True)
def test_phase_error_100(isfile, enumerate, mock_subprocess, sorted_eggs, client, p, os_uid):
def test_phase_error_100(isfile, enumerate, mock_subprocess, client, p, os_uid):
with pytest.raises(SystemExit) as sys_exit:
mock_subprocess.return_value.returncode= 100
mock_subprocess.return_value.communicate.return_value = ('output', 'error')
insights_client.run_phase(p, client, sorted_eggs)
insights_client.run_phase(p, client, validated_eggs=[""])
assert sys_exit.value.code == 0

0 comments on commit 12a324b

Please sign in to comment.