Project

General

Profile

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

root / lib / debian / version.rb @ bc82ca34

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, rver)
37
  end
38

    
39
  def check_version_constraint(lver, op, rver)
40
    Debian_apt.debVS.check_dep(lver, @@op_to_opcode[op], rver)
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
        version =~ /^(?:(\d+):)?#{@upstream}(?:-(.+))?$/
61
	@epoch = $1 ? $1 : 0
62
	@revision = $2
63
      end
64
    end
65

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

    
78
    def to_s
79
      ver = self.version
80
      (ver.nil?) ? "none" : ver
81
    end
82

    
83
    def <=>(rver)
84
      compare_versions(self.version, rver.version)
85
    end
86

    
87
    def is_native?
88
      @revision.nil?
89
    end
90

    
91
    def compare(op, rver)
92
      check_version_constraint(self.version, op, rver.version)
93
    end
94

    
95
    def compare_epochless(op, rver)
96
      v1 = self.dup
97
      v2 = rver.dup
98
      v1.epoch = 0
99
      v2.epoch = 0
100
      check_version_constraint(v1.version, op, v2.version)
101
    end
102

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

    
113
    def compare_revision(op, rver)
114
      check_version_constraint(self.revision, op, rver.revision)
115
    end
116

    
117
    def compare_epoch(op, rver)
118
      check_version_constraint(self.epoch, op, rver.epoch)
119
    end
120

    
121
    def upstream_tilde_part
122
      @upstream =~ DEB_TILDE
123
      $1
124
    end
125

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

    
132
    def set_upstream_tilde_part(str)
133
      remove_upstream_tilde_part
134
      @upstream += "~#{str}"
135
    end
136

    
137
    def revision_tilde_part
138
      @revision =~ DEB_TILDE
139
      $1
140
    end
141

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

    
148
    def set_revision_tilde_part(str)
149
      remove_revision_tilde_part
150
      @revision += "~#{str}"
151
    end
152
  end
153
end
154

    
    (1-1/1)