| 
									
										
										
										
											2015-03-13 20:59:17 -04:00
										 |  |  | require "fileutils" | 
					
						
							|  |  |  | require "tmpdir" | 
					
						
							| 
									
										
										
										
											2016-02-04 06:26:23 -08:00
										 |  |  | require "etc" | 
					
						
							| 
									
										
										
										
											2012-03-04 16:48:00 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  | # Homebrew extends Ruby's `FileUtils` to make our code more readable. | 
					
						
							| 
									
										
										
										
											2017-01-20 12:30:47 +00:00
										 |  |  | # @see http://ruby-doc.org/stdlib-2.0.0/libdoc/fileutils/rdoc/FileUtils.html Ruby's FileUtils API | 
					
						
							| 
									
										
										
										
											2014-06-09 21:36:36 -05:00
										 |  |  | module FileUtils | 
					
						
							| 
									
										
										
										
											2012-03-04 16:48:00 -08:00
										 |  |  |   # Create a temporary directory then yield. When the block returns, | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |   # 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 | 
					
						
							|  |  |  |   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 | 
					
						
							| 
									
										
										
										
											2015-03-10 23:26:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |     # 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 | 
					
						
							| 
									
										
										
										
											2016-02-04 06:26:23 -08:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Instructs this Mktemp to retain the staged files | 
					
						
							|  |  |  |     def retain! | 
					
						
							|  |  |  |       @retain = true | 
					
						
							| 
									
										
										
										
											2016-02-04 06:26:23 -08:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |     # True if the staged temporary files should be retained | 
					
						
							|  |  |  |     def retain? | 
					
						
							|  |  |  |       @retain | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Instructs this Mktemp to not emit messages when retention is triggered | 
					
						
							|  |  |  |     def quiet! | 
					
						
							|  |  |  |       @quiet = true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-03-13 20:59:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |     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? | 
					
						
							| 
									
										
										
										
											2016-09-11 17:53:00 +01:00
										 |  |  |         HOMEBREW_BREW_FILE.stat.gid | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         Process.gid | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-03-13 20:59:17 -04:00
										 |  |  |       begin | 
					
						
							| 
									
										
										
										
											2016-08-18 14:35:39 +08:00
										 |  |  |         chown(nil, group_id, tmpdir) | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |       rescue Errno::EPERM | 
					
						
							| 
									
										
										
										
											2016-05-03 15:24:32 +09:00
										 |  |  |         opoo "Failed setting group \"#{Etc.getgrgid(group_id).name}\" on #{tmpdir}" | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       begin | 
					
						
							|  |  |  |         Dir.chdir(tmpdir) { yield self } | 
					
						
							| 
									
										
										
										
											2015-03-13 20:59:17 -04:00
										 |  |  |       ensure | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |         ignore_interrupts { rm_rf(tmpdir) } unless retain? | 
					
						
							| 
									
										
										
										
											2015-03-13 20:59:17 -04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-03-10 23:26:22 -04:00
										 |  |  |     ensure | 
					
						
							| 
									
										
										
										
											2016-04-10 22:53:56 -04:00
										 |  |  |       if retain? && !@tmpdir.nil? && !@quiet | 
					
						
							|  |  |  |         ohai "Kept temporary files" | 
					
						
							|  |  |  |         puts "Temporary files retained at #{@tmpdir}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-03-10 23:26:22 -04:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-03-04 16:48:00 -08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |   # @private | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:48 +02:00
										 |  |  |   alias old_mkdir mkdir | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # A version of mkdir that also changes to that folder in a block. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def mkdir(name, &_block) | 
					
						
							| 
									
										
										
										
											2016-09-20 11:50:27 +01:00
										 |  |  |     mkdir_p(name) | 
					
						
							| 
									
										
										
										
											2016-09-23 22:02:23 +02:00
										 |  |  |     return unless block_given? | 
					
						
							|  |  |  |     chdir name do | 
					
						
							|  |  |  |       yield | 
					
						
							| 
									
										
										
										
											2012-03-04 16:48:00 -08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-06-09 21:36:36 -05:00
										 |  |  |   module_function :mkdir | 
					
						
							| 
									
										
										
										
											2012-03-04 16:48:00 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |   # Run `scons` using a Homebrew-installed version rather than whatever is in the `PATH`. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def scons(*args) | 
					
						
							| 
									
										
										
										
											2014-03-14 16:41:57 -05:00
										 |  |  |     system Formulary.factory("scons").opt_bin/"scons", *args | 
					
						
							| 
									
										
										
										
											2014-02-28 07:20:52 -08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |   # Run the `rake` from the `ruby` Homebrew is using rather than whatever is in the `PATH`. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def rake(*args) | 
					
						
							|  |  |  |     system RUBY_BIN/"rake", *args | 
					
						
							| 
									
										
										
										
											2013-02-02 11:24:55 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-26 20:06:11 -07:00
										 |  |  |   # Run `make` 3.81 or newer. | 
					
						
							|  |  |  |   # Uses the system make on Leopard and newer, and the | 
					
						
							|  |  |  |   # path to the actually-installed make on Tiger or older. | 
					
						
							|  |  |  |   def make(*args) | 
					
						
							|  |  |  |     if Utils.popen_read("/usr/bin/make", "--version").match(/Make (\d\.\d+)/)[1] > "3.80" | 
					
						
							|  |  |  |       system "/usr/bin/make", *args | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       make = Formula["make"].opt_bin/"make" | 
					
						
							|  |  |  |       make_path = make.exist? ? make.to_s : (Formula["make"].opt_bin/"gmake").to_s | 
					
						
							|  |  |  |       system make_path, *args | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |   if method_defined?(:ruby) | 
					
						
							|  |  |  |     # @private | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:48 +02:00
										 |  |  |     alias old_ruby ruby | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Run the `ruby` Homebrew is using rather than whatever is in the `PATH`. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def ruby(*args) | 
					
						
							| 
									
										
										
										
											2013-03-10 17:02:06 +00:00
										 |  |  |     system RUBY_PATH, *args | 
					
						
							| 
									
										
										
										
											2013-02-02 11:24:55 -06:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-02-27 21:47:38 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |   # Run `xcodebuild` without Homebrew's compiler environment variables set. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def xcodebuild(*args) | 
					
						
							| 
									
										
										
										
											2014-02-27 21:47:38 -06:00
										 |  |  |     removed = ENV.remove_cc_etc | 
					
						
							|  |  |  |     system "xcodebuild", *args | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     ENV.update(removed) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-03-04 16:48:00 -08:00
										 |  |  | end |