Class: Dependabot::Python::Requirement

Inherits:
Requirement
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/python/requirement.rb

Constant Summary collapse

OR_SEPARATOR =
/(?<=[a-zA-Z0-9)*])\s*\|+/
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*".freeze
PATTERN =
/\A#{PATTERN_RAW}\z/
PARENS_PATTERN =
/\A\(([^)]+)\)\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Returns a new instance of Requirement.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dependabot/python/requirement.rb', line 66

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

    # Standard python doesn't support whitespace in requirements, but Poetry does.
    req_string = req_string.gsub(/(\d +)([<=>])/, '\1,\2')

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

  super(requirements)
end

Class Method Details

.parse(obj) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dependabot/python/requirement.rb', line 31

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

  line = obj.to_s
  if (matches = PARENS_PATTERN.match(line))
    line = matches[1]
  end

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

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

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

.requirements_array(requirement_string) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dependabot/python/requirement.rb', line 54

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

  if (matches = PARENS_PATTERN.match(requirement_string))
    requirement_string = matches[1]
  end

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

Instance Method Details

#exact?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/dependabot/python/requirement.rb', line 87

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

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

#satisfied_by?(version) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/dependabot/python/requirement.rb', line 81

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

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