2009-07-31 02:51:17 +01:00
|
|
|
require 'pathname'
|
2012-05-25 23:44:11 -05:00
|
|
|
require 'mach'
|
2009-07-31 02:51:17 +01:00
|
|
|
|
|
|
|
# we enhance pathname to make our code more readable
|
|
|
|
class Pathname
|
2012-05-25 23:44:11 -05:00
|
|
|
include MachO
|
|
|
|
|
2012-07-05 20:29:31 -05:00
|
|
|
BOTTLE_EXTNAME_RX = /(\.[a-z]+\.bottle\.(\d+\.)?tar\.gz)$/
|
|
|
|
OLD_BOTTLE_EXTNAME_RX = /((\.[a-z]+)?[\.-]bottle\.tar\.gz)$/
|
|
|
|
|
2012-02-09 18:43:47 -08:00
|
|
|
def install *sources
|
2012-02-17 23:07:16 -08:00
|
|
|
results = []
|
2012-02-09 18:43:47 -08:00
|
|
|
sources.each do |src|
|
|
|
|
case src
|
|
|
|
when Array
|
2012-03-30 22:34:42 -07:00
|
|
|
if src.empty?
|
2012-03-30 23:02:32 -07:00
|
|
|
opoo "tried to install empty array to #{self}"
|
2012-03-30 22:34:42 -07:00
|
|
|
return []
|
|
|
|
end
|
2012-02-17 23:07:16 -08:00
|
|
|
src.each {|s| results << install_p(s) }
|
2012-02-09 18:43:47 -08:00
|
|
|
when Hash
|
2012-03-30 22:34:42 -07:00
|
|
|
if src.empty?
|
2012-03-30 23:02:32 -07:00
|
|
|
opoo "tried to install empty hash to #{self}"
|
2012-03-30 22:34:42 -07:00
|
|
|
return []
|
|
|
|
end
|
2012-02-17 23:07:16 -08:00
|
|
|
src.each {|s, new_basename| results << install_p(s, new_basename) }
|
2012-02-09 18:43:47 -08:00
|
|
|
else
|
2012-02-17 23:07:16 -08:00
|
|
|
results << install_p(src)
|
2012-02-09 18:43:47 -08:00
|
|
|
end
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
2012-02-17 23:07:16 -08:00
|
|
|
return results
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
2010-02-19 13:13:42 +00:00
|
|
|
|
|
|
|
def install_p src, new_basename = nil
|
|
|
|
if new_basename
|
|
|
|
new_basename = File.basename(new_basename) # rationale: see Pathname.+
|
|
|
|
dst = self+new_basename
|
2012-02-08 21:58:46 -08:00
|
|
|
return_value = Pathname.new(dst)
|
2010-02-19 13:13:42 +00:00
|
|
|
else
|
|
|
|
dst = self
|
2010-03-08 23:42:01 -08:00
|
|
|
return_value = self+File.basename(src)
|
2010-02-19 13:13:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
src = src.to_s
|
|
|
|
dst = dst.to_s
|
|
|
|
|
|
|
|
# if it's a symlink, don't resolve it to a file because if we are moving
|
|
|
|
# files one by one, it's likely we will break the symlink by moving what
|
|
|
|
# it points to before we move it
|
|
|
|
# and also broken symlinks are not the end of the world
|
|
|
|
raise "#{src} does not exist" unless File.symlink? src or File.exist? src
|
|
|
|
|
|
|
|
mkpath
|
|
|
|
if File.symlink? src
|
|
|
|
# we use the BSD mv command because FileUtils copies the target and
|
|
|
|
# not the link! I'm beginning to wish I'd used Python quite honestly!
|
|
|
|
raise unless Kernel.system 'mv', src, dst
|
|
|
|
else
|
|
|
|
# we mv when possible as it is faster and you should only be using
|
|
|
|
# this function when installing from the temporary build directory
|
|
|
|
FileUtils.mv src, dst
|
|
|
|
end
|
|
|
|
|
2010-03-08 23:42:01 -08:00
|
|
|
return return_value
|
2010-02-19 13:13:42 +00:00
|
|
|
end
|
|
|
|
|
2012-02-12 10:36:16 -08:00
|
|
|
# Creates symlinks to sources in this folder.
|
|
|
|
def install_symlink *sources
|
2012-02-17 23:07:16 -08:00
|
|
|
results = []
|
2012-02-12 10:36:16 -08:00
|
|
|
sources.each do |src|
|
|
|
|
case src
|
|
|
|
when Array
|
2012-02-17 23:07:16 -08:00
|
|
|
src.each {|s| results << install_symlink_p(s) }
|
2012-02-12 10:36:16 -08:00
|
|
|
when Hash
|
2012-02-17 23:07:16 -08:00
|
|
|
src.each {|s, new_basename| results << install_symlink_p(s, new_basename) }
|
2012-02-12 10:36:16 -08:00
|
|
|
else
|
2012-02-17 23:07:16 -08:00
|
|
|
results << install_symlink_p(src)
|
2012-02-12 10:36:16 -08:00
|
|
|
end
|
|
|
|
end
|
2012-02-17 23:07:16 -08:00
|
|
|
return results
|
2012-02-12 10:36:16 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def install_symlink_p src, new_basename = nil
|
|
|
|
if new_basename.nil?
|
|
|
|
dst = self+File.basename(src)
|
|
|
|
else
|
|
|
|
dst = self+File.basename(new_basename)
|
|
|
|
end
|
|
|
|
|
|
|
|
src = src.to_s
|
|
|
|
dst = dst.to_s
|
|
|
|
|
|
|
|
mkpath
|
|
|
|
FileUtils.ln_s src, dst
|
|
|
|
|
|
|
|
return dst
|
|
|
|
end
|
|
|
|
|
2009-08-31 22:34:42 -06:00
|
|
|
# we assume this pathname object is a file obviously
|
|
|
|
def write content
|
2009-09-02 14:31:28 +01:00
|
|
|
raise "Will not overwrite #{to_s}" if exist? and not ARGV.force?
|
2009-08-31 22:34:42 -06:00
|
|
|
dirname.mkpath
|
|
|
|
File.open(self, 'w') {|f| f.write content }
|
|
|
|
end
|
2009-07-31 02:51:17 +01:00
|
|
|
|
2012-03-16 12:58:39 +00:00
|
|
|
# NOTE always overwrites
|
|
|
|
def atomic_write content
|
|
|
|
require 'tempfile'
|
|
|
|
tf = Tempfile.new(self.basename.to_s)
|
|
|
|
tf.write(content)
|
|
|
|
tf.close
|
|
|
|
FileUtils.mv tf.path, self.to_s
|
|
|
|
end
|
|
|
|
|
2009-07-31 02:51:17 +01:00
|
|
|
def cp dst
|
|
|
|
if file?
|
|
|
|
FileUtils.cp to_s, dst
|
|
|
|
else
|
|
|
|
FileUtils.cp_r to_s, dst
|
|
|
|
end
|
2009-09-02 14:31:28 +01:00
|
|
|
return dst
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
|
|
|
|
2012-01-12 20:08:35 -06:00
|
|
|
# extended to support common double extensions
|
2012-04-30 14:17:56 +10:00
|
|
|
alias extname_old extname
|
2009-07-31 02:51:17 +01:00
|
|
|
def extname
|
2012-07-05 20:29:31 -05:00
|
|
|
BOTTLE_EXTNAME_RX.match to_s
|
|
|
|
return $1 if $1
|
|
|
|
OLD_BOTTLE_EXTNAME_RX.match to_s
|
|
|
|
return $1 if $1
|
2012-02-08 21:08:18 -08:00
|
|
|
/(\.(tar|cpio)\.(gz|bz2|xz|Z))$/.match to_s
|
2009-07-31 02:51:17 +01:00
|
|
|
return $1 if $1
|
|
|
|
return File.extname(to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
# for filetypes we support, basename without extension
|
|
|
|
def stem
|
|
|
|
return File.basename(to_s, extname)
|
|
|
|
end
|
|
|
|
|
2009-08-10 16:48:30 +01:00
|
|
|
# I don't trust the children.length == 0 check particularly, not to mention
|
|
|
|
# it is slow to enumerate the whole directory just to see if it is empty,
|
|
|
|
# instead rely on good ol' libc and the filesystem
|
|
|
|
def rmdir_if_possible
|
|
|
|
rmdir
|
2009-09-02 14:31:28 +01:00
|
|
|
true
|
2009-08-10 16:48:30 +01:00
|
|
|
rescue SystemCallError => e
|
2012-08-10 16:08:50 -04:00
|
|
|
raise unless e.errno == Errno::ENOTEMPTY::Errno or e.errno == Errno::EACCES::Errno or e.errno == Errno::ENOENT::Errno
|
2009-09-02 14:31:28 +01:00
|
|
|
false
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
2012-01-22 22:12:39 +01:00
|
|
|
|
2009-08-10 16:48:30 +01:00
|
|
|
def chmod_R perms
|
|
|
|
require 'fileutils'
|
|
|
|
FileUtils.chmod_R perms, to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def abv
|
2009-09-02 14:31:28 +01:00
|
|
|
out=''
|
2010-09-02 11:47:18 -04:00
|
|
|
n=`find #{to_s} -type f ! -name .DS_Store | wc -l`.to_i
|
2009-09-02 14:31:28 +01:00
|
|
|
out<<"#{n} files, " if n > 1
|
2010-01-03 02:59:16 +00:00
|
|
|
out<<`/usr/bin/du -hd0 #{to_s} | cut -d"\t" -f1`.strip
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
2009-07-31 02:51:17 +01:00
|
|
|
def version
|
2012-07-09 23:24:27 -05:00
|
|
|
require 'version'
|
2012-07-10 16:01:02 -05:00
|
|
|
Version.parse(self)
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
2012-01-22 22:12:39 +01:00
|
|
|
|
2012-04-29 11:45:46 -07:00
|
|
|
def compression_type
|
2012-05-03 20:40:04 -07:00
|
|
|
# Don't treat jars or wars as compressed
|
2012-04-29 11:45:46 -07:00
|
|
|
return nil if self.extname == '.jar'
|
2012-05-03 20:40:04 -07:00
|
|
|
return nil if self.extname == '.war'
|
2012-04-29 11:45:46 -07:00
|
|
|
|
|
|
|
# OS X installer package
|
|
|
|
return :pkg if self.extname == '.pkg'
|
|
|
|
|
2012-05-03 20:31:00 -07:00
|
|
|
# Get enough of the file to detect common file types
|
|
|
|
# POSIX tar magic has a 257 byte offset
|
2012-04-29 11:45:46 -07:00
|
|
|
magic_bytes = nil
|
2012-05-03 20:31:00 -07:00
|
|
|
File.open(self) { |f| magic_bytes = f.read(262) }
|
2012-04-29 11:45:46 -07:00
|
|
|
|
|
|
|
# magic numbers stolen from /usr/share/file/magic/
|
|
|
|
case magic_bytes
|
|
|
|
when /^PK\003\004/ then :zip
|
|
|
|
when /^\037\213/ then :gzip
|
|
|
|
when /^BZh/ then :bzip2
|
|
|
|
when /^\037\235/ then :compress
|
2012-05-03 20:31:00 -07:00
|
|
|
when /^.{257}ustar/ then :tar
|
2012-04-29 11:45:46 -07:00
|
|
|
when /^\xFD7zXZ\x00/ then :xz
|
|
|
|
when /^Rar!/ then :rar
|
|
|
|
else
|
2012-08-22 09:33:10 -04:00
|
|
|
# This code so that bad-tarballs and zips produce good error messages
|
|
|
|
# when they don't unarchive properly.
|
|
|
|
case extname
|
|
|
|
when ".tar.gz", ".tgz", ".tar.bz2", ".tbz" then :tar
|
|
|
|
when ".zip" then :zip
|
|
|
|
end
|
2012-04-29 11:45:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-25 23:44:11 -05:00
|
|
|
def text_executable?
|
2012-09-27 17:03:43 -05:00
|
|
|
%r[^#!\s*\S+] === open('r') { |f| f.read(1024) }
|
2012-05-25 23:44:11 -05:00
|
|
|
end
|
|
|
|
|
2010-03-27 10:41:24 -05:00
|
|
|
def incremental_hash(hasher)
|
|
|
|
incr_hash = hasher.new
|
2010-03-23 19:56:20 -05:00
|
|
|
self.open('r') do |f|
|
2010-03-27 10:41:24 -05:00
|
|
|
while(buf = f.read(1024))
|
|
|
|
incr_hash << buf
|
2010-03-23 19:56:20 -05:00
|
|
|
end
|
|
|
|
end
|
2010-03-27 10:41:24 -05:00
|
|
|
incr_hash.hexdigest
|
|
|
|
end
|
|
|
|
|
|
|
|
def md5
|
|
|
|
require 'digest/md5'
|
|
|
|
incremental_hash(Digest::MD5)
|
|
|
|
end
|
2012-01-22 22:12:39 +01:00
|
|
|
|
2010-03-27 10:41:24 -05:00
|
|
|
def sha1
|
|
|
|
require 'digest/sha1'
|
|
|
|
incremental_hash(Digest::SHA1)
|
|
|
|
end
|
2012-01-22 22:12:39 +01:00
|
|
|
|
2010-03-27 10:41:24 -05:00
|
|
|
def sha2
|
|
|
|
require 'digest/sha2'
|
|
|
|
incremental_hash(Digest::SHA2)
|
2009-12-30 18:56:46 +00:00
|
|
|
end
|
2012-06-18 19:58:35 -05:00
|
|
|
alias_method :sha256, :sha2
|
|
|
|
|
|
|
|
def verify_checksum expected
|
|
|
|
raise ChecksumMissingError if expected.nil? or expected.empty?
|
|
|
|
actual = Checksum.new(expected.hash_type, send(expected.hash_type).downcase)
|
|
|
|
raise ChecksumMismatchError.new(expected, actual) unless expected == actual
|
|
|
|
end
|
2009-12-30 18:56:46 +00:00
|
|
|
|
2009-09-30 22:17:46 -05:00
|
|
|
if '1.9' <= RUBY_VERSION
|
|
|
|
alias_method :to_str, :to_s
|
|
|
|
end
|
2010-02-27 12:29:45 +00:00
|
|
|
|
|
|
|
def cd
|
|
|
|
Dir.chdir(self){ yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
def subdirs
|
|
|
|
children.select{ |child| child.directory? }
|
|
|
|
end
|
2010-06-14 11:56:27 -07:00
|
|
|
|
2010-07-25 12:07:35 -07:00
|
|
|
def resolved_path
|
|
|
|
self.symlink? ? dirname+readlink : self
|
|
|
|
end
|
|
|
|
|
2010-06-14 11:56:27 -07:00
|
|
|
def resolved_path_exists?
|
|
|
|
(dirname+readlink).exist?
|
|
|
|
end
|
2010-08-08 09:13:27 -07:00
|
|
|
|
2010-09-12 21:52:30 +01:00
|
|
|
# perhaps confusingly, this Pathname object becomes the symlink pointing to
|
|
|
|
# the src paramter.
|
2010-08-15 17:17:26 -07:00
|
|
|
def make_relative_symlink src
|
2012-02-21 12:45:45 +00:00
|
|
|
src = Pathname.new(src) unless src.kind_of? Pathname
|
|
|
|
|
2010-08-15 17:17:26 -07:00
|
|
|
self.dirname.mkpath
|
|
|
|
Dir.chdir self.dirname do
|
2012-03-06 13:02:10 +00:00
|
|
|
# NOTE only system ln -s will create RELATIVE symlinks
|
2012-03-19 00:15:40 +00:00
|
|
|
quiet_system 'ln', '-s', src.relative_path_from(self.dirname), self.basename
|
|
|
|
if not $?.success?
|
2012-04-08 23:27:34 -05:00
|
|
|
if self.exist?
|
|
|
|
raise <<-EOS.undent
|
|
|
|
Could not symlink file: #{src.expand_path}
|
|
|
|
Target #{self} already exists. You may need to delete it.
|
2012-06-17 16:54:20 -05:00
|
|
|
To force the link and delete this file, do:
|
2012-10-20 20:54:11 -05:00
|
|
|
brew link --overwrite formula_name
|
2012-06-17 16:54:20 -05:00
|
|
|
|
|
|
|
To list all files that would be deleted:
|
2012-10-20 20:54:11 -05:00
|
|
|
brew link --overwrite --dry-run formula_name
|
2012-04-08 23:27:34 -05:00
|
|
|
EOS
|
2012-07-28 11:36:08 -07:00
|
|
|
elsif !dirname.writable_real?
|
2012-04-08 23:27:34 -05:00
|
|
|
raise <<-EOS.undent
|
|
|
|
Could not symlink file: #{src.expand_path}
|
|
|
|
#{dirname} is not writable. You should change its permissions.
|
|
|
|
EOS
|
|
|
|
else
|
|
|
|
raise <<-EOS.undent
|
|
|
|
Could not symlink file: #{src.expand_path}
|
|
|
|
#{self} may already exist.
|
|
|
|
#{dirname} may not be writable.
|
|
|
|
EOS
|
|
|
|
end
|
2012-03-19 00:15:40 +00:00
|
|
|
end
|
2010-08-15 17:17:26 -07:00
|
|
|
end
|
|
|
|
end
|
2010-09-12 21:52:30 +01:00
|
|
|
|
|
|
|
def / that
|
|
|
|
join that.to_s
|
|
|
|
end
|
2010-05-10 01:10:49 +01:00
|
|
|
|
|
|
|
def ensure_writable
|
2011-06-16 17:38:52 +01:00
|
|
|
saved_perms = nil
|
2012-07-28 11:36:08 -07:00
|
|
|
unless writable_real?
|
2011-06-16 17:38:52 +01:00
|
|
|
saved_perms = stat.mode
|
2010-05-10 01:10:49 +01:00
|
|
|
chmod 0644
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
chmod saved_perms if saved_perms
|
|
|
|
end
|
2012-01-21 00:51:20 +01:00
|
|
|
|
|
|
|
def install_info
|
|
|
|
unless self.symlink?
|
|
|
|
raise "Cannot install info entry for unbrewed info file '#{self}'"
|
|
|
|
end
|
2012-02-10 13:02:47 -06:00
|
|
|
system '/usr/bin/install-info', '--quiet', self.to_s, (self.dirname+'dir').to_s
|
2012-01-21 00:51:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def uninstall_info
|
|
|
|
unless self.symlink?
|
|
|
|
raise "Cannot uninstall info entry for unbrewed info file '#{self}'"
|
|
|
|
end
|
|
|
|
system '/usr/bin/install-info', '--delete', '--quiet', self.to_s, (self.dirname+'dir').to_s
|
|
|
|
end
|
2012-03-02 20:28:54 +00:00
|
|
|
|
2012-03-16 17:24:46 +00:00
|
|
|
def all_formula pwd = self
|
2012-03-02 20:28:54 +00:00
|
|
|
children.map{ |child| child.relative_path_from(pwd) }.each do |pn|
|
|
|
|
yield pn if pn.to_s =~ /.rb$/
|
|
|
|
end
|
|
|
|
children.each do |child|
|
2012-03-16 17:24:46 +00:00
|
|
|
child.all_formula(pwd) do |pn|
|
2012-03-02 20:28:54 +00:00
|
|
|
yield pn
|
|
|
|
end if child.directory?
|
|
|
|
end
|
|
|
|
end
|
2012-03-16 17:24:46 +00:00
|
|
|
|
|
|
|
def find_formula
|
|
|
|
# remove special casing once tap is established and alt removed
|
|
|
|
if self == HOMEBREW_LIBRARY/"Taps/adamv-alt"
|
|
|
|
all_formula do |file|
|
|
|
|
yield file
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
[self/:Formula, self/:HomebrewFormula, self].each do |d|
|
2012-03-16 21:31:18 +00:00
|
|
|
if d.exist?
|
|
|
|
d.children.map{ |child| child.relative_path_from(self) }.each do |pn|
|
|
|
|
yield pn if pn.to_s =~ /.rb$/
|
|
|
|
end
|
2012-03-16 17:24:46 +00:00
|
|
|
break
|
2012-03-16 21:31:18 +00:00
|
|
|
end
|
2012-03-16 17:24:46 +00:00
|
|
|
end
|
|
|
|
end
|
2009-07-31 02:51:17 +01:00
|
|
|
end
|
2009-08-10 16:48:30 +01:00
|
|
|
|
|
|
|
# sets $n and $d so you can observe creation of stuff
|
|
|
|
module ObserverPathnameExtension
|
|
|
|
def unlink
|
|
|
|
super
|
|
|
|
puts "rm #{to_s}" if ARGV.verbose?
|
|
|
|
$n+=1
|
|
|
|
end
|
|
|
|
def rmdir
|
|
|
|
super
|
|
|
|
puts "rmdir #{to_s}" if ARGV.verbose?
|
|
|
|
$d+=1
|
|
|
|
end
|
|
|
|
def make_relative_symlink src
|
2010-08-15 17:17:26 -07:00
|
|
|
super
|
|
|
|
$n+=1
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
2012-01-21 00:51:20 +01:00
|
|
|
def install_info
|
|
|
|
super
|
|
|
|
puts "info #{to_s}" if ARGV.verbose?
|
|
|
|
end
|
|
|
|
def uninstall_info
|
|
|
|
super
|
|
|
|
puts "uninfo #{to_s}" if ARGV.verbose?
|
|
|
|
end
|
2009-08-10 16:48:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
$n=0
|
|
|
|
$d=0
|