Class: Dependabot::Python::FileUpdater::RequirementReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/python/file_updater/requirement_replacer.rb

Constant Summary collapse

PACKAGE_NOT_FOUND_ERROR =
"PackageNotFoundError"

Instance Method Summary collapse

Constructor Details

#initialize(content:, dependency_name:, old_requirement:, new_requirement:, new_hash_version: nil, index_urls: nil) ⇒ RequirementReplacer

Returns a new instance of RequirementReplacer.



17
18
19
20
21
22
23
24
25
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 17

def initialize(content:, dependency_name:, old_requirement:,
               new_requirement:, new_hash_version: nil, index_urls: nil)
  @content          = content
  @dependency_name  = normalise(dependency_name)
  @old_requirement  = old_requirement
  @new_requirement  = new_requirement
  @new_hash_version = new_hash_version
  @index_urls = index_urls
end

Instance Method Details

#updated_contentObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 27

def updated_content
  updated_content =
    content.gsub(original_declaration_replacement_regex) do |mtch|
      # If the "declaration" is setting an option (e.g., no-binary)
      # ignore it, since it isn't actually a declaration
      next mtch if Regexp.last_match&.pre_match&.match?(/--.*\z/)

      updated_dependency_declaration_string
    end

  raise "Expected content to change!" if old_requirement != new_requirement && content == updated_content

  updated_content
end