Project

General

Profile

Download (2.87 KB) Statistics
| Branch: | Revision:

root / lib / debian / version.rb @ ff29708e

1
#!/usr/bin/ruby
2

    
3
require 'debian_apt'
4

    
5
module Debian_apt
6
  def self.included(base)
7
    @@op_to_opcode = {}
8
    i = 1
9
    while true 
10
      op = Debian_apt::PkgCache.comp_type_deb(i)
11
      break if op == ""
12
      @@op_to_opcode[op] = i
13
      i += 1
14
    end
15
  end
16

    
17
  def compare_versions(lver, rver)
18
    Debian_apt.debVS.cmp_version(lver, rver)
19
  end
20

    
21
  def check_version_constraint(lver, op, rver)
22
    Debian_apt.debVS.check_dep(lver, @@op_to_opcode[op], rver)
23
  end
24
end
25

    
26
module Debian
27
  class DebianVersion
28
    include Comparable
29
    include Debian_apt
30

    
31
    attr_accessor :epoch, :upstream, :revision
32

    
33
    DEB_TILDE = /~(.*)$/
34

    
35
    def initialize(version = nil)
36
      if version.nil? or version == ""
37
        @epoch = 0
38
        @upstream = nil
39
        @revision = nil
40
      else
41
        @upstream = Debian_apt.debVS.upstream_version(version)
42
        version =~ /^(?:(\d+):)?#{@upstream}(?:-(.+))?$/
43
	@epoch = $1 ? $1 : 0
44
	@revision = $2
45
      end
46
    end
47

    
48
    def version
49
      if @upstream.nil?
50
        ver = nil
51
      else
52
        ver = "#{@upstream}"
53
        # @revision can be nil for native packages
54
        ver += "-#{@revision}" unless @revision.nil?
55
        ver = "#{@epoch}:#{ver}" unless @epoch == 0
56
      end
57
      ver
58
    end
59

    
60
    def to_s
61
      ver = self.version
62
      (ver.nil?) ? "none" : ver
63
    end
64

    
65
    def <=>(rver)
66
      compare_versions(self.version, rver.version)
67
    end
68

    
69
    def is_native?
70
      @revision.nil?
71
    end
72

    
73
    def compare(op, rver)
74
      check_version_constraint(self.version, op, rver.version)
75
    end
76

    
77
    def compare_epochless(op, rver)
78
      v1 = self.dup
79
      v2 = rver.dup
80
      v1.epoch = 0
81
      v2.epoch = 0
82
      check_version_constraint(v1.version, op, v2.version)
83
    end
84

    
85
    def compare_upstream(op, rver, remove_tilde_part = false)
86
      v1 = self.dup
87
      v2 = rver.dup
88
      if remove_tilde_part
89
        v1.remove_upstream_tilde_part
90
        v2.remove_upstream_tilde_part
91
      end
92
      check_version_constraint(v1.upstream, op, v2.upstream)
93
    end
94

    
95
    def compare_revision(op, rver)
96
      check_version_constraint(self.revision, op, rver.revision)
97
    end
98

    
99
    def compare_epoch(op, rver)
100
      check_version_constraint(self.epoch, op, rver.epoch)
101
    end
102

    
103
    def upstream_tilde_part
104
      @upstream =~ DEB_TILDE
105
      $1
106
    end
107

    
108
    def remove_upstream_tilde_part
109
      # trigger a bug with dupped objects
110
      #@upstream.gsub!(DEB_TILDE, "")
111
      @upstream = @upstream.gsub(DEB_TILDE, "")
112
    end
113

    
114
    def set_upstream_tilde_part(str)
115
      remove_upstream_tilde_part
116
      @upstream += "~#{str}"
117
    end
118

    
119
    def revision_tilde_part
120
      @revision =~ DEB_TILDE
121
      $1
122
    end
123

    
124
    def remove_revision_tilde_part
125
      # trigger a bug with dupped objects
126
      #@revision.gsub!(DEB_TILDE, "")
127
      @revision = @revision.gsub(DEB_TILDE, "")
128
    end
129

    
130
    def set_revision_tilde_part(str)
131
      remove_revision_tilde_part
132
      @revision += "~#{str}"
133
    end
134
  end
135
end
136

    
    (1-1/1)