Sources now retained in Caches/Homebrew/Sources

Full path is ~/Library/Caches/Homebrew/Sources

This creates a brand new directory for each build, but leaves previous.
This commit is contained in:
Lukas Oberhuber 2022-07-31 20:33:25 +01:00
parent 4b0d52ef62
commit 8b1eb32e99
3 changed files with 10 additions and 88 deletions

View File

@ -1,84 +0,0 @@
# typed: false
# frozen_string_literal: true
# Performs {Formula#mktemp}'s functionality, and tracks the results.
# Each instance is only intended to be used once.
class Mksource
extend T::Sig
include FileUtils
# Path to the tmpdir used in this run, as a {Pathname}.
attr_reader :tmpdir
def initialize(prefix, opts = {})
@prefix = prefix
@retain = opts[:retain]
@quiet = false
end
# Instructs this {Mksource} to retain the staged files.
sig { void }
def retain!
@retain = true
end
# True if the staged temporary files should be retained.
def retain?
@retain
end
# Instructs this Mksource to not emit messages when retention is triggered.
sig { void }
def quiet!
@quiet = true
end
sig { returns(String) }
def to_s
"[Mksource: #{tmpdir} retain=#{@retain} quiet=#{@quiet}]"
end
def run
@tmpdir = Pathname.new(Dir.mktmpdir("#{@prefix.tr "@", "AT"}-", HOMEBREW_TEMP))
# Make sure files inside the temporary directory have the same group as the
# brew instance.
#
# Reference from `man 2 open`
# > When a new file is created, it is given the group of the directory which
# contains it.
group_id = if HOMEBREW_BREW_FILE.grpowned?
HOMEBREW_BREW_FILE.stat.gid
else
Process.gid
end
begin
chown(nil, group_id, tmpdir)
rescue Errno::EPERM
opoo "Failed setting group \"#{Etc.getgrgid(group_id).name}\" on #{tmpdir}"
end
begin
Dir.chdir(tmpdir) { yield self }
ensure
ignore_interrupts { chmod_rm_rf(tmpdir) } unless retain?
end
ensure
ohai "Temporary files retained at:", @tmpdir.to_s if retain? && !@tmpdir.nil? && !@quiet
end
private
def chmod_rm_rf(path)
if path.directory? && !path.symlink?
chmod("u+rw", path) if path.owned? # Need permissions in order to see the contents
path.children.each { |child| chmod_rm_rf(child) }
rmdir(path)
else
rm_f(path)
end
rescue
nil # Just skip this directory.
end
end

View File

@ -13,7 +13,8 @@ class Mktemp
def initialize(prefix, opts = {})
@prefix = prefix
@retain = opts[:retain]
@retain_in_sources = opts[:retain_in_sources]
@retain = opts[:retain] || @retain_in_sources
@quiet = false
end
@ -40,7 +41,13 @@ class Mktemp
end
def run
@tmpdir = Pathname.new(Dir.mktmpdir("#{@prefix.tr "@", "AT"}-", HOMEBREW_TEMP))
if @retain_in_sources
root = "#{HOMEBREW_CACHE}/Sources"
FileUtils.mkdir_p root
else
root = HOMEBREW_TEMP
end
@tmpdir = Pathname.new(Dir.mktmpdir("#{@prefix.tr "@", "AT"}-", root))
# Make sure files inside the temporary directory have the same group as the
# brew instance.

View File

@ -5,7 +5,6 @@ require "download_strategy"
require "checksum"
require "version"
require "mktemp"
require "mksource"
require "livecheck"
require "extend/on_system"
@ -237,7 +236,7 @@ class Resource
protected
def stage_resource(prefix, debug_symbols: false, &block)
debug_symbols ? Mksource.new(prefix).run(&block) : Mktemp.new(prefix).run(&block)
Mktemp.new(prefix, retain_in_sources: debug_symbols).run(&block)
end
private