aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/extend/fileutils.rb
diff options
context:
space:
mode:
authorAndrew Janke2016-04-10 22:53:56 -0400
committerAndrew Janke2016-04-18 12:23:08 -0400
commitacc9a7ca8554bc2413dee2d6d0f407b3a59c628c (patch)
tree4fb68e17149a73d9d123d9538e2d5fdccdd0ffd9 /Library/Homebrew/extend/fileutils.rb
parent0e8140b012181413438002b65290c84284721694 (diff)
downloadbrew-acc9a7ca8554bc2413dee2d6d0f407b3a59c628c.tar.bz2
brew test, install, update-test: add --keep-tmp option
Also enables sandbox for --interactive and --debug use of install and test, using automatic retention. Closes #66. Signed-off-by: Andrew Janke <andrew@apjanke.net>
Diffstat (limited to 'Library/Homebrew/extend/fileutils.rb')
-rw-r--r--Library/Homebrew/extend/fileutils.rb93
1 files changed, 67 insertions, 26 deletions
diff --git a/Library/Homebrew/extend/fileutils.rb b/Library/Homebrew/extend/fileutils.rb
index ee6735e27..af17d4eff 100644
--- a/Library/Homebrew/extend/fileutils.rb
+++ b/Library/Homebrew/extend/fileutils.rb
@@ -6,42 +6,83 @@ require "etc"
# @see http://ruby-doc.org/stdlib-1.8.7/libdoc/fileutils/rdoc/FileUtils.html Ruby's FileUtils API
module FileUtils
# Create a temporary directory then yield. When the block returns,
- # recursively delete the temporary directory.
- def mktemp(prefix = name)
- prev = pwd
- tmp = Dir.mktmpdir(prefix, 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
+ # recursively delete the temporary directory. Passing opts[:retain]
+ # or calling `do |staging| ... staging.retain!` in the block will skip
+ # the deletion and retain the temporary directory's contents.
+ def mktemp(prefix = name, opts = {})
+ Mktemp.new(prefix, opts).run do |staging|
+ yield staging
end
- begin
- # group_id.to_s makes OS X 10.6.7 (ruby-1.8.7-p174) and earlier happy.
- chown(nil, group_id.to_s, tmp)
- rescue Errno::EPERM
- opoo "Failed setting group \"#{Etc.getgrgid(group_id).name}\" on #{tmp}"
+ end
+
+ module_function :mktemp
+
+ # Performs mktemp's functionality, and tracks the results.
+ # Each instance is only intended to be used once.
+ class Mktemp
+ include FileUtils
+
+ # Path to the tmpdir used in this run, as a Pathname.
+ attr_reader :tmpdir
+
+ def initialize(prefix = name, opts = {})
+ @prefix = prefix
+ @retain = opts[:retain]
+ @quiet = false
+ end
+
+ # Instructs this Mktemp to retain the staged files
+ def retain!
+ @retain = true
+ end
+
+ # True if the staged temporary files should be retained
+ def retain?
+ @retain
end
- begin
- cd(tmp)
+ # Instructs this Mktemp to not emit messages when retention is triggered
+ def quiet!
+ @quiet = true
+ end
+
+ def to_s
+ "[Mktemp: #{tmpdir} retain=#{@retain} quiet=#{@quiet}]"
+ end
+ def run
+ @tmpdir = Pathname.new(Dir.mktmpdir("#{@prefix}-", 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
- yield
+ # group_id.to_s makes OS X 10.6.7 (ruby-1.8.7-p174) and earlier happy.
+ chown(nil, group_id.to_s, tmpdir)
+ rescue Errno::EPERM
+ opoo "Failed setting group \"#{Etc.getgrgid(group_id).name}\" on #{tmp}"
+ end
+
+ begin
+ Dir.chdir(tmpdir) { yield self }
ensure
- cd(prev)
+ ignore_interrupts { rm_rf(tmpdir) } unless retain?
end
ensure
- ignore_interrupts { rm_rf(tmp) }
+ if retain? && !@tmpdir.nil? && !@quiet
+ ohai "Kept temporary files"
+ puts "Temporary files retained at #{@tmpdir}"
+ end
end
end
- module_function :mktemp
# @private
alias_method :old_mkdir, :mkdir