Class: Dependabot::Python::Requirement

Inherits:
Gem::Requirement
  • Object
show all
Defined in:
lib/dependabot/python/requirement.rb

Constant Summary collapse

OR_SEPARATOR =
/(?<=[a-zA-Z0-9*])\s*\|+/.freeze
OPS =

Add equality and arbitrary-equality matchers

OPS.merge(
  "==" => ->(v, r) { v == r },
  "===" => ->(v, r) { v.to_s == r.to_s }
)
PATTERN_RAW =
"\\s*(#{quoted})?\\s*(#{version_pattern})\\s*"
PATTERN =
/\A#{PATTERN_RAW}\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Returns a new instance of Requirement.


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dependabot/python/requirement.rb', line 49

def initialize(*requirements)
  requirements = requirements.flatten.flat_map do |req_string|
    next if req_string.nil?

    req_string.split(",").map do |r|
      convert_python_constraint_to_ruby_constraint(r)
    end
  end

  super(requirements)
end

Class Method Details

.parse(obj) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dependabot/python/requirement.rb', line 24

def self.parse(obj)
  return ["=", Python::Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

  unless (matches = PATTERN.match(obj.to_s))
    msg = "Illformed requirement [#{obj.inspect}]"
    raise BadRequirementError, msg
  end

  return DefaultRequirement if matches[1] == ">=" && matches[2] == "0"

  [matches[1] || "=", Python::Version.new(matches[2])]
end

.requirements_array(requirement_string) ⇒ Object

Returns an array of requirements. At least one requirement from the returned array must be satisfied for a version to be valid.

NOTE: Or requirements are only valid for Poetry.


41
42
43
44
45
46
47
# File 'lib/dependabot/python/requirement.rb', line 41

def self.requirements_array(requirement_string)
  return [new(nil)] if requirement_string.nil?

  requirement_string.strip.split(OR_SEPARATOR).map do |req_string|
    new(req_string.strip)
  end
end

Instance Method Details

#exact?Boolean

Returns:

  • (Boolean)

67
68
69
70
71
# File 'lib/dependabot/python/requirement.rb', line 67

def exact?
  return false unless @requirements.size == 1

  %w(= == ===).include?(@requirements[0][0])
end

#satisfied_by?(version) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
64
65
# File 'lib/dependabot/python/requirement.rb', line 61

def satisfied_by?(version)
  version = Python::Version.new(version.to_s)

  requirements.all? { |op, rv| (OPS[op] || OPS["="]).call(version, rv) }
end