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.



13
14
15
16
# File 'lib/dependabot/python/file_updater/pipfile_preparer.rb', line 13

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



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

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



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

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



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

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



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

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