cleaner: remove RECORD and modify INSTALLER

According to [Python specification][1], we should remove `RECORD` file
to prevent changes to installed formula files via other tools, e.g. pip.
This also improves chances of generating an `all` bottle as it avoids
diff due to checksums of HOMEBREW_PREFIX present files. Also modify
`INSTALLER` file to indicate that brew is managing the Python package.

[1]: https://packaging.python.org/en/latest/specifications/recording-installed-packages/#intentionally-preventing-changes-to-installed-packages

Signed-off-by: Michael Cho <michael@michaelcho.dev>
This commit is contained in:
Michael Cho 2024-03-08 13:23:25 -05:00
parent cd1f040949
commit 74f310577f
No known key found for this signature in database
GPG Key ID: 55E85E28A7CD1E85
2 changed files with 60 additions and 12 deletions

View File

@ -58,7 +58,7 @@ class Cleaner
end end
rewrite_shebangs rewrite_shebangs
remove_pip_direct_url clean_python_metadata
prune prune
end end
@ -165,25 +165,27 @@ class Cleaner
end end
end end
# Remove non-reproducible pip direct_url.json which records the /tmp build directory # Remove non-reproducible pip direct_url.json which records the /tmp build directory.
# Remove RECORD files to prevent changes to the installed Python package.
# Modify INSTALLER to provide information that files are managed by brew.
#
# @see https://packaging.python.org/en/latest/specifications/recording-installed-packages/
sig { void } sig { void }
def remove_pip_direct_url def clean_python_metadata
basepath = @formula.prefix.realpath basepath = @formula.prefix.realpath
basepath.find do |path| basepath.find do |path|
Find.prune if @formula.skip_clean?(path) Find.prune if @formula.skip_clean?(path)
next if path.directory? || path.symlink? next if path.directory? || path.symlink?
next if path.basename.to_s != "direct_url.json"
next if path.parent.extname != ".dist-info" next if path.parent.extname != ".dist-info"
odebug "Removing #{path}" case path.basename.to_s
path.unlink when "direct_url.json", "RECORD"
observe_file_removal path
record = path.parent/"RECORD" when "INSTALLER"
next unless record.file? odebug "Modifying #{path} contents from #{path.read.chomp} to brew"
path.atomic_write("brew\n")
odebug "Modifying #{record}" end
@formula.inreplace record, %r{^.*/direct_url\.json,.*$\n?}, "", false
end end
end end
end end

View File

@ -155,6 +155,52 @@ RSpec.describe Cleaner do
expect(arch_file).not_to exist expect(arch_file).not_to exist
expect(name_file).to exist expect(name_file).to exist
end end
it "removes '*.dist-info/direct_url.json' files" do
dir = f.lib/"python3.12/site-packages/test.dist-info"
file = dir/"direct_url.json"
unrelated_file = dir/"METADATA"
unrelated_dir_file = f.lib/"direct_url.json"
dir.mkpath
touch file
touch unrelated_file
touch unrelated_dir_file
cleaner.clean
expect(file).not_to exist
expect(unrelated_file).to exist
expect(unrelated_dir_file).to exist
end
it "removes '*.dist-info/RECORD' files" do
dir = f.lib/"python3.12/site-packages/test.dist-info"
file = dir/"RECORD"
unrelated_file = dir/"METADATA"
unrelated_dir_file = f.lib/"RECORD"
dir.mkpath
touch file
touch unrelated_file
touch unrelated_dir_file
cleaner.clean
expect(file).not_to exist
expect(unrelated_file).to exist
expect(unrelated_dir_file).to exist
end
it "modifies '*.dist-info/INSTALLER' files" do
file = f.lib/"python3.12/site-packages/test.dist-info/INSTALLER"
file.dirname.mkpath
file.write "pip\n"
cleaner.clean
expect(file.read).to eq "brew\n"
end
end end
describe "::skip_clean" do describe "::skip_clean" do