Skip to content

Commit b08754d

Browse files
authored
Merge pull request #153 from edquist/PackageVersion-cmp
PackageVersion comparison helper class
2 parents c46e4e4 + fe28ba8 commit b08754d

File tree

8 files changed

+51
-92
lines changed

8 files changed

+51
-92
lines changed

osgtest/library/core.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,49 @@ def handler(*args, **kwargs):
6363
'slurm-perlapi',
6464
'slurm-slurmdbd']
6565

66+
67+
# ------------------------------------------------------------------------------
68+
# Helper Classes
69+
# ------------------------------------------------------------------------------
70+
71+
class PackageVersion:
72+
"""Compare the installed rpm version of a package to an "[E:]V[-R]" string.
73+
74+
If the Release field is not specified for [E:]V[-R], it will be ignored
75+
for the package's evr in the comparison. The default Epoch is "0".
76+
77+
Example package version tests:
78+
79+
PackageVersion('osg-release') == '3.4'
80+
PackageVersion('xrootd-lcmaps') >= '1.4.0'
81+
PackageVersion('voms-server') < '2.0.12-3.2'
82+
83+
Version ranges can also be tested in the normal python fashion:
84+
85+
'8.4.0' <= PackageVersion('condor') < '8.8.0'
86+
"""
87+
88+
def __init__(self, pkgname):
89+
e,n,v,r,a = get_package_envra(pkgname)
90+
self.evr = e,v,r
91+
92+
def __repr__(self):
93+
return "%s:%s-%s" % self.evr
94+
95+
def __cmp__(self, evr):
96+
if isinstance(evr, str):
97+
evr = stringToVersion(evr)
98+
else:
99+
raise TypeError('PackageVersion compares to "[E:]V[-R]" string')
100+
101+
if evr[2] is None:
102+
pkg_evr = (self.evr[0], self.evr[1], None)
103+
else:
104+
pkg_evr = self.evr
105+
106+
return rpm.labelCompare(pkg_evr, evr)
107+
108+
66109
# ------------------------------------------------------------------------------
67110
# Global Functions
68111
# ------------------------------------------------------------------------------
@@ -406,47 +449,6 @@ def get_package_envra(package_name):
406449
return (epoch, name, version, release, arch)
407450

408451

409-
def version_compare(evr1, evr2):
410-
"""Compare the EVRs (epoch, version, release) of two RPMs and return
411-
- -1 if the first EVR is older than the second,
412-
- 0 if the two arguments are equal,
413-
- 1 if the first EVR is newer than the second.
414-
415-
Each EVR may be specified as a string (of the form "V-R" or "E:V-R"), or
416-
as a 3-element tuple or list.
417-
418-
"""
419-
if is_string(evr1):
420-
epoch1, version1, release1 = stringToVersion(evr1)
421-
elif isinstance(evr1, bytes):
422-
epoch1, version1, release1 = stringToVersion(evr1.decode())
423-
else:
424-
epoch1, version1, release1 = evr1
425-
426-
if is_string(evr2):
427-
epoch2, version2, release2 = stringToVersion(evr2)
428-
elif isinstance(evr2, bytes):
429-
epoch2, version2, release2 = stringToVersion(evr2.decode())
430-
else:
431-
epoch2, version2, release2 = evr2
432-
433-
return rpm.labelCompare((epoch1, version1, release1), (epoch2, version2, release2))
434-
435-
def package_version_compare(package_name, evr):
436-
"""Compare EVR of installed package_name to provided evr and return:
437-
-1 if the package version is older than evr,
438-
0 if the package version is equal to evr,
439-
1 if the package version is newer than evr.
440-
441-
evr can be a string ("[E:]V[-R]") or 3-element tuple or list.
442-
443-
This is a wrapper around 'version_compare' that avoids having to call
444-
'get_package_envra' and extract (e,v,r)
445-
"""
446-
e,n,v,r,a = get_package_envra(package_name)
447-
return version_compare((e,v,r), evr)
448-
449-
450452
def diagnose(message, command, status, stdout, stderr):
451453
"""Constructs a detailed failure message based on arguments."""
452454
result = message + '\n'

osgtest/library/voms.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ def is_installed():
150150

151151
# TODO: drop this check when 3.3 is completely EOL
152152
if core.el_release() >= 7:
153-
epoch, _, version, release, _ = core.get_package_envra('voms-server')
154-
if core.version_compare((epoch, version, release), '2.0.12-3.2') < 0:
153+
if core.PackageVersion('voms-server') < '2.0.12-3.2':
155154
core.log_message("voms-server installed but too old (missing SOFTWARE-2357 fix)")
156155
return False
157156

osgtest/tests/test_140_lcmaps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_01_configure(self):
2222

2323
def test_02_old_xrootd_policy(self):
2424
core.skip_ok_unless_installed('xrootd-lcmaps', *self.required_rpms)
25-
self.skip_ok_if(core.package_version_compare('xrootd-lcmaps', '1.4.0') >= 0)
25+
self.skip_ok_if(core.PackageVersion('xrootd-lcmaps') >= '1.4.0')
2626

2727
files.append(core.config['lcmaps.db'],
2828
'''xrootd_policy:

osgtest/tests/test_150_xrootd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_01_start_xrootd(self):
5353
if all([core.rpm_is_installed(x) for x in lcmaps_packages]):
5454
core.log_message("Using xrootd-lcmaps authentication")
5555
sec_protocol = '-authzfun:libXrdLcmaps.so -authzfunparms:--loglevel,5'
56-
if core.package_version_compare('xrootd-lcmaps', '1.4.0') >= 0:
56+
if core.PackageVersion('xrootd-lcmaps') >= '1.4.0':
5757
sec_protocol += ',--policy,authorize_only'
5858
else:
5959
core.log_message("Using XRootD mapfile authentication")

osgtest/tests/test_230_gratia.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,6 @@ def patternreplace(self, infile_name, pattern, full_line):
3030
shutil.move(outfile_name, infile_name)
3131

3232

33-
def tuple_cmp(self, t1, t2):
34-
""" This tuple comparsion method assumes:
35-
A. Tuple has 3 entries
36-
B. An integer comparsion is desired
37-
Note that the python "cmp" method does NOT perform integer comparison
38-
Similar to python "cmp" method,
39-
The return value is negative if t1 < t2, zero if t1 == t2 and strictly positive if t1 > t2."""
40-
41-
t1_0 = int(t1[0])
42-
t1_1 = int(t1[1])
43-
t1_2 = int(t1[2])
44-
45-
t2_0 = int(t2[0])
46-
t2_1 = int(t2[1])
47-
t2_2 = int(t2[2])
48-
49-
if t1_0 < t2_0:
50-
return -1
51-
elif t1_0 > t2_0:
52-
return 1
53-
else: #t1_0 == t2_0
54-
if t1_1 < t2_1:
55-
return -1
56-
elif t1_1 > t2_1:
57-
return 1
58-
else: #t1_1 == t2_1
59-
if t1_2 < t2_2:
60-
return -1
61-
elif t1_2 > t2_2:
62-
return 1
63-
else: #t1_2 == t2_2
64-
return 0
65-
6633
#This test preserves the mentioned gratia directory, if it exists
6734
def test_01_backup_varlibgratia(self):
6835
core.skip_ok_unless_installed('gratia-service')
@@ -85,10 +52,7 @@ def test_03_config_parameters(self):
8552
core.config['gratia.host'] = core.get_hostname()
8653
core.config['gratia.config.dir'] = '/etc/gratia'
8754
# The name of the gratia directory changed
88-
gratia_version = core.get_package_envra('gratia-service')[2]
89-
gratia_version_split = gratia_version.split('.')
90-
91-
if self.tuple_cmp(gratia_version_split, ['1', '13', '5']) < 0:
55+
if core.PackageVersion('gratia-service') < '1.13.5':
9256
core.config['gratia.directory'] = "collector"
9357
else:
9458
core.config['gratia.directory'] = "services"

osgtest/tests/test_410_jobs.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#pylint: disable=R0904
44

55
import os
6-
import rpm
76
import shutil
87
import tempfile
98

@@ -49,11 +48,7 @@ def test_01_condor_run_pbs(self):
4948
# Figure out whether the installed BLAHP package is the same as or later
5049
# than "blahp-1.18.11.bosco-4.osg*" (in the RPM sense), because it's the
5150
# first build in which the job environments are correctly passed to PBS.
52-
# The release following "osg" does not matter and it is easier to ignore
53-
# the OS major version. This code may be a useful starting point for a
54-
# more general library function.
55-
blahp_envra = core.get_package_envra('blahp')
56-
blahp_pbs_has_env_vars = (rpm.labelCompare(['blahp', '1.18.11.bosco', '4.osg'], blahp_envra[1:4]) <= 0)
51+
blahp_pbs_has_env_vars = core.PackageVersion('blahp') >= '1.18.11.bosco-4.osg'
5752

5853
self.run_job_in_tmp_dir(command, 'condor_run a Condor job', verify_environment=blahp_pbs_has_env_vars)
5954

osgtest/tests/test_460_stashcache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_04_xroot_fetch_from_cache(self):
8484

8585
def test_05_stashcp(self):
8686
command = ["stashcp", "-d"]
87-
if core.package_version_compare('stashcache-client', '5.1.0-5') < 0:
87+
if core.PackageVersion('stashcache-client') < '5.1.0-5':
8888
command.append("--cache=root://localhost")
8989
name, contents = self.testfiles[3]
9090
with tempfile.NamedTemporaryFile(mode="r+b") as tf:

osgtest/tests/test_540_gratia.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ def test_02_show_databases(self):
193193
def test_03_show_gratia_database_tables(self):
194194
core.skip_ok_unless_installed('gratia-service')
195195
command = "echo \"use gratia_osgtest;show tables;" + core.config['gratia.sql.querystring'] + "| wc -l",
196-
gratia_version = tuple(map(int, core.get_package_envra('gratia-service')[2].split('.')))
197-
if gratia_version >= (1, 16, 3):
196+
if core.PackageVersion('gratia-service') >= '1.16.3':
198197
expected_table_count = '82'
199198
else:
200199
expected_table_count = '81'
@@ -227,7 +226,7 @@ def test_06_execute_gridftptransfer_probedriver(self):
227226
core.state['gratia.log.stat'] = core.get_stat(core.config['gratia.log.file'])
228227
core.log_message('stat.st_ino is: ' + str(core.state['gratia.log.stat'].st_ino))
229228
core.log_message('stat.st_size is: ' + str(core.state['gratia.log.stat'].st_size))
230-
if core.package_version_compare('gratia-probe-gridftp-transfer', '1.17.0-1') >= 0:
229+
if core.PackageVersion('gratia-probe-gridftp-transfer') >= '1.17.0-1':
231230
probe_script = 'gridftp-transfer_meter'
232231
else:
233232
probe_script = 'GridftpTransferProbeDriver'

0 commit comments

Comments
 (0)