Project

General

Profile

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

root / lib / debian / version.rb @ 8b5fca79

1
#!/usr/bin/ruby
2

    
3
#--
4
# Ruby-Debian, a Ruby interface for Debian packaging aspects
5
# Copyright (c) 2009 Marc Dequènes (Duck) <Duck@DuckCorp.org>
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
#++
20

    
21
require 'debian_apt'
22

    
23
module Debian_apt
24
  def self.included(base)
25
    @@op_to_opcode = {}
26
    i = 1
27
    while true 
28
      op = Debian_apt::PkgCache.comp_type_deb(i)
29
      break if op == ""
30
      @@op_to_opcode[op] = i
31
      i += 1
32
    end
33
  end
34

    
35
  def compare_versions(lver, rver)
36
    Debian_apt.debVS.cmp_version(lver.to_s, rver.to_s)
37
  end
38

    
39
  def check_version_constraint(lver, op, rver)
40
    Debian_apt.debVS.check_dep(lver.to_s, @@op_to_opcode[op], rver.to_s)
41
  end
42
end
43

    
44
module Debian
45
  class DebianVersion
46
    include Comparable
47
    include Debian_apt
48

    
49
    attr_accessor :epoch, :upstream, :revision
50

    
51
    DEB_TILDE = /~(.*)$/
52

    
53
    def initialize(version = nil)
54
      if version.nil? or version == ""
55
        @epoch = 0
56
        @upstream = nil
57
        @revision = nil
58
      else
59
        @upstream = Debian_apt.debVS.upstream_version(version)
60
	if @upstream == ""
61
          @epoch = 0
62
          @upstream = nil
63
          @revision = nil
64
	else
65
	  @epoch, @revision = version.split(@upstream, 2)
66
	  @epoch = @epoch ? @epoch[0..-2].to_i : 0
67
	  @revision = @revision[1..-1] if @revision
68
	end
69

    
70
        # magic fence
71
        raise "DebianVersion bug in parsing. Stopping before breaking the world ! (given version was: '#{version}', result was: '#{self.version}')" if self.version != version
72
      end
73
    end
74

    
75
    def version
76
      if @upstream.nil? or @upstream == ""
77
        ver = nil
78
      else
79
        ver = "#{@upstream}"
80
        # @revision can be nil for native packages
81
        ver += "-#{@revision}" unless @revision.nil?
82
        ver = "#{@epoch}:#{ver}" unless @epoch == 0
83
      end
84
      ver
85
    end
86

    
87
    def to_s
88
      ver = self.version
89
      (ver.nil?) ? "none" : ver
90
    end
91

    
92
    def <=>(rver)
93
      compare_versions(self.version, rver.version)
94
    end
95

    
96
    def is_native?
97
      @revision.nil?
98
    end
99

    
100
    def compare(op, rver)
101
      check_version_constraint(self.version, op, rver.version)
102
    end
103

    
104
    def compare_epochless(op, rver)
105
      v1 = self.dup
106
      v2 = rver.dup
107
      v1.epoch = 0
108
      v2.epoch = 0
109
      check_version_constraint(v1.version, op, v2.version)
110
    end
111

    
112
    def compare_upstream(op, rver, remove_tilde_part = false)
113
      v1 = self.dup
114
      v2 = rver.dup
115
      if remove_tilde_part
116
        v1.remove_upstream_tilde_part
117
        v2.remove_upstream_tilde_part
118
      end
119
      check_version_constraint(v1.upstream, op, v2.upstream)
120
    end
121

    
122
    def compare_revision(op, rver)
123
      check_version_constraint(self.revision, op, rver.revision)
124
    end
125

    
126
    def compare_epoch(op, rver)
127
      check_version_constraint(self.epoch, op, rver.epoch)
128
    end
129

    
130
    def upstream_tilde_part
131
      @upstream =~ DEB_TILDE
132
      $1
133
    end
134

    
135
    def remove_upstream_tilde_part
136
      # trigger a bug with dupped objects
137
      #@upstream.gsub!(DEB_TILDE, "")
138
      @upstream = @upstream.gsub(DEB_TILDE, "")
139
    end
140

    
141
    def set_upstream_tilde_part(str)
142
      remove_upstream_tilde_part
143
      @upstream += "~#{str}"
144
    end
145

    
146
    def revision_tilde_part
147
      @revision =~ DEB_TILDE
148
      $1
149
    end
150

    
151
    def remove_revision_tilde_part
152
      # trigger a bug with dupped objects
153
      #@revision.gsub!(DEB_TILDE, "")
154
      @revision = @revision.gsub(DEB_TILDE, "")
155
    end
156

    
157
    def set_revision_tilde_part(str)
158
      remove_revision_tilde_part
159
      @revision += "~#{str}"
160
    end
161
  end
162
end
163

    
    (1-1/1)