+class SlesKVersion implements Comparable<SlesKVersion> {
+ Integer major = 0
+ Integer minor = 0
+ Integer patch = 0
+ Integer slesrelease = 0
+ Integer slesmajor = 0
+ Integer slesminor = 0
+
+ SlesKVersion() {}
+
+ SlesKVersion(version) {
+ this.parse(version)
+ }
+
+ static SlesKVersion minKVersion() {
+ return new SlesKVersion("rpm-0.0.0-0.0.0")
+ }
+
+ static SlesKVersion maxKVersion() {
+ return new SlesKVersion("rpm-" + Integer.MAX_VALUE + ".0.0-0.0.0")
+ }
+
+ static SlesKVersion factory(version) {
+ return new SlesKVersion(version)
+ }
+
+ def parse(version) {
+ this.major = 0
+ this.minor = 0
+ this.patch = 0
+ this.slesrelease = 0
+ this.slesmajor = 0
+ this.slesminor = 0
+
+ if (!version) {
+ throw new EmptyKVersionException("Empty kernel version")
+ }
+
+ // Eg. 5.14.21-150400.22
+ // Eg. 5.14.21-150400.24.100.2
+ // From tag: rpm-5.14.21-150400.24.100 -> 5.14.21-150400.24.100
+ def match = version =~ /^(rpm-)??(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)(\.\d+)??$/
+ if (!match) {
+ throw new InvalidKVersionException("Invalid kernel version: ${version}")
+ }
+
+ this.major = Integer.parseInt(match.group(2))
+ this.minor = Integer.parseInt(match.group(3))
+ this.patch = Integer.parseInt(match.group(4))
+ this.slesrelease = Integer.parseInt(match.group(5))
+ this.slesmajor = Integer.parseInt(match.group(6))
+
+ if (match.group(7) != null) {
+ this.slesminor = Integer.parseInt(match.group(7).drop(1))
+ }
+ }
+
+ Boolean isRC() {
+ return false
+ }
+
+ Boolean isSameStable(SlesKVersion o) {
+ if (this.major != o.major) {
+ return false
+ }
+ if (this.minor != o.minor) {
+ return false
+ }
+ if (this.patch != o.patch) {
+ return false
+ }
+ if (this.slesrelease != o.slesrelease) {
+ return false
+ }
+ if (this.slesmajor != o.slesmajor) {
+ return false
+ }
+ if (this.slesminor != o.slesminor) {
+ return false
+ }
+ return true
+ }
+
+ @Override int compareTo(SlesKVersion o) {
+ if (this.major != o.major) {
+ return Integer.compare(this.major, o.major)
+ }
+ if (this.minor != o.minor) {
+ return Integer.compare(this.minor, o.minor)
+ }
+ if (this.patch != o.patch) {
+ return Integer.compare(this.patch, o.patch)
+ }
+ if (this.slesrelease != o.slesrelease) {
+ return Integer.compare(this.slesrelease, o.slesrelease)
+ }
+ if (this.slesmajor != o.slesmajor) {
+ return Integer.compare(this.slesmajor, o.slesmajor)
+ }
+ if (this.slesminor != o.slesminor) {
+ return Integer.compare(this.slesminor, o.slesminor)
+ }
+ return 0
+ }
+
+ String toString() {
+ String vString = "rpm-${this.major}.${this.minor}.${patch}-${this.slesrelease}.${this.slesmajor}"
+ if (this.slesminor != 0) {
+ vString = vString.concat(".${this.slesminor}")
+ }
+ return vString
+ }
+}
+
+class UbuntuKVersion implements Comparable<UbuntuKVersion> {
+
+ Integer major = 0
+ Integer minor = 0
+ Integer patch = 0
+ Integer umajor = 0
+ Integer uminor = 0
+ String suffix = ""
+ String strLTS = ""
+ Boolean isLTS = false
+
+ UbuntuKVersion() {}
+
+ UbuntuKVersion(version) {
+ this.parse(version)
+ }
+
+ static UbuntuKVersion minKVersion() {
+ return new UbuntuKVersion("Ubuntu-lts-0.0.0-0.0")
+ }
+
+ static UbuntuKVersion maxKVersion() {
+ return new UbuntuKVersion("Ubuntu-" + Integer.MAX_VALUE + ".0.0-0.0")
+ }
+
+ static UbuntuKVersion factory(version) {
+ return new UbuntuKVersion(version)
+ }
+
+ def parse(version) {
+ this.major = 0
+ this.minor = 0
+ this.patch = 0
+ this.umajor = 0
+ this.uminor = 0
+ this.suffix = "";
+ this.isLTS = false
+
+ if (!version) {
+ throw new EmptyKVersionException("Empty kernel version")
+ }
+
+ //'Ubuntu-hwe-5.8-5.8.0-19.20_20.04.3',
+ //'Ubuntu-lts-4.8.0-27.29_16.04.1',
+ //'Ubuntu-4.4.0-70.91',
+ def match = version =~ /^Ubuntu-(lts-|hwe-)??(?:\d+\.\d+-)??(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)(.*)??$/
+ if (!match) {
+ throw new InvalidKVersionException("Invalid kernel version: ${version}")
+ }
+
+ if (match.group(1) != null) {
+ this.isLTS = true
+ this.strLTS = match.group(1)
+ }
+
+ // Major
+ this.major = Integer.parseInt(match.group(2))
+
+ // Minor
+ this.minor = Integer.parseInt(match.group(3))
+
+ // Patch level
+ this.patch = Integer.parseInt(match.group(4))
+
+ // Ubuntu major
+ this.umajor = Integer.parseInt(match.group(5))
+
+ // Ubuntu minor
+ this.uminor = Integer.parseInt(match.group(6))
+
+ if (match.group(7) != null) {
+ this.suffix = match.group(7)
+ }
+ }
+
+ // Return true if this version is a release candidate
+ Boolean isRC() {
+ return false
+ }
+
+ // Return true if both version are of the same stable branch
+ Boolean isSameStable(UbuntuKVersion o) {
+ if (this.isLTS != o.isLTS) {
+ return false
+ }
+ if (this.major != o.major) {
+ return false
+ }
+ if (this.minor != o.minor) {
+ return false
+ }
+ if (this.patch != o.patch) {
+ return false
+ }
+
+ return true
+ }
+
+ @Override int compareTo(UbuntuKVersion o) {
+ if (this.major != o.major) {
+ return Integer.compare(this.major, o.major)
+ }
+ if (this.minor != o.minor) {
+ return Integer.compare(this.minor, o.minor)
+ }
+ if (this.patch != o.patch) {
+ return Integer.compare(this.patch, o.patch)
+ }
+ if (this.umajor != o.umajor) {
+ return Integer.compare(this.umajor, o.umajor)
+ }
+ if (this.uminor != o.uminor) {
+ return Integer.compare(this.uminor, o.uminor)
+ }
+ if (this.isLTS != o.isLTS) {
+ if (o.isLTS) {
+ return 1
+ } else {
+ return -1
+ }
+ }
+
+ // Same version
+ return 0;
+ }
+
+ String toString() {
+ String vString = "Ubuntu-"
+
+ if (this.isLTS) {
+ vString = vString.concat("${this.strLTS}")
+ }
+
+ // The tag pattern changed for HWE kernels >= 5.0
+ if (this.isLTS && this.major >= 5) {
+ vString = vString.concat("${this.major}.${this.minor}-${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
+ } else {
+ vString = vString.concat("${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
+ }
+
+ return vString
+ }
+}
+