| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | require 'resource' | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | require 'erb' | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 16:23:42 -05:00
										 |  |  | module Patch | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |   def self.create(strip, src, &block) | 
					
						
							| 
									
										
										
										
											2014-07-10 12:38:09 -05:00
										 |  |  |     case strip | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |     when :DATA | 
					
						
							|  |  |  |       DATAPatch.new(:p1) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |     when String | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |       StringPatch.new(:p1, strip) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |     when Symbol | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |       case src | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |       when :DATA | 
					
						
							|  |  |  |         DATAPatch.new(strip) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |       when String | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |         StringPatch.new(strip, src) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |       else | 
					
						
							|  |  |  |         ExternalPatch.new(strip, &block) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       raise ArgumentError, "unexpected value #{strip.inspect} for strip" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   def self.normalize_legacy_patches(list) | 
					
						
							|  |  |  |     patches = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case list | 
					
						
							|  |  |  |     when Hash | 
					
						
							|  |  |  |       list | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |     when Array, String, :DATA | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |       { :p1 => list } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       {} | 
					
						
							|  |  |  |     end.each_pair do |strip, urls| | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |       Array(urls).each do |url| | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |         case url | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |         when :DATA | 
					
						
							|  |  |  |           patch = DATAPatch.new(strip) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |         else | 
					
						
							|  |  |  |           patch = LegacyPatch.new(strip, url) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         patches << patch | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     patches | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  | class EmbeddedPatch | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   attr_writer :owner | 
					
						
							|  |  |  |   attr_reader :strip | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |   def initialize(strip) | 
					
						
							|  |  |  |     @strip = strip | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 16:23:42 -05:00
										 |  |  |   def external? | 
					
						
							|  |  |  |     false | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |   def contents | 
					
						
							|  |  |  |     raise NotImplementedError | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def apply | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |     data = contents.gsub("HOMEBREW_PREFIX", HOMEBREW_PREFIX) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |     IO.popen("/usr/bin/patch -g 0 -f -#{strip}", "w") { |p| p.write(data) } | 
					
						
							|  |  |  |     raise ErrorDuringExecution, "Applying DATA patch failed" unless $?.success? | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:24 -05:00
										 |  |  |   def inspect | 
					
						
							| 
									
										
										
										
											2014-07-01 15:07:06 -05:00
										 |  |  |     "#<#{self.class.name}: #{strip.inspect}>" | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:24 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  | class DATAPatch < EmbeddedPatch | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |   attr_accessor :path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def initialize(strip) | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  |     super | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:06 -05:00
										 |  |  |     @path = nil | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def contents | 
					
						
							|  |  |  |     data = "" | 
					
						
							|  |  |  |     path.open("rb") do |f| | 
					
						
							|  |  |  |       begin | 
					
						
							|  |  |  |         line = f.gets | 
					
						
							|  |  |  |       end until line.nil? || /^__END__$/ === line | 
					
						
							|  |  |  |       data << line while line = f.gets | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     data | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 16:06:07 -05:00
										 |  |  | class StringPatch < EmbeddedPatch | 
					
						
							|  |  |  |   def initialize(strip, str) | 
					
						
							|  |  |  |     super(strip) | 
					
						
							|  |  |  |     @str = str | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def contents | 
					
						
							|  |  |  |     @str | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 16:23:42 -05:00
										 |  |  | class ExternalPatch | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   attr_reader :resource, :strip | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def initialize(strip, &block) | 
					
						
							|  |  |  |     @strip    = strip | 
					
						
							| 
									
										
										
										
											2014-03-15 22:40:14 -05:00
										 |  |  |     @resource = Resource.new("patch", &block) | 
					
						
							| 
									
										
										
										
											2014-07-28 16:23:42 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def external? | 
					
						
							|  |  |  |     true | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def owner= owner | 
					
						
							|  |  |  |     resource.owner   = owner | 
					
						
							| 
									
										
										
										
											2014-03-14 12:38:06 -05:00
										 |  |  |     resource.version = resource.checksum || ERB::Util.url_encode(resource.url) | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def apply | 
					
						
							|  |  |  |     dir = Pathname.pwd | 
					
						
							|  |  |  |     resource.unpack do | 
					
						
							|  |  |  |       # Assumption: the only file in the staging directory is the patch | 
					
						
							|  |  |  |       patchfile = Pathname.pwd.children.first | 
					
						
							|  |  |  |       safe_system "/usr/bin/patch", "-g", "0", "-f", "-d", dir, "-#{strip}", "-i", patchfile | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 16:12:31 -05:00
										 |  |  |   def url | 
					
						
							|  |  |  |     resource.url | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def fetch | 
					
						
							|  |  |  |     resource.fetch | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def verify_download_integrity(fn) | 
					
						
							|  |  |  |     resource.verify_download_integrity(fn) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def cached_download | 
					
						
							|  |  |  |     resource.cached_download | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def clear_cache | 
					
						
							|  |  |  |     resource.clear_cache | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:24 -05:00
										 |  |  |   def inspect | 
					
						
							| 
									
										
										
										
											2014-07-01 15:07:06 -05:00
										 |  |  |     "#<#{self.class.name}: #{strip.inspect} #{url.inspect}>" | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:24 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Legacy patches have no checksum and are not cached | 
					
						
							|  |  |  | class LegacyPatch < ExternalPatch | 
					
						
							|  |  |  |   def initialize(strip, url) | 
					
						
							|  |  |  |     super(strip) | 
					
						
							|  |  |  |     resource.url = url | 
					
						
							| 
									
										
										
										
											2014-03-18 14:24:07 -05:00
										 |  |  |     resource.download_strategy = CurlDownloadStrategy | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def fetch | 
					
						
							| 
									
										
										
										
											2014-03-14 16:12:31 -05:00
										 |  |  |     clear_cache | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |     super | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def verify_download_integrity(fn) | 
					
						
							|  |  |  |     # no-op | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def apply | 
					
						
							|  |  |  |     super | 
					
						
							|  |  |  |   ensure | 
					
						
							| 
									
										
										
										
											2014-03-14 16:12:31 -05:00
										 |  |  |     clear_cache | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |