Class: Dependabot::Python::FileUpdater::PyprojectPreparer

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

Instance Method Summary collapse

Constructor Details

#initialize(pyproject_content:, lockfile: nil) ⇒ PyprojectPreparer

Returns a new instance of PyprojectPreparer.


16
17
18
19
# File 'lib/dependabot/python/file_updater/pyproject_preparer.rb', line 16

def initialize(pyproject_content:, lockfile: nil)
  @pyproject_content = pyproject_content
  @lockfile = lockfile
end

Instance Method Details

#freeze_top_level_dependencies_except(dependencies) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dependabot/python/file_updater/pyproject_preparer.rb', line 39

def freeze_top_level_dependencies_except(dependencies)
  return pyproject_content unless lockfile

  pyproject_object = TomlRB.parse(pyproject_content)
  poetry_object = pyproject_object["tool"]["poetry"]
  excluded_names = dependencies.map(&:name) + ["python"]

  %w(dependencies dev-dependencies).each do |key|
    next unless poetry_object[key]

    poetry_object.fetch(key).each do |dep_name, _|
      next if excluded_names.include?(normalise(dep_name))

      locked_details = locked_details(dep_name)

      next unless (locked_version = locked_details&.fetch("version"))

      if locked_details&.dig("source", "type") == "git"
        poetry_object[key][dep_name] = {
          "git" => locked_details&.dig("source", "url"),
          "rev" => locked_details&.dig("source", "reference")
        }
      elsif poetry_object[key][dep_name].is_a?(Hash)
        poetry_object[key][dep_name]["version"] = locked_version
      else
        poetry_object[key][dep_name] = locked_version
      end
    end
  end

  TomlRB.dump(pyproject_object)
end

#replace_sources(credentials) ⇒ Object


21
22
23
24
25
26
27
28
29
# File 'lib/dependabot/python/file_updater/pyproject_preparer.rb', line 21

def replace_sources(credentials)
  pyproject_object = TomlRB.parse(pyproject_content)
  poetry_object = pyproject_object.fetch("tool").fetch("poetry")

  sources = pyproject_sources + config_variable_sources(credentials)
  poetry_object["source"] = sources if sources.any?

  TomlRB.dump(pyproject_object)
end

#sanitizeObject


31
32
33
34
35
36
# File 'lib/dependabot/python/file_updater/pyproject_preparer.rb', line 31

def sanitize
  # {{ name }} syntax not allowed
  pyproject_content.
    gsub(/\{\{.*?\}\}/, "something").
    gsub('#{', "{")
end