Class: Dependabot::Python::FileUpdater::PipfilePreparer

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

Instance Method Summary collapse

Constructor Details

#initialize(pipfile_content:, lockfile: nil) ⇒ PipfilePreparer

Returns a new instance of PipfilePreparer.


15
16
17
18
# File 'lib/dependabot/python/file_updater/pipfile_preparer.rb', line 15

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

Instance Method Details

#freeze_dependency(dep_name, pipfile_object, keys) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dependabot/python/file_updater/pipfile_preparer.rb', line 50

def freeze_dependency(dep_name, pipfile_object, keys)
  locked_version = version_from_lockfile(
    keys[:lockfile],
    normalise(dep_name)
  )
  locked_ref = ref_from_lockfile(
    keys[:lockfile],
    normalise(dep_name)
  )

  pipfile_req = pipfile_object[keys[:pipfile]][dep_name]
  if pipfile_req.is_a?(Hash) && locked_version
    pipfile_req["version"] = "==#{locked_version}"
  elsif pipfile_req.is_a?(Hash) && locked_ref && !pipfile_req["ref"]
    pipfile_req["ref"] = locked_ref
  elsif locked_version
    pipfile_object[keys[:pipfile]][dep_name] = "==#{locked_version}"
  end
end

#freeze_top_level_dependencies_except(dependencies) ⇒ Object


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

def freeze_top_level_dependencies_except(dependencies)
  return pipfile_content unless lockfile

  pipfile_object = TomlRB.parse(pipfile_content)
  excluded_names = dependencies.map(&:name)

  Python::FileParser::DEPENDENCY_GROUP_KEYS.each do |keys|
    next unless pipfile_object[keys[:pipfile]]

    pipfile_object.fetch(keys[:pipfile]).each do |dep_name, _|
      next if excluded_names.include?(normalise(dep_name))

      freeze_dependency(dep_name, pipfile_object, keys)
    end
  end

  TomlRB.dump(pipfile_object)
end

#replace_sources(credentials) ⇒ Object


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

def replace_sources(credentials)
  pipfile_object = TomlRB.parse(pipfile_content)

  pipfile_object["source"] =
    pipfile_sources.reject { |h| h["url"].include?("${") } +
    config_variable_sources(credentials)

  TomlRB.dump(pipfile_object)
end

#update_python_requirement(requirement) ⇒ Object

rubocop:enable Metrics/PerceivedComplexity


71
72
73
74
75
76
77
78
79
80
# File 'lib/dependabot/python/file_updater/pipfile_preparer.rb', line 71

def update_python_requirement(requirement)
  pipfile_object = TomlRB.parse(pipfile_content)

  pipfile_object["requires"] ||= {}
  pipfile_object["requires"].delete("python_full_version")
  pipfile_object["requires"].delete("python_version")
  pipfile_object["requires"]["python_full_version"] = requirement

  TomlRB.dump(pipfile_object)
end