dependabot-python 0.404.0 → 0.405.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/helpers/lib/parser.py +14 -9
- data/helpers/run.py +1 -1
- data/helpers/test/test_parse_requirements.py +17 -0
- data/lib/dependabot/python/dependency_grapher/requirements_layers.rb +2 -24
- data/lib/dependabot/python/dependency_grapher.rb +7 -3
- data/lib/dependabot/python/file_parser/pep_dependency.rb +4 -1
- data/lib/dependabot/python/file_parser/poetry_lock/record.rb +124 -0
- data/lib/dependabot/python/file_parser/poetry_lock.rb +73 -39
- data/lib/dependabot/python/file_parser/pyproject_document/pep621_mutations.rb +154 -0
- data/lib/dependabot/python/file_parser/pyproject_document/poetry_mutations.rb +195 -0
- data/lib/dependabot/python/file_parser/pyproject_document.rb +3 -0
- data/lib/dependabot/python/file_parser.rb +58 -7
- data/lib/dependabot/python/file_updater/poetry_file_updater.rb +46 -86
- data/lib/dependabot/python/file_updater/pyproject_preparer.rb +32 -212
- data/lib/dependabot/python/file_updater.rb +7 -3
- data/lib/dependabot/python/marker_evaluator.rb +13 -2
- data/lib/dependabot/python/poetry_subdependency_pin.rb +20 -106
- data/lib/dependabot/python/requirement_references.rb +34 -0
- data/lib/dependabot/python/shared_file_fetcher.rb +3 -2
- data/lib/dependabot/python/update_checker/poetry_version_resolver.rb +29 -63
- metadata +8 -4
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
# typed:
|
|
1
|
+
# typed: strong
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
-
require "toml-rb"
|
|
5
4
|
require "sorbet-runtime"
|
|
6
5
|
require "dependabot/dependency"
|
|
7
6
|
require "dependabot/python/file_parser"
|
|
7
|
+
require "dependabot/python/file_parser/poetry_lock"
|
|
8
|
+
require "dependabot/python/file_parser/pyproject_document"
|
|
8
9
|
require "dependabot/python/file_updater"
|
|
9
10
|
require "dependabot/python/authed_url_builder"
|
|
10
|
-
require "dependabot/python/name_normaliser"
|
|
11
|
-
require "dependabot/python/requirement"
|
|
12
11
|
require "securerandom"
|
|
13
12
|
|
|
14
13
|
module Dependabot
|
|
@@ -17,119 +16,26 @@ module Dependabot
|
|
|
17
16
|
class PyprojectPreparer
|
|
18
17
|
extend T::Sig
|
|
19
18
|
|
|
20
|
-
# Fixed regex for extracting the name+extras prefix from a PEP 508 entry.
|
|
21
|
-
# Does not interpolate library input, avoiding polynomial backtracking.
|
|
22
|
-
PEP508_PREFIX = /\A(?<prefix>(?<name>[a-zA-Z0-9](?:[a-zA-Z0-9._-]*[a-zA-Z0-9])?)(?:\[[^\]]*\])?)/i
|
|
23
|
-
|
|
24
|
-
# Pins a single PEP 508 dependency entry string to a specific version,
|
|
25
|
-
# preserving extras and environment markers.
|
|
26
|
-
sig { params(entry: String, version: String).returns(String) }
|
|
27
|
-
def self.pin_pep508_entry(entry, version)
|
|
28
|
-
prefix_match = entry.match(PEP508_PREFIX)
|
|
29
|
-
return entry unless prefix_match
|
|
30
|
-
|
|
31
|
-
prefix = T.must(prefix_match[:prefix])
|
|
32
|
-
rest = T.must(entry[prefix.length..])
|
|
33
|
-
|
|
34
|
-
# Skip direct references (e.g. "pkg @ git+https://...") — already pinned to a URL
|
|
35
|
-
return entry if rest.match?(/\A\s*@\s/)
|
|
36
|
-
|
|
37
|
-
# Extract the environment marker ("; ..." suffix) if present
|
|
38
|
-
marker_match = rest.match(/(\s*;.*)/)
|
|
39
|
-
marker = marker_match ? marker_match[1] : ""
|
|
40
|
-
|
|
41
|
-
"#{prefix}==#{version}#{marker}"
|
|
42
|
-
end
|
|
43
|
-
private_class_method :pin_pep508_entry
|
|
44
|
-
|
|
45
|
-
# Freezes PEP 621 dependencies in-place within a parsed pyproject object.
|
|
46
|
-
# Replaces version specifiers with ==dep.version for each matching dep.
|
|
47
|
-
# Accepts an optional block to filter which dependencies to freeze.
|
|
48
|
-
sig do
|
|
49
|
-
params(
|
|
50
|
-
pyproject_object: T::Hash[String, T.untyped],
|
|
51
|
-
deps: T::Array[Dependabot::Dependency],
|
|
52
|
-
blk: T.nilable(T.proc.params(dep: Dependabot::Dependency).returns(T::Boolean))
|
|
53
|
-
).void
|
|
54
|
-
end
|
|
55
|
-
def self.freeze_pep621_deps!(pyproject_object, deps, &blk)
|
|
56
|
-
dep_arrays = collect_pep621_dep_arrays(pyproject_object)
|
|
57
|
-
return if dep_arrays.empty?
|
|
58
|
-
|
|
59
|
-
deps.each do |dep|
|
|
60
|
-
next if blk && !yield(dep)
|
|
61
|
-
next unless dep.version
|
|
62
|
-
|
|
63
|
-
pin_pep621_dep_in_arrays!(dep_arrays, dep)
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
sig { params(pyproject_object: T::Hash[String, T.untyped]).returns(T::Array[T::Array[String]]) }
|
|
68
|
-
def self.collect_pep621_dep_arrays(pyproject_object)
|
|
69
|
-
project_object = pyproject_object["project"]
|
|
70
|
-
return [] unless project_object
|
|
71
|
-
|
|
72
|
-
dep_arrays = [project_object["dependencies"]]
|
|
73
|
-
project_object["optional-dependencies"]&.each_value { |opt| dep_arrays << opt }
|
|
74
|
-
dep_arrays.compact!
|
|
75
|
-
dep_arrays.select! { |arr| arr.is_a?(Array) && arr.all?(String) }
|
|
76
|
-
dep_arrays
|
|
77
|
-
end
|
|
78
|
-
private_class_method :collect_pep621_dep_arrays
|
|
79
|
-
|
|
80
|
-
sig { params(dep_arrays: T::Array[T::Array[String]], dep: Dependabot::Dependency).void }
|
|
81
|
-
def self.pin_pep621_dep_in_arrays!(dep_arrays, dep)
|
|
82
|
-
normalised_name = NameNormaliser.normalise(dep.name)
|
|
83
|
-
dep_arrays.each do |arr|
|
|
84
|
-
arr.each_with_index do |entry, i|
|
|
85
|
-
match = entry.match(PEP508_PREFIX)
|
|
86
|
-
next unless match
|
|
87
|
-
next unless NameNormaliser.normalise(T.must(match[:name])) == normalised_name
|
|
88
|
-
|
|
89
|
-
arr[i] = pin_pep508_entry(entry, T.must(dep.version))
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
private_class_method :pin_pep621_dep_in_arrays!
|
|
94
|
-
|
|
95
19
|
sig { params(pyproject_content: String, lockfile: T.nilable(Dependabot::DependencyFile)).void }
|
|
96
20
|
def initialize(pyproject_content:, lockfile: nil)
|
|
97
21
|
@pyproject_content = pyproject_content
|
|
98
22
|
@lockfile = lockfile
|
|
99
|
-
@parsed_lockfile = T.let(nil, T.nilable(T::Hash[String, T.untyped]))
|
|
100
23
|
end
|
|
101
24
|
|
|
102
25
|
# For hosted Dependabot token will be nil since the credentials aren't present.
|
|
103
26
|
# This is for those running Dependabot themselves and for dry-run.
|
|
104
27
|
sig { params(credentials: T.nilable(T::Array[Dependabot::Credential])).void }
|
|
105
28
|
def add_auth_env_vars(credentials)
|
|
106
|
-
|
|
107
|
-
cred = credentials&.find { |c| c["index-url"] == source["url"] }
|
|
108
|
-
next unless cred
|
|
29
|
+
return if credentials.nil? || credentials.empty?
|
|
109
30
|
|
|
110
|
-
|
|
111
|
-
next unless token && token.count(":") == 1
|
|
112
|
-
|
|
113
|
-
arr = token.split(":")
|
|
114
|
-
# https://python-poetry.org/docs/configuration/#using-environment-variables
|
|
115
|
-
name = source["name"]&.upcase&.gsub(/\W/, "_")
|
|
116
|
-
ENV["POETRY_HTTP_BASIC_#{name}_USERNAME"] = arr[0]
|
|
117
|
-
ENV["POETRY_HTTP_BASIC_#{name}_PASSWORD"] = arr[1]
|
|
118
|
-
end
|
|
31
|
+
auth_environment(credentials).each { |key, value| ENV[key] = value }
|
|
119
32
|
end
|
|
120
33
|
|
|
121
34
|
sig { params(requirement: String).returns(String) }
|
|
122
35
|
def update_python_requirement(requirement)
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
python_requirements = Python::Requirement.requirements_array(python_specification)
|
|
127
|
-
unless python_requirements.empty? || python_requirements.any? { |req| req.satisfied_by?(requirement) }
|
|
128
|
-
pyproject_object["tool"]["poetry"]["dependencies"]["python"] = "~#{requirement}"
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
TomlRB.dump(pyproject_object)
|
|
36
|
+
document = FileParser::PyprojectDocument.from_content(@pyproject_content)
|
|
37
|
+
document.update_python_requirement(requirement)
|
|
38
|
+
document.to_toml
|
|
133
39
|
end
|
|
134
40
|
|
|
135
41
|
sig { returns(String) }
|
|
@@ -140,133 +46,47 @@ module Dependabot
|
|
|
140
46
|
.gsub('#{', "{")
|
|
141
47
|
end
|
|
142
48
|
|
|
143
|
-
UNSUPPORTED_SOURCE_TYPES = %w(directory file url).freeze
|
|
144
|
-
|
|
145
49
|
sig { params(dependencies: T::Array[Dependabot::Dependency]).returns(String) }
|
|
146
50
|
def freeze_top_level_dependencies_except(dependencies)
|
|
147
51
|
return pyproject_content unless lockfile
|
|
148
52
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return pyproject_content unless poetry_object || locked_poetry_build_backend?
|
|
53
|
+
document = FileParser::PyprojectDocument.from_content(pyproject_content)
|
|
54
|
+
return pyproject_content unless document.poetry? || document.poetry_build_backend?
|
|
153
55
|
|
|
154
|
-
|
|
56
|
+
lock = FileParser::PoetryLock.from_content(T.must(T.must(lockfile).content))
|
|
155
57
|
excluded_names = dependencies.map(&:name) + ["python"]
|
|
58
|
+
document.freeze_poetry_dependencies(lock: lock, excluding: excluded_names)
|
|
59
|
+
document.freeze_locked_pep621_dependencies(lock: lock, excluding: excluded_names)
|
|
60
|
+
document.to_toml
|
|
61
|
+
end
|
|
156
62
|
|
|
157
|
-
|
|
158
|
-
next unless poetry_object[key]
|
|
159
|
-
|
|
160
|
-
poetry_object.fetch(key).each do |dep_name, _|
|
|
161
|
-
next if excluded_names.include?(normalise(dep_name))
|
|
63
|
+
private
|
|
162
64
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
65
|
+
sig { params(credentials: T::Array[Dependabot::Credential]).returns(T::Hash[String, T.nilable(String)]) }
|
|
66
|
+
def auth_environment(credentials)
|
|
67
|
+
assignments = T.let({}, T::Hash[String, T.nilable(String)])
|
|
68
|
+
FileParser::PyprojectDocument.from_content(@pyproject_content).poetry_auth_sources.each do |source|
|
|
69
|
+
url = source.url
|
|
70
|
+
cred = credentials.find { |c| c["index-url"] == url }
|
|
71
|
+
next unless cred
|
|
166
72
|
|
|
167
|
-
|
|
168
|
-
|
|
73
|
+
token = cred["token"]
|
|
74
|
+
next unless token && token.count(":") == 1
|
|
169
75
|
|
|
170
|
-
|
|
76
|
+
arr = token.split(":")
|
|
77
|
+
# https://python-poetry.org/docs/configuration/#using-environment-variables
|
|
78
|
+
name = source.name&.upcase&.gsub(/\W/, "_")
|
|
79
|
+
assignments["POETRY_HTTP_BASIC_#{name}_USERNAME"] = arr[0]
|
|
80
|
+
assignments["POETRY_HTTP_BASIC_#{name}_PASSWORD"] = arr[1]
|
|
81
|
+
end
|
|
82
|
+
assignments
|
|
171
83
|
end
|
|
172
84
|
|
|
173
|
-
private
|
|
174
|
-
|
|
175
85
|
sig { returns(String) }
|
|
176
86
|
attr_reader :pyproject_content
|
|
177
87
|
|
|
178
88
|
sig { returns(T.nilable(Dependabot::DependencyFile)) }
|
|
179
89
|
attr_reader :lockfile
|
|
180
|
-
|
|
181
|
-
sig { returns(T::Boolean) }
|
|
182
|
-
def locked_poetry_build_backend?
|
|
183
|
-
!lockfile.nil? && FileParser::PyprojectDocument.from_content(pyproject_content).poetry_build_backend?
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
sig { params(deps_hash: T::Hash[String, T.untyped], dep_name: String).void }
|
|
187
|
-
def freeze_poetry_dep!(deps_hash, dep_name)
|
|
188
|
-
details = locked_details(dep_name)
|
|
189
|
-
return unless (locked_version = details&.fetch("version"))
|
|
190
|
-
|
|
191
|
-
source_type = details.dig("source", "type")
|
|
192
|
-
return if UNSUPPORTED_SOURCE_TYPES.include?(source_type)
|
|
193
|
-
|
|
194
|
-
if source_type == "git"
|
|
195
|
-
freeze_git_dep!(deps_hash, dep_name, details)
|
|
196
|
-
elsif deps_hash[dep_name].is_a?(Hash)
|
|
197
|
-
deps_hash[dep_name]["version"] = locked_version
|
|
198
|
-
elsif !deps_hash[dep_name].is_a?(Array)
|
|
199
|
-
deps_hash[dep_name] = locked_version
|
|
200
|
-
end
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
sig { params(deps_hash: T::Hash[String, T.untyped], dep_name: String, details: T::Hash[String, T.untyped]).void }
|
|
204
|
-
def freeze_git_dep!(deps_hash, dep_name, details)
|
|
205
|
-
existing_extras = deps_hash[dep_name].is_a?(Hash) ? deps_hash[dep_name]["extras"] : nil
|
|
206
|
-
|
|
207
|
-
deps_hash[dep_name] = {
|
|
208
|
-
"git" => details.dig("source", "url"),
|
|
209
|
-
"rev" => details.dig("source", "reference")
|
|
210
|
-
}
|
|
211
|
-
subdirectory = details.dig("source", "subdirectory")
|
|
212
|
-
deps_hash[dep_name]["subdirectory"] = subdirectory if subdirectory
|
|
213
|
-
deps_hash[dep_name]["extras"] = existing_extras if existing_extras
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
sig { params(pyproject_object: T::Hash[String, T.untyped], excluded_names: T::Array[String]).void }
|
|
217
|
-
def freeze_pep621_top_level_deps!(pyproject_object, excluded_names)
|
|
218
|
-
project_object = pyproject_object["project"]
|
|
219
|
-
return unless project_object
|
|
220
|
-
|
|
221
|
-
freeze_pep621_dep_array!(project_object["dependencies"], excluded_names)
|
|
222
|
-
|
|
223
|
-
project_object["optional-dependencies"]&.each_value do |opt_deps|
|
|
224
|
-
freeze_pep621_dep_array!(opt_deps, excluded_names)
|
|
225
|
-
end
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
sig { params(dep_array: T.nilable(T::Array[String]), excluded_names: T::Array[String]).void }
|
|
229
|
-
def freeze_pep621_dep_array!(dep_array, excluded_names)
|
|
230
|
-
return unless dep_array
|
|
231
|
-
|
|
232
|
-
dep_array.each_with_index do |entry, index|
|
|
233
|
-
# Extract dependency name from PEP 508 string
|
|
234
|
-
match = entry.match(/\A([a-zA-Z0-9](?:[a-zA-Z0-9._-]*[a-zA-Z0-9])?)/i)
|
|
235
|
-
next unless match
|
|
236
|
-
|
|
237
|
-
dep_name = normalise(T.must(match[1]))
|
|
238
|
-
next if excluded_names.include?(dep_name)
|
|
239
|
-
|
|
240
|
-
# One pin can't suit every Python branch of a package locked at several versions.
|
|
241
|
-
next if locked_entry_count(dep_name) > 1
|
|
242
|
-
|
|
243
|
-
locked_details = locked_details(dep_name)
|
|
244
|
-
next unless (locked_version = locked_details&.fetch("version"))
|
|
245
|
-
|
|
246
|
-
dep_array[index] = self.class.send(:pin_pep508_entry, entry, locked_version)
|
|
247
|
-
end
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
sig { params(dep_name: String).returns(T.nilable(T::Hash[String, T.untyped])) }
|
|
251
|
-
def locked_details(dep_name)
|
|
252
|
-
parsed_lockfile.fetch("package")
|
|
253
|
-
.find { |d| d["name"] == normalise(dep_name) }
|
|
254
|
-
end
|
|
255
|
-
|
|
256
|
-
sig { params(dep_name: String).returns(Integer) }
|
|
257
|
-
def locked_entry_count(dep_name)
|
|
258
|
-
parsed_lockfile.fetch("package").count { |d| d["name"] == normalise(dep_name) }
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
sig { params(name: String).returns(String) }
|
|
262
|
-
def normalise(name)
|
|
263
|
-
NameNormaliser.normalise(name)
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
sig { returns(T::Hash[String, T.untyped]) }
|
|
267
|
-
def parsed_lockfile
|
|
268
|
-
@parsed_lockfile ||= TomlRB.parse(lockfile&.content)
|
|
269
|
-
end
|
|
270
90
|
end
|
|
271
91
|
end
|
|
272
92
|
end
|
|
@@ -20,18 +20,22 @@ module Dependabot
|
|
|
20
20
|
|
|
21
21
|
sig { override.returns(T::Array[DependencyFile]) }
|
|
22
22
|
def updated_dependency_files
|
|
23
|
+
resolver = resolver_type
|
|
23
24
|
updated_files =
|
|
24
|
-
case
|
|
25
|
+
case resolver
|
|
25
26
|
when :pipfile then updated_pipfile_based_files
|
|
26
27
|
when :poetry then updated_poetry_based_files
|
|
27
28
|
when :pip_compile then updated_pip_compile_based_files
|
|
28
29
|
when :requirements then updated_requirement_based_files
|
|
29
|
-
else raise "Unexpected resolver type: #{
|
|
30
|
+
else raise "Unexpected resolver type: #{resolver}"
|
|
30
31
|
end
|
|
31
32
|
|
|
32
33
|
if updated_files.none? ||
|
|
33
34
|
updated_files.sort_by(&:name) == dependency_files.sort_by(&:name)
|
|
34
|
-
raise
|
|
35
|
+
raise no_files_changed_error(
|
|
36
|
+
"No files have changed!",
|
|
37
|
+
{ resolver: resolver, updated_files: updated_files.map(&:name) }
|
|
38
|
+
)
|
|
35
39
|
end
|
|
36
40
|
|
|
37
41
|
updated_files
|
|
@@ -8,6 +8,13 @@ module Dependabot
|
|
|
8
8
|
class MarkerEvaluator
|
|
9
9
|
extend T::Sig
|
|
10
10
|
|
|
11
|
+
# File parsers can't know the target platform, so they assume non-Python markers hold. The resolver
|
|
12
|
+
# leaves this off so it only applies constraints whose markers depend on the Python version.
|
|
13
|
+
sig { params(non_python_markers_satisfied: T::Boolean).void }
|
|
14
|
+
def initialize(non_python_markers_satisfied: false)
|
|
15
|
+
@non_python_markers_satisfied = non_python_markers_satisfied
|
|
16
|
+
end
|
|
17
|
+
|
|
11
18
|
sig { params(requirement_string: String).returns([T.nilable(String), T.nilable(String)]) }
|
|
12
19
|
def split_requirement_and_marker(requirement_string)
|
|
13
20
|
separator_index = T.let(nil, T.nilable(Integer))
|
|
@@ -60,12 +67,16 @@ module Dependabot
|
|
|
60
67
|
|
|
61
68
|
not_expression = strip_top_level_not(expr)
|
|
62
69
|
if not_expression
|
|
63
|
-
return
|
|
70
|
+
return @non_python_markers_satisfied unless python_marker?(not_expression)
|
|
64
71
|
|
|
65
72
|
return !evaluate_marker_expression(not_expression, python_version)
|
|
66
73
|
end
|
|
67
74
|
|
|
68
|
-
evaluate_python_version_condition(
|
|
75
|
+
evaluate_python_version_condition(
|
|
76
|
+
expr,
|
|
77
|
+
python_version,
|
|
78
|
+
default: python_marker?(expr) || @non_python_markers_satisfied
|
|
79
|
+
)
|
|
69
80
|
end
|
|
70
81
|
|
|
71
82
|
sig { params(expression: String).returns(T.nilable(String)) }
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# typed:
|
|
1
|
+
# typed: strong
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
4
|
require "sorbet-runtime"
|
|
5
|
-
require "toml-rb"
|
|
6
5
|
require "dependabot/dependency_file"
|
|
6
|
+
require "dependabot/python/file_parser/poetry_lock"
|
|
7
|
+
require "dependabot/python/file_parser/pyproject_document"
|
|
7
8
|
require "dependabot/python/name_normaliser"
|
|
8
|
-
require "dependabot/python/file_updater/pyproject_preparer"
|
|
9
9
|
|
|
10
10
|
module Dependabot
|
|
11
11
|
module Python
|
|
@@ -25,24 +25,6 @@ module Dependabot
|
|
|
25
25
|
# rather than risk locking a version outside its intended scope.
|
|
26
26
|
class ScopeNotReproducible < StandardError; end
|
|
27
27
|
|
|
28
|
-
# A string, or a table such as PEP 735's `{ include-group = "..." }`.
|
|
29
|
-
DeclarationEntry = T.type_alias { T.any(String, T::Hash[String, String]) }
|
|
30
|
-
private_constant :DeclarationEntry
|
|
31
|
-
|
|
32
|
-
class LockedEntry < T::Struct
|
|
33
|
-
const :optional, T::Boolean
|
|
34
|
-
const :groups, T::Array[String]
|
|
35
|
-
const :markers, T::Hash[String, T::Array[String]]
|
|
36
|
-
|
|
37
|
-
extend T::Sig
|
|
38
|
-
|
|
39
|
-
sig { params(group: String).returns(T::Array[String]) }
|
|
40
|
-
def markers_for(group)
|
|
41
|
-
markers.fetch(group, [])
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
private_constant :LockedEntry
|
|
45
|
-
|
|
46
28
|
sig { params(lockfile: T.nilable(Dependabot::DependencyFile), name: String).void }
|
|
47
29
|
def initialize(lockfile, name)
|
|
48
30
|
@lockfile = lockfile
|
|
@@ -51,16 +33,14 @@ module Dependabot
|
|
|
51
33
|
|
|
52
34
|
# Updates every declaration of the dependency in place; a sub-dependency isn't declared anywhere,
|
|
53
35
|
# so it is pinned instead to force Poetry's solver to consider the target version.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
project_object = pyproject_object["project"]
|
|
58
|
-
return unless project_object
|
|
36
|
+
sig { params(document: FileParser::PyprojectDocument, requirement: String).void }
|
|
37
|
+
def set_requirement!(document, requirement)
|
|
38
|
+
return unless document.project?
|
|
59
39
|
|
|
60
40
|
ensure_single_version!
|
|
61
|
-
return if
|
|
41
|
+
return if document.replace_pep621_requirement(name: @name, requirement: requirement).positive?
|
|
62
42
|
|
|
63
|
-
pin!(
|
|
43
|
+
pin!(document, "#{@name}#{requirement}")
|
|
64
44
|
end
|
|
65
45
|
|
|
66
46
|
# A package locked at several versions has per-marker scopes that one target version can't reproduce.
|
|
@@ -72,26 +52,16 @@ module Dependabot
|
|
|
72
52
|
# Adds a pin per place the package is locked (main, extras, other groups), each keeping its locked
|
|
73
53
|
# markers. Raises ScopeNotReproducible for a package locked at several versions, or an optional
|
|
74
54
|
# package with no extras in its markers.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
project_object = pyproject_object["project"]
|
|
79
|
-
return unless project_object
|
|
55
|
+
sig { params(document: FileParser::PyprojectDocument, pin: String).void }
|
|
56
|
+
def pin!(document, pin)
|
|
57
|
+
return unless document.project?
|
|
80
58
|
|
|
81
59
|
main_pin, extra_pins, group_pins = pins_for(pin)
|
|
82
|
-
|
|
83
|
-
(project_object["dependencies"] ||= []) << main_pin if main_pin
|
|
84
|
-
append_pins!(project_object["optional-dependencies"] ||= {}, extra_pins)
|
|
85
|
-
append_pins!(pyproject_object["dependency-groups"] ||= {}, group_pins) if group_pins.any?
|
|
60
|
+
document.append_pep621_pins(main: main_pin, extras: extra_pins, groups: group_pins)
|
|
86
61
|
end
|
|
87
62
|
|
|
88
63
|
private
|
|
89
64
|
|
|
90
|
-
sig { params(table: T::Hash[String, T::Array[DeclarationEntry]], pins: T::Hash[String, String]).void }
|
|
91
|
-
def append_pins!(table, pins)
|
|
92
|
-
pins.each { |key, pin| (table[declared_key(table, key)] ||= []) << pin }
|
|
93
|
-
end
|
|
94
|
-
|
|
95
65
|
# Returns [main_pin, extra_pins, group_pins]. Raises ScopeNotReproducible for a package locked at
|
|
96
66
|
# several versions, or an optional package with no extras in its markers (legacy lockfile format):
|
|
97
67
|
# pinning it unconditionally would promote it into the main install.
|
|
@@ -109,83 +79,27 @@ module Dependabot
|
|
|
109
79
|
[nil, extra_pins(pin, entry), group_pins]
|
|
110
80
|
end
|
|
111
81
|
|
|
112
|
-
sig { params(pin: String, entry:
|
|
82
|
+
sig { params(pin: String, entry: FileParser::PoetryLock::Scope, group: String).returns(String) }
|
|
113
83
|
def scoped_pin(pin, entry, group)
|
|
114
84
|
marker = entry.markers_for(group).first
|
|
115
85
|
marker ? "#{pin}; #{marker}" : pin
|
|
116
86
|
end
|
|
117
87
|
|
|
118
|
-
|
|
119
|
-
sig { params(table: T::Hash[String, T::Array[DeclarationEntry]], name: String).returns(String) }
|
|
120
|
-
def declared_key(table, name)
|
|
121
|
-
table.keys.find { |key| normalise_key(key) == normalise_key(name) } || name
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
sig { params(name: String).returns(String) }
|
|
125
|
-
def normalise_key(name)
|
|
126
|
-
name.downcase.gsub(/[-_.]+/, "-")
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
# rubocop:disable-next Sorbet/ForbidTUntyped
|
|
130
|
-
sig { params(pyproject_object: T::Hash[String, T.untyped]).returns(T::Array[T::Array[DeclarationEntry]]) }
|
|
131
|
-
def declaration_arrays(pyproject_object)
|
|
132
|
-
project = pyproject_object.fetch("project", {})
|
|
133
|
-
tables = [project["optional-dependencies"], pyproject_object["dependency-groups"]].compact
|
|
134
|
-
[project["dependencies"], *tables.flat_map(&:values)].grep(Array)
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
sig { params(declarations: T::Array[DeclarationEntry], requirement: String).returns(T::Boolean) }
|
|
138
|
-
def update_declarations!(declarations, requirement)
|
|
139
|
-
updated = T.let(false, T::Boolean)
|
|
140
|
-
|
|
141
|
-
declarations.each_with_index do |entry, index|
|
|
142
|
-
next unless entry.is_a?(String)
|
|
143
|
-
|
|
144
|
-
match = entry.match(Python::FileUpdater::PyprojectPreparer::PEP508_PREFIX)
|
|
145
|
-
next unless match && NameNormaliser.normalise(T.must(match[:name])) == @name
|
|
146
|
-
|
|
147
|
-
prefix = T.must(match[:prefix])
|
|
148
|
-
head, separator, tail = entry[prefix.length..].to_s.partition(";")
|
|
149
|
-
marker = separator.empty? ? "" : "#{head[head.rstrip.length..]};#{tail}"
|
|
150
|
-
declarations[index] = "#{prefix}#{requirement}#{marker}"
|
|
151
|
-
updated = true
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
updated
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
sig { returns(T::Array[LockedEntry]) }
|
|
88
|
+
sig { returns(T::Array[FileParser::PoetryLock::Scope]) }
|
|
158
89
|
def entries
|
|
159
|
-
@entries ||= T.let(locked_entries, T.nilable(T::Array[
|
|
90
|
+
@entries ||= T.let(locked_entries, T.nilable(T::Array[FileParser::PoetryLock::Scope]))
|
|
160
91
|
end
|
|
161
92
|
|
|
162
|
-
sig { returns(T::Array[
|
|
93
|
+
sig { returns(T::Array[FileParser::PoetryLock::Scope]) }
|
|
163
94
|
def locked_entries
|
|
164
95
|
return [] unless @lockfile
|
|
165
96
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
# Legacy lockfiles record no groups; those entries are treated as main.
|
|
170
|
-
groups = Array(entry["groups"])
|
|
171
|
-
groups = ["main"] if groups.empty?
|
|
172
|
-
LockedEntry.new(optional: entry["optional"] == true, groups: groups, markers: locked_markers(entry, groups))
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
# Markers are a single string, or a table keyed by group.
|
|
177
|
-
# rubocop:disable-next Sorbet/ForbidTUntyped
|
|
178
|
-
sig do
|
|
179
|
-
params(entry: T::Hash[String, T.untyped], groups: T::Array[String]).returns(T::Hash[String, T::Array[String]])
|
|
180
|
-
end
|
|
181
|
-
def locked_markers(entry, groups)
|
|
182
|
-
markers = entry["markers"]
|
|
183
|
-
return markers.transform_values { |marker| ["(#{marker})"] } if markers.is_a?(Hash)
|
|
184
|
-
|
|
185
|
-
groups.to_h { |group| [group, markers ? ["(#{markers})"] : []] }
|
|
97
|
+
FileParser::PoetryLock.from_content(T.must(@lockfile.content))
|
|
98
|
+
.records_for(@name) { |name| NameNormaliser.normalise(name) }
|
|
99
|
+
.map(&:scope)
|
|
186
100
|
end
|
|
187
101
|
|
|
188
|
-
sig { params(pin: String, entry:
|
|
102
|
+
sig { params(pin: String, entry: FileParser::PoetryLock::Scope).returns(T::Hash[String, String]) }
|
|
189
103
|
def extra_pins(pin, entry)
|
|
190
104
|
marker = entry.markers_for("main").first.to_s
|
|
191
105
|
extras = marker.scan(EXTRA_NAME).flatten.uniq
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# typed: strong
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "pathname"
|
|
5
|
+
require "sorbet-runtime"
|
|
6
|
+
|
|
7
|
+
require "dependabot/dependency_file"
|
|
8
|
+
|
|
9
|
+
module Dependabot
|
|
10
|
+
module Python
|
|
11
|
+
module RequirementReferences
|
|
12
|
+
extend T::Sig
|
|
13
|
+
|
|
14
|
+
CHILD_REQUIREMENT_REGEX = /^-r\s?(?<path>.*\.(?:txt|in))/
|
|
15
|
+
CONSTRAINT_REGEX = /^-c\s?(?<path>.*\.(?:txt|in))/
|
|
16
|
+
|
|
17
|
+
# Resolves the sibling paths a requirements file references via `-r`/`-c`, returning repo-relative
|
|
18
|
+
# cleanpaths (matching fetched DependencyFile names).
|
|
19
|
+
sig { params(file: Dependabot::DependencyFile).returns(T::Array[String]) }
|
|
20
|
+
def self.paths(file)
|
|
21
|
+
content = file.content
|
|
22
|
+
return [] if content.nil?
|
|
23
|
+
|
|
24
|
+
current_dir = File.dirname(file.name)
|
|
25
|
+
referenced = content.scan(CHILD_REQUIREMENT_REGEX).flatten + content.scan(CONSTRAINT_REGEX).flatten
|
|
26
|
+
|
|
27
|
+
referenced.map do |path|
|
|
28
|
+
resolved = current_dir == "." ? path : File.join(current_dir, path)
|
|
29
|
+
Pathname.new(resolved).cleanpath.to_path
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -8,6 +8,7 @@ require "dependabot/file_fetchers"
|
|
|
8
8
|
require "dependabot/file_fetchers/base"
|
|
9
9
|
require "dependabot/python/language_version_manager"
|
|
10
10
|
require "dependabot/python/requirement_parser"
|
|
11
|
+
require "dependabot/python/requirement_references"
|
|
11
12
|
require "dependabot/python/file_parser/pyproject_files_parser"
|
|
12
13
|
require "dependabot/python/file_parser/python_requirement_parser"
|
|
13
14
|
require "dependabot/errors"
|
|
@@ -21,8 +22,8 @@ module Dependabot
|
|
|
21
22
|
|
|
22
23
|
abstract!
|
|
23
24
|
|
|
24
|
-
CHILD_REQUIREMENT_REGEX =
|
|
25
|
-
CONSTRAINT_REGEX =
|
|
25
|
+
CHILD_REQUIREMENT_REGEX = RequirementReferences::CHILD_REQUIREMENT_REGEX
|
|
26
|
+
CONSTRAINT_REGEX = RequirementReferences::CONSTRAINT_REGEX
|
|
26
27
|
DEPENDENCY_TYPES = %w(packages dev-packages).freeze
|
|
27
28
|
MAX_FILE_SIZE = 500_000
|
|
28
29
|
|