~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/rubygems/requirement.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#--
2
 
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3
 
# All rights reserved.
4
 
# See LICENSE.txt for permissions.
5
 
#++
6
 
 
7
 
require 'rubygems/version'
 
1
require "rubygems/version"
8
2
 
9
3
##
10
 
# Requirement version includes a prefaced comparator in addition
11
 
# to a version number.
12
 
#
13
 
# A Requirement object can actually contain multiple, er,
14
 
# requirements, as in (> 1.2, < 2.0).
 
4
# A Requirement is a set of one or more version restrictions. It supports a
 
5
# few (<tt>=, !=, >, <, >=, <=, ~></tt>) different restriction operators.
15
6
 
16
7
class Gem::Requirement
17
 
 
18
8
  include Comparable
19
9
 
20
 
  attr_reader :requirements
21
 
 
22
 
  OPS = {
 
10
  OPS = { #:nodoc:
23
11
    "="  =>  lambda { |v, r| v == r },
24
12
    "!=" =>  lambda { |v, r| v != r },
25
 
    ">"  =>  lambda { |v, r| v > r },
26
 
    "<"  =>  lambda { |v, r| v < r },
 
13
    ">"  =>  lambda { |v, r| v > r  },
 
14
    "<"  =>  lambda { |v, r| v < r  },
27
15
    ">=" =>  lambda { |v, r| v >= r },
28
16
    "<=" =>  lambda { |v, r| v <= r },
29
 
    "~>" =>  lambda { |v, r| v >= r && v < r.bump }
 
17
    "~>" =>  lambda { |v, r| v = v.release; v >= r && v < r.bump }
30
18
  }
31
19
 
32
 
  OP_RE = /#{OPS.keys.map{ |k| Regexp.quote k }.join '|'}/o
 
20
  quoted  = OPS.keys.map { |k| Regexp.quote k }.join "|"
 
21
  PATTERN = /\A\s*(#{quoted})?\s*(#{Gem::Version::VERSION_PATTERN})\s*\z/
33
22
 
34
23
  ##
35
 
  # Factory method to create a Gem::Requirement object.  Input may be a
36
 
  # Version, a String, or nil.  Intended to simplify client code.
 
24
  # Factory method to create a Gem::Requirement object.  Input may be
 
25
  # a Version, a String, or nil.  Intended to simplify client code.
37
26
  #
38
 
  # If the input is "weird", the default version requirement is returned.
 
27
  # If the input is "weird", the default version requirement is
 
28
  # returned.
39
29
 
40
 
  def self.create(input)
 
30
  def self.create input
41
31
    case input
42
32
    when Gem::Requirement then
43
33
      input
45
35
      new input
46
36
    else
47
37
      if input.respond_to? :to_str then
48
 
        self.new [input.to_str]
 
38
        new [input.to_str]
49
39
      else
50
 
        self.default
 
40
        default
51
41
      end
52
42
    end
53
43
  end
60
50
  # "A default "version requirement" can surely _only_ be '> 0'."
61
51
 
62
52
  def self.default
63
 
    self.new ['>= 0']
64
 
  end
65
 
 
66
 
  ##
67
 
  # Constructs a Requirement from +requirements+ which can be a String, a
68
 
  # Gem::Version, or an Array of those.  See parse for details on the
69
 
  # formatting of requirement strings.
70
 
 
71
 
  def initialize(requirements)
72
 
    @requirements = case requirements
73
 
                    when Array then
74
 
                      requirements.map do |requirement|
75
 
                        parse(requirement)
76
 
                      end
77
 
                    else
78
 
                      [parse(requirements)]
79
 
                    end
80
 
    @version = nil   # Avoid warnings.
81
 
  end
82
 
 
83
 
  ##
84
 
  # Marshal raw requirements, rather than the full object
 
53
    new '>= 0'
 
54
  end
 
55
 
 
56
  ##
 
57
  # Parse +obj+, returning an <tt>[op, version]</tt> pair. +obj+ can
 
58
  # be a String or a Gem::Version.
 
59
  #
 
60
  # If +obj+ is a String, it can be either a full requirement
 
61
  # specification, like <tt>">= 1.2"</tt>, or a simple version number,
 
62
  # like <tt>"1.2"</tt>.
 
63
  #
 
64
  #     parse("> 1.0")                 # => [">", "1.0"]
 
65
  #     parse("1.0")                   # => ["=", "1.0"]
 
66
  #     parse(Gem::Version.new("1.0")) # => ["=,  "1.0"]
 
67
 
 
68
  def self.parse obj
 
69
    return ["=", obj] if Gem::Version === obj
 
70
 
 
71
    unless PATTERN =~ obj.to_s
 
72
      raise ArgumentError, "Illformed requirement [#{obj.inspect}]"
 
73
    end
 
74
 
 
75
    [$1 || "=", Gem::Version.new($2)]
 
76
  end
 
77
 
 
78
  ##
 
79
  # An array of requirement pairs. The first element of the pair is
 
80
  # the op, and the second is the Gem::Version.
 
81
 
 
82
  attr_reader :requirements #:nodoc:
 
83
 
 
84
  ##
 
85
  # Constructs a requirement from +requirements+. Requirements can be
 
86
  # Strings, Gem::Versions, or Arrays of those. +nil+ and duplicate
 
87
  # requirements are ignored. An empty set of +requirements+ is the
 
88
  # same as <tt>">= 0"</tt>.
 
89
 
 
90
  def initialize *requirements
 
91
    requirements = requirements.flatten
 
92
    requirements.compact!
 
93
    requirements.uniq!
 
94
 
 
95
    requirements << ">= 0" if requirements.empty?
 
96
    @none = (requirements == ">= 0")
 
97
    @requirements = requirements.map! { |r| self.class.parse r }
 
98
  end
 
99
 
 
100
  def none?
 
101
    @none ||= (to_s == ">= 0")
 
102
  end
 
103
 
 
104
  def as_list # :nodoc:
 
105
    requirements.map { |op, version| "#{op} #{version}" }
 
106
  end
 
107
 
 
108
  def hash # :nodoc:
 
109
    requirements.hash
 
110
  end
85
111
 
86
112
  def marshal_dump # :nodoc:
87
113
    [@requirements]
88
114
  end
89
115
 
90
 
  ##
91
 
  # Load custom marshal format
92
 
 
93
 
  def marshal_load(array) # :nodoc:
 
116
  def marshal_load array # :nodoc:
94
117
    @requirements = array[0]
95
 
    @version = nil
 
118
  end
 
119
 
 
120
  def prerelease?
 
121
    requirements.any? { |r| r.last.prerelease? }
 
122
  end
 
123
 
 
124
  def pretty_print q # :nodoc:
 
125
    q.group 1, 'Gem::Requirement.new(', ')' do
 
126
      q.pp as_list
 
127
    end
 
128
  end
 
129
 
 
130
  ##
 
131
  # True if +version+ satisfies this Requirement.
 
132
 
 
133
  def satisfied_by? version
 
134
    requirements.all? { |op, rv| OPS[op].call version, rv }
96
135
  end
97
136
 
98
137
  def to_s # :nodoc:
99
 
    as_list.join(", ")
100
 
  end
101
 
 
102
 
  def as_list
103
 
    normalize
104
 
    @requirements.collect { |req|
105
 
      "#{req[0]} #{req[1]}"
106
 
    }
107
 
  end
108
 
 
109
 
  def normalize
110
 
    return if not defined? @version or @version.nil?
111
 
    @requirements = [parse(@version)]
112
 
    @nums = nil
113
 
    @version = nil
114
 
    @op = nil
115
 
  end
116
 
 
117
 
  ##
118
 
  # True if this requirement satisfied by the Gem::Version +version+.
119
 
 
120
 
  def satisfied_by?(version)
121
 
    normalize
122
 
    @requirements.all? { |op, rv| satisfy?(op, version, rv) }
123
 
  end
124
 
 
125
 
  ##
126
 
  # Is "+version+ +op+ +required_version+" satisfied?
127
 
 
128
 
  def satisfy?(op, version, required_version)
129
 
    OPS[op].call(version, required_version)
130
 
  end
131
 
 
132
 
  ##
133
 
  # Parse the version requirement obj returning the operator and version.
134
 
  #
135
 
  # The requirement can be a String or a Gem::Version.  A String can be an
136
 
  # operator (<, <=, =, =>, >, !=, ~>), a version number, or both, operator
137
 
  # first.
138
 
 
139
 
  def parse(obj)
140
 
    case obj
141
 
    when /^\s*(#{OP_RE})\s*([0-9.]+)\s*$/o then
142
 
      [$1, Gem::Version.new($2)]
143
 
    when /^\s*([0-9.]+)\s*$/ then
144
 
      ['=', Gem::Version.new($1)]
145
 
    when /^\s*(#{OP_RE})\s*$/o then
146
 
      [$1, Gem::Version.new('0')]
147
 
    when Gem::Version then
148
 
      ['=', obj]
149
 
    else
150
 
      fail ArgumentError, "Illformed requirement [#{obj.inspect}]"
151
 
    end
152
 
  end
153
 
 
154
 
  def <=>(other) # :nodoc:
 
138
    as_list.join ", "
 
139
  end
 
140
 
 
141
  def <=> other # :nodoc:
155
142
    to_s <=> other.to_s
156
143
  end
157
 
 
158
 
  def hash # :nodoc:
159
 
    to_s.hash
160
 
  end
161
 
 
162
144
end
163
145
 
 
146
# :stopdoc:
 
147
# Gem::Version::Requirement is used in a lot of old YAML specs. It's aliased
 
148
# here for backwards compatibility. I'd like to remove this, maybe in RubyGems
 
149
# 2.0.
 
150
 
 
151
::Gem::Version::Requirement = ::Gem::Requirement
 
152
# :startdoc:
 
153