Project

General

Profile

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

root / lib / debian / version.rb @ ce4364e3

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
	@epoch, @revision = version.split(@upstream, 2)
61
	@epoch = @epoch ? @epoch[0..-2].to_i : 0
62
	@revision = @revision[1..-1] if @revision
63

    
64
        # magic fence
65
        raise "DebianVersion bug in parsing. Stopping before breaking the world !" if self.version != version
66
      end
67
    end
68

    
69
    def version
70
      if @upstream.nil?
71
        ver = nil
72
      else
73
        ver = "#{@upstream}"
74
        # @revision can be nil for native packages
75
        ver += "-#{@revision}" unless @revision.nil?
76
        ver = "#{@epoch}:#{ver}" unless @epoch == 0
77
      end
78
      ver
79
    end
80

    
81
    def to_s
82
      ver = self.version
83
      (ver.nil?) ? "none" : ver
84
    end
85

    
86
    def <=>(rver)
87
      compare_versions(self.version, rver.version)
88
    end
89

    
90
    def is_native?
91
      @revision.nil?
92
    end
93

    
94
    def compare(op, rver)
95
      check_version_constraint(self.version, op, rver.version)
96
    end
97

    
98
    def compare_epochless(op, rver)
99
      v1 = self.dup
100
      v2 = rver.dup
101
      v1.epoch = 0
102
      v2.epoch = 0
103
      check_version_constraint(v1.version, op, v2.version)
104
    end
105

    
106
    def compare_upstream(op, rver, remove_tilde_part = false)
107
      v1 = self.dup
108
      v2 = rver.dup
109
      if remove_tilde_part
110
        v1.remove_upstream_tilde_part
111
        v2.remove_upstream_tilde_part
112
      end
113
      check_version_constraint(v1.upstream, op, v2.upstream)
114
    end
115

    
116
    def compare_revision(op, rver)
117
      check_version_constraint(self.revision, op, rver.revision)
118
    end
119

    
120
    def compare_epoch(op, rver)
121
      check_version_constraint(self.epoch, op, rver.epoch)
122
    end
123

    
124
    def upstream_tilde_part
125
      @upstream =~ DEB_TILDE
126
      $1
127
    end
128

    
129
    def remove_upstream_tilde_part
130
      # trigger a bug with dupped objects
131
      #@upstream.gsub!(DEB_TILDE, "")
132
      @upstream = @upstream.gsub(DEB_TILDE, "")
133
    end
134

    
135
    def set_upstream_tilde_part(str)
136
      remove_upstream_tilde_part
137
      @upstream += "~#{str}"
138
    end
139

    
140
    def revision_tilde_part
141
      @revision =~ DEB_TILDE
142
      $1
143
    end
144

    
145
    def remove_revision_tilde_part
146
      # trigger a bug with dupped objects
147
      #@revision.gsub!(DEB_TILDE, "")
148
      @revision = @revision.gsub(DEB_TILDE, "")
149
    end
150

    
151
    def set_revision_tilde_part(str)
152
      remove_revision_tilde_part
153
      @revision += "~#{str}"
154
    end
155
  end
156
end
157

    
    (1-1/1)