def __cmp__(self, other): if self.version is None or other.version is None: # cannot compare None version raise Exception("Unable to compare None versions") try: sv = StrictVersion() sv.parse(self.version) return sv.__cmp__(other.version) except Exception: return LooseVersion(self.version).__cmp__(LooseVersion(other.version))
def __cmp__(self, other): has_esr = set() if isinstance( other, LooseModernMozillaVersion) and str(other).endswith('esr'): # If other version ends with esr, coerce through MozillaVersion ending up with # a StrictVersion if possible has_esr.add('other') other = MozillaVersion( str(other)[:-3]) # strip ESR from end of string if isinstance(self, LooseModernMozillaVersion) and str(self).endswith('esr'): # If our version ends with esr, coerce through MozillaVersion ending up with # a StrictVersion if possible has_esr.add('self') self = MozillaVersion( str(self)[:-3]) # strip ESR from end of string if isinstance(other, LooseModernMozillaVersion) or \ isinstance(self, LooseModernMozillaVersion): # If we're still LooseVersion for self or other, run LooseVersion compare # Being sure to pass through Loose Version type first val = LooseVersion.__cmp__(LooseModernMozillaVersion(str(self)), LooseModernMozillaVersion(str(other))) else: # No versions are loose, therefore we can use StrictVersion val = StrictVersion.__cmp__(self, other) if has_esr.isdisjoint(set(['other', 'self'])) or \ has_esr.issuperset(set(['other', 'self'])): # If both had esr string or neither, then cmp() was accurate return val elif val is not 0: # cmp is accurate here even if esr is present in only 1 compare, since # versions are not equal return val elif 'other' in has_esr: return -1 # esr is not greater than non esr return 1 # non esr is greater than esr