Class: DebVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/deb_version.rb,
lib/deb_version/compare.rb,
lib/deb_version/version.rb

Overview

Debian Version comparison class This is based on the Python deb-pkg-tools implementation github.com/xolox/python-deb-pkg-tools

Defined Under Namespace

Classes: Error

Constant Summary collapse

PUNCTUATION =

String of ASCII characters which are considered punctuation characters in the C locale: Except for ~ Already sorted

"!\"\#$%&'()*+,-./:;<=>?@[]^_`{|}".chars
DIGITS =
("0".."9").to_a
SORT_LIST =

Sorted list of characters used by Debian Version sort. see www.debian.org/doc/debian-policy/ch-controlfields.html#version

["~", ""] + ("A".."Z").to_a + ("a".."z").to_a + PUNCTUATION
ORDER_MAPPING =

Set it to a constant

mapping
VERSION =
"1.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ DebVersion

Returns a new instance of DebVersion.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/deb_version/compare.rb', line 11

def initialize(version)
  @version = version
  @epoch = 0
  @debian_revision = ""

  if @version.include? ":"
    @epoch, @version = @version.split(":")
    @epoch = @epoch.to_i
  end

  if @version.include? "-"
    @upstream_version, _, @debian_revision = @version.rpartition("-")
  else
    @upstream_version = @version
  end
end

Instance Attribute Details

#debian_revisionObject (readonly)

Returns the value of attribute debian_revision.



7
8
9
# File 'lib/deb_version/compare.rb', line 7

def debian_revision
  @debian_revision
end

#epochObject (readonly)

Returns the value of attribute epoch.



7
8
9
# File 'lib/deb_version/compare.rb', line 7

def epoch
  @epoch
end

#upstream_versionObject (readonly)

Returns the value of attribute upstream_version.



7
8
9
# File 'lib/deb_version/compare.rb', line 7

def upstream_version
  @upstream_version
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two versions using all 3 parts



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/deb_version/compare.rb', line 95

def <=>(other)
  return epoch <=> other.epoch if epoch != other.epoch

  result = compare_strings(upstream_version, other.upstream_version)

  return result if result != 0

  return compare_strings(debian_revision, other.debian_revision) if debian_revision || other.debian_revision

  0
end

#compare_strings(version1, version2) ⇒ Object

Compare strings as per www.debian.org/doc/debian-policy/ch-controlfields.html#version Section 5.6.12 This is used to compare upstream_version as well as debian_revision rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/deb_version/compare.rb', line 60

def compare_strings(version1, version2)
  v1 = version1.chars
  v2 = version2.chars
  while !v1.empty? || !v2.empty?
    p1 = get_non_digit_prefix(v1)
    p2 = get_non_digit_prefix(v2)
    if p1 != p2
      loop do
        c1 = p1.shift
        c2 = p2.shift
        break if c1.nil? && c2.nil?

        o1 = DebVersion::ORDER_MAPPING.fetch(c1 || "")
        o2 = DebVersion::ORDER_MAPPING.fetch(c2 || "")
        if o1 < o2
          return -1
        elsif o1 > o2
          return 1
        end
      end
    end
    d1 = get_digit_prefix(v1)
    d2 = get_digit_prefix(v2)
    if d1 < d2
      return -1
    elsif d1 > d2
      return 1
    end
  end
  0
end

#get_digit_prefix(characters) ⇒ Object

Internal method to get the largest digit prefix



43
44
45
46
47
# File 'lib/deb_version/compare.rb', line 43

def get_digit_prefix(characters)
  value = 0
  value = (value * 10) + characters.shift.to_i while characters && DebVersion::DIGITS.include?(characters[0])
  value
end

#get_non_digit_prefix(characters) ⇒ Object

Internal method to get the largest non-digit prefix



50
51
52
53
54
# File 'lib/deb_version/compare.rb', line 50

def get_non_digit_prefix(characters)
  prefix = []
  prefix << characters.shift while characters.size.positive? && !DebVersion::DIGITS.include?(characters[0])
  prefix
end

#to_aObject

Convert to an array of [epoch, upstream_version, debian_revision] Helpful for printing and debugging



30
31
32
# File 'lib/deb_version/compare.rb', line 30

def to_a
  [@epoch, @upstream_version, @debian_revision]
end

#to_sObject



34
35
36
37
38
39
40
# File 'lib/deb_version/compare.rb', line 34

def to_s
  result = ""
  result << "#{@epoch}:" if @epoch != 0
  result << @upstream_version
  result << "-#{@debian_revision}" unless @debian_revision.empty?
  result
end