repos-cmp/repos_cmp/colors.py

75 lines
2.1 KiB
Python
Raw Permalink Normal View History

import enum
from repos_cmp import rpm_ffi
class Color(enum.Enum):
'''Color-coding for package pairs.'''
RED = enum.auto()
EXTRA = enum.auto()
NONE = enum.auto()
ORANGE = enum.auto()
GREEN = enum.auto()
AMBER = enum.auto()
YELLOW = enum.auto()
SLATE = enum.auto()
@property
def help(self):
return _DESCRIPTIONS[self]
_DESCRIPTIONS = {
Color.RED: 'target repository does not have this package',
Color.EXTRA: 'package is present in target only',
Color.NONE: 'package is not present in both target and base',
Color.ORANGE: 'normal package, target has an older EVR',
Color.GREEN: 'normal package, target has the current EVR',
Color.AMBER: 'special package, target has an older EV',
Color.YELLOW: 'special package, target has same EV, releases are not compared', # noqa
Color.SLATE: 'the target has newer EVR and package is not YELLOW',
}
_SPECIAL_MARKERS = (b'.port', b'.rv64', b'.mips')
def colorize_pair(base_pkg, target_pkg) -> Color:
# one or both packages are missing
if not base_pkg and not target_pkg:
return Color.NONE
if not base_pkg:
return Color.EXTRA
if not target_pkg:
return Color.RED
if any(x in target_pkg.release for x in _SPECIAL_MARKERS):
# package is special, ignore releases
cmp = rpm_ffi.evr_cmp(
(base_pkg.epoch, base_pkg.version, b'alt0'),
(target_pkg.epoch, target_pkg.version, b'alt0'))
if cmp < 0:
return Color.SLATE
elif cmp > 0:
return Color.AMBER
elif cmp == 0:
return Color.YELLOW
else:
# pacakge is normal
cmp = rpm_ffi.evr_cmp(
(base_pkg.epoch, base_pkg.version, base_pkg.release),
(target_pkg.epoch, target_pkg.version, target_pkg.release))
if cmp < 0:
return Color.SLATE
elif cmp > 0:
return Color.ORANGE
elif cmp == 0:
return Color.GREEN
raise ValueError("Cannot colorize %s and %s" % (base_pkg, target_pkg))