forked from dependabot/dependabot-core
/
pip_compile_file_updater.rb
605 lines (478 loc) 路 20.8 KB
/
pip_compile_file_updater.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# frozen_string_literal: true
require "open3"
require "dependabot/dependency"
require "dependabot/python/requirement_parser"
require "dependabot/python/file_fetcher"
require "dependabot/python/file_parser/python_requirement_parser"
require "dependabot/python/file_updater"
require "dependabot/shared_helpers"
require "dependabot/python/native_helpers"
require "dependabot/python/python_versions"
require "dependabot/python/name_normaliser"
require "dependabot/python/authed_url_builder"
module Dependabot
module Python
class FileUpdater
# rubocop:disable Metrics/ClassLength
class PipCompileFileUpdater
require_relative "requirement_replacer"
require_relative "requirement_file_updater"
require_relative "setup_file_sanitizer"
UNSAFE_PACKAGES = %w(setuptools distribute pip).freeze
INCOMPATIBLE_VERSIONS_REGEX = /There are incompatible versions in the resolved dependencies:.*\z/m.freeze
WARNINGS = /\s*# WARNING:.*\Z/m.freeze
UNSAFE_NOTE =
/\s*# The following packages are considered to be unsafe.*\Z/m.freeze
attr_reader :dependencies, :dependency_files, :credentials
def initialize(dependencies:, dependency_files:, credentials:)
@dependencies = dependencies
@dependency_files = dependency_files
@credentials = credentials
end
def updated_dependency_files
return @updated_dependency_files if @update_already_attempted
@update_already_attempted = true
@updated_dependency_files ||= fetch_updated_dependency_files
end
private
def dependency
# For now, we'll only ever be updating a single dependency
dependencies.first
end
def fetch_updated_dependency_files
updated_compiled_files = compile_new_requirement_files
updated_manifest_files = update_manifest_files
updated_files = updated_compiled_files + updated_manifest_files
updated_uncompiled_files = update_uncompiled_files(updated_files)
[
*updated_manifest_files,
*updated_compiled_files,
*updated_uncompiled_files
]
end
def compile_new_requirement_files
SharedHelpers.in_a_temporary_directory do
write_updated_dependency_files
install_required_python
filenames_to_compile.each do |filename|
# Shell out to pip-compile, generate a new set of requirements.
# This is slow, as pip-compile needs to do installs.
name_part = "pyenv exec pip-compile "\
"#{pip_compile_options(filename)} -P "\
"#{dependency.name}"
version_part = "#{dependency.version} #{filename}"
# Don't escape pyenv `dep-name==version` syntax
run_pip_compile_command(
"#{SharedHelpers.escape_command(name_part)}=="\
"#{SharedHelpers.escape_command(version_part)}",
allow_unsafe_shell_command: true
)
# Run pip-compile a second time, without an update argument, to
# ensure it resets the right comments.
run_pip_compile_command(
"pyenv exec pip-compile #{pip_compile_options(filename)} "\
"#{filename}"
)
end
# Remove any .python-version file before parsing the reqs
FileUtils.remove_entry(".python-version", true)
dependency_files.map do |file|
next unless file.name.end_with?(".txt")
updated_content = File.read(file.name)
updated_content =
post_process_compiled_file(updated_content, file)
next if updated_content == file.content
file.dup.tap { |f| f.content = updated_content }
end.compact
end
end
def update_manifest_files
dependency_files.map do |file|
next unless file.name.end_with?(".in")
file = file.dup
updated_content = update_dependency_requirement(file)
next if updated_content == file.content
file.content = updated_content
file
end.compact
end
def update_uncompiled_files(updated_files)
updated_filenames = updated_files.map(&:name)
old_reqs = dependency.previous_requirements.
reject { |r| updated_filenames.include?(r[:file]) }
new_reqs = dependency.requirements.
reject { |r| updated_filenames.include?(r[:file]) }
return [] if new_reqs.none?
files = dependency_files.
reject { |file| updated_filenames.include?(file.name) }
args = dependency.to_h
args = args.keys.map { |k| [k.to_sym, args[k]] }.to_h
args[:requirements] = new_reqs
args[:previous_requirements] = old_reqs
RequirementFileUpdater.new(
dependencies: [Dependency.new(**args)],
dependency_files: files,
credentials: credentials
).updated_dependency_files
end
def run_command(cmd, env: python_env, allow_unsafe_shell_command: false)
start = Time.now
command = if allow_unsafe_shell_command
cmd
else
SharedHelpers.escape_command(cmd)
end
stdout, process = Open3.capture2e(env, command)
time_taken = Time.now - start
return stdout if process.success?
handle_pip_errors(stdout, command, time_taken, process.to_s)
end
def handle_pip_errors(stdout, command, time_taken, exit_value)
if stdout.match?(INCOMPATIBLE_VERSIONS_REGEX)
raise DependencyFileNotResolvable, stdout.match(INCOMPATIBLE_VERSIONS_REGEX)
end
raise SharedHelpers::HelperSubprocessFailed.new(
message: stdout,
error_context: {
command: command,
time_taken: time_taken,
process_exit_value: exit_value
}
)
end
def run_pip_compile_command(command, allow_unsafe_shell_command: false)
run_command("pyenv local #{python_version}")
run_command(
command,
allow_unsafe_shell_command: allow_unsafe_shell_command
)
end
def python_env
env = {}
# Handle Apache Airflow 1.10.x installs
if dependency_files.any? { |f| f.content.include?("apache-airflow") }
if dependency_files.any? { |f| f.content.include?("unidecode") }
env["AIRFLOW_GPL_UNIDECODE"] = "yes"
else
env["SLUGIFY_USES_TEXT_UNIDECODE"] = "yes"
end
end
env
end
def write_updated_dependency_files
dependency_files.each do |file|
path = file.name
FileUtils.mkdir_p(Pathname.new(path).dirname)
File.write(path, freeze_dependency_requirement(file))
end
# Overwrite the .python-version with updated content
File.write(".python-version", python_version)
setup_files.each do |file|
path = file.name
FileUtils.mkdir_p(Pathname.new(path).dirname)
File.write(path, sanitized_setup_file_content(file))
end
setup_cfg_files.each do |file|
path = file.name
FileUtils.mkdir_p(Pathname.new(path).dirname)
File.write(path, "[metadata]\nname = sanitized-package\n")
end
end
def install_required_python
return if run_command("pyenv versions").include?("#{python_version}\n")
run_command("pyenv install -s #{python_version}")
run_command("pyenv exec pip install -r "\
"#{NativeHelpers.python_requirements_path}")
end
def sanitized_setup_file_content(file)
@sanitized_setup_file_content ||= {}
return @sanitized_setup_file_content[file.name] if @sanitized_setup_file_content[file.name]
@sanitized_setup_file_content[file.name] =
SetupFileSanitizer.
new(setup_file: file, setup_cfg: setup_cfg(file)).
sanitized_content
end
def setup_cfg(file)
dependency_files.find do |f|
f.name == file.name.sub(/\.py$/, ".cfg")
end
end
def freeze_dependency_requirement(file)
return file.content unless file.name.end_with?(".in")
old_req = dependency.previous_requirements.
find { |r| r[:file] == file.name }
return file.content unless old_req
return file.content if old_req == "==#{dependency.version}"
RequirementReplacer.new(
content: file.content,
dependency_name: dependency.name,
old_requirement: old_req[:requirement],
new_requirement: "==#{dependency.version}"
).updated_content
end
def update_dependency_requirement(file)
return file.content unless file.name.end_with?(".in")
old_req = dependency.previous_requirements.
find { |r| r[:file] == file.name }
new_req = dependency.requirements.
find { |r| r[:file] == file.name }
return file.content unless old_req&.fetch(:requirement)
return file.content if old_req == new_req
RequirementReplacer.new(
content: file.content,
dependency_name: dependency.name,
old_requirement: old_req[:requirement],
new_requirement: new_req[:requirement]
).updated_content
end
def post_process_compiled_file(updated_content, file)
content = replace_header_with_original(updated_content, file.content)
content = remove_new_warnings(content, file.content)
content = update_hashes_if_required(content, file.content)
replace_absolute_file_paths(content, file.content)
end
def replace_header_with_original(updated_content, original_content)
original_header_lines =
original_content.lines.take_while { |l| l.start_with?("#") }
updated_content_lines =
updated_content.lines.drop_while { |l| l.start_with?("#") }
[*original_header_lines, *updated_content_lines].join
end
def replace_absolute_file_paths(updated_content, original_content)
content = updated_content
update_count = 0
original_content.lines.each do |original_line|
next unless original_line.start_with?("-e")
next update_count += 1 if updated_content.include?(original_line)
line_to_update =
updated_content.lines.
select { |l| l.start_with?("-e") }.
at(update_count)
raise "Mismatch in editable requirements!" unless line_to_update
content = content.gsub(line_to_update, original_line)
update_count += 1
end
content
end
def remove_new_warnings(updated_content, original_content)
content = updated_content
content = content.sub(WARNINGS, "\n") if content.match?(WARNINGS) && !original_content.match?(WARNINGS)
if content.match?(UNSAFE_NOTE) &&
!original_content.match?(UNSAFE_NOTE)
content = content.sub(UNSAFE_NOTE, "\n")
end
content
end
def update_hashes_if_required(updated_content, original_content)
deps_to_update =
deps_to_augment_hashes_for(updated_content, original_content)
updated_content_with_hashes = updated_content
deps_to_update.each do |mtch|
updated_string = mtch.to_s.sub(
RequirementParser::HASHES,
package_hashes_for(
name: mtch.named_captures.fetch("name"),
version: mtch.named_captures.fetch("version"),
algorithm: mtch.named_captures.fetch("algorithm")
).sort.join(hash_separator(mtch.to_s))
)
updated_content_with_hashes = updated_content_with_hashes.
gsub(mtch.to_s, updated_string)
end
updated_content_with_hashes
end
def deps_to_augment_hashes_for(updated_content, original_content)
regex = /^#{RequirementParser::INSTALL_REQ_WITH_REQUIREMENT}/
new_matches = []
updated_content.scan(regex) { new_matches << Regexp.last_match }
old_matches = []
original_content.scan(regex) { old_matches << Regexp.last_match }
new_deps = []
changed_hashes_deps = []
new_matches.each do |mtch|
nm = mtch.named_captures["name"]
old_match = old_matches.find { |m| m.named_captures["name"] == nm }
next new_deps << mtch unless old_match
next unless old_match.named_captures["hashes"]
old_count = old_match.named_captures["hashes"].split("--hash").count
new_count = mtch.named_captures["hashes"].split("--hash").count
changed_hashes_deps << mtch if new_count < old_count
end
return [] if changed_hashes_deps.none?
[*new_deps, *changed_hashes_deps]
end
def package_hashes_for(name:, version:, algorithm:)
SharedHelpers.run_helper_subprocess(
command: "pyenv exec python #{NativeHelpers.python_helper_path}",
function: "get_dependency_hash",
args: [name, version, algorithm]
).map { |h| "--hash=#{algorithm}:#{h['hash']}" }
end
def hash_separator(requirement_string)
hash_regex = RequirementParser::HASH
return unless requirement_string.match?(hash_regex)
current_separator =
requirement_string.
match(/#{hash_regex}((?<separator>\s*\\?\s*?)#{hash_regex})*/).
named_captures.fetch("separator")
default_separator =
requirement_string.
match(RequirementParser::HASH).
pre_match.match(/(?<separator>\s*\\?\s*?)\z/).
named_captures.fetch("separator")
current_separator || default_separator
end
def pip_compile_options(filename)
options = ["--build-isolation"]
options += pip_compile_index_options
if (requirements_file = compiled_file_for_filename(filename))
options += pip_compile_options_from_compiled_file(requirements_file)
end
options.join(" ")
end
def pip_compile_options_from_compiled_file(requirements_file)
options = ["--output-file=#{requirements_file.name}"]
options << "--no-emit-index-url" unless requirements_file.content.include?("index-url http")
options << "--generate-hashes" if requirements_file.content.include?("--hash=sha")
options << "--allow-unsafe" if includes_unsafe_packages?(requirements_file.content)
options << "--no-annotate" unless requirements_file.content.include?("# via ")
options << "--no-header" unless requirements_file.content.include?("autogenerated by pip-c")
options << "--pre" if requirements_file.content.include?("--pre")
options << "--strip-extras" if requirements_file.content.include?("--strip-extras")
options
end
def pip_compile_index_options
credentials.
select { |cred| cred["type"] == "python_index" }.
map do |cred|
authed_url = AuthedUrlBuilder.authed_url(credential: cred)
if cred["replaces-base"]
"--index-url=#{authed_url}"
else
"--extra-index-url=#{authed_url}"
end
end
end
def includes_unsafe_packages?(content)
UNSAFE_PACKAGES.any? { |n| content.match?(/^#{Regexp.quote(n)}==/) }
end
def filenames_to_compile
files_from_reqs =
dependency.requirements.
map { |r| r[:file] }.
select { |fn| fn.end_with?(".in") }
files_from_compiled_files =
pip_compile_files.map(&:name).select do |fn|
compiled_file = compiled_file_for_filename(fn)
compiled_file_includes_dependency?(compiled_file)
end
filenames = [*files_from_reqs, *files_from_compiled_files].uniq
order_filenames_for_compilation(filenames)
end
def compiled_file_for_filename(filename)
compiled_file =
compiled_files.
find { |f| f.content.match?(output_file_regex(filename)) }
compiled_file ||=
compiled_files.
find { |f| f.name == filename.gsub(/\.in$/, ".txt") }
compiled_file
end
def output_file_regex(filename)
"--output-file[=\s]+.*\s#{Regexp.escape(filename)}\s*$"
end
def compiled_file_includes_dependency?(compiled_file)
return false unless compiled_file
regex = RequirementParser::INSTALL_REQ_WITH_REQUIREMENT
matches = []
compiled_file.content.scan(regex) { matches << Regexp.last_match }
matches.any? { |m| normalise(m[:name]) == dependency.name }
end
def normalise(name)
NameNormaliser.normalise(name)
end
# If the files we need to update require one another then we need to
# update them in the right order
def order_filenames_for_compilation(filenames)
ordered_filenames = []
while (remaining_filenames = filenames - ordered_filenames).any?
ordered_filenames +=
remaining_filenames.
select do |fn|
unupdated_reqs = requirement_map[fn] - ordered_filenames
(unupdated_reqs & filenames).empty?
end
end
ordered_filenames
end
def requirement_map
child_req_regex = Python::FileFetcher::CHILD_REQUIREMENT_REGEX
@requirement_map ||=
pip_compile_files.each_with_object({}) do |file, req_map|
paths = file.content.scan(child_req_regex).flatten
current_dir = File.dirname(file.name)
req_map[file.name] =
paths.map do |path|
path = File.join(current_dir, path) if current_dir != "."
path = Pathname.new(path).cleanpath.to_path
path = path.gsub(/\.txt$/, ".in")
next if path == file.name
path
end.uniq.compact
end
end
def python_version
@python_version ||=
user_specified_python_version ||
python_version_matching_imputed_requirements ||
PythonVersions::PRE_INSTALLED_PYTHON_VERSIONS.first
end
def user_specified_python_version
return unless python_requirement_parser.user_specified_requirements.any?
user_specified_requirements =
python_requirement_parser.user_specified_requirements.
map { |r| Python::Requirement.requirements_array(r) }
python_version_matching(user_specified_requirements)
end
def python_version_matching_imputed_requirements
compiled_file_python_requirement_markers =
python_requirement_parser.imputed_requirements.map do |r|
Dependabot::Python::Requirement.new(r)
end
python_version_matching(compiled_file_python_requirement_markers)
end
def python_version_matching(requirements)
PythonVersions::SUPPORTED_VERSIONS_TO_ITERATE.find do |version_string|
version = Python::Version.new(version_string)
requirements.all? do |req|
next req.any? { |r| r.satisfied_by?(version) } if req.is_a?(Array)
req.satisfied_by?(version)
end
end
end
def python_requirement_parser
@python_requirement_parser ||=
FileParser::PythonRequirementParser.new(
dependency_files: dependency_files
)
end
def pre_installed_python?(version)
PythonVersions::PRE_INSTALLED_PYTHON_VERSIONS.include?(version)
end
def setup_files
dependency_files.select { |f| f.name.end_with?("setup.py") }
end
def pip_compile_files
dependency_files.select { |f| f.name.end_with?(".in") }
end
def compiled_files
dependency_files.select { |f| f.name.end_with?(".txt") }
end
def setup_cfg_files
dependency_files.select { |f| f.name.end_with?("setup.cfg") }
end
end
# rubocop:enable Metrics/ClassLength
end
end
end