2010-06-23 11:20:47 -07:00
|
|
|
require 'formula'
|
|
|
|
require 'utils'
|
2012-03-17 19:49:49 -07:00
|
|
|
require 'extend/ENV'
|
|
|
|
|
|
|
|
# Some formulae use ENV in patches, so set up an environment
|
|
|
|
ENV.extend(HomebrewEnvExtension)
|
|
|
|
ENV.setup_build_environment
|
|
|
|
|
|
|
|
class Module
|
|
|
|
def redefine_const(name, value)
|
|
|
|
__send__(:remove_const, name) if const_defined?(name)
|
|
|
|
const_set(name, value)
|
|
|
|
end
|
|
|
|
end
|
2010-06-23 11:20:47 -07:00
|
|
|
|
|
|
|
def ff
|
2010-07-18 13:57:18 -07:00
|
|
|
return Formula.all if ARGV.named.empty?
|
|
|
|
return ARGV.formulae
|
2010-06-23 11:20:47 -07:00
|
|
|
end
|
|
|
|
|
2011-03-10 12:26:29 -08:00
|
|
|
def audit_formula_text name, text
|
2010-08-07 15:23:13 -07:00
|
|
|
problems = []
|
|
|
|
|
2012-03-10 10:26:06 -08:00
|
|
|
if text =~ /<(Formula|AmazonWebServicesFormula|ScriptFileFormula|GithubGistFormula)/
|
2011-06-06 07:25:00 -07:00
|
|
|
problems << " * Use a space in class inheritance: class Foo < #{$1}"
|
|
|
|
end
|
2011-03-21 12:57:35 -07:00
|
|
|
|
2010-08-08 10:17:53 -07:00
|
|
|
# Commented-out cmake support from default template
|
2010-11-04 08:11:49 -07:00
|
|
|
if (text =~ /# depends_on 'cmake'/) or (text =~ /# system "cmake/)
|
2010-07-12 10:34:12 -07:00
|
|
|
problems << " * Commented cmake support found."
|
|
|
|
end
|
|
|
|
|
2010-09-07 14:34:39 -07:00
|
|
|
# 2 (or more in an if block) spaces before depends_on, please
|
2010-08-07 15:23:13 -07:00
|
|
|
if text =~ /^\ ?depends_on/
|
2010-07-23 21:31:32 -07:00
|
|
|
problems << " * Check indentation of 'depends_on'."
|
|
|
|
end
|
|
|
|
|
2012-02-21 18:57:27 -08:00
|
|
|
# build tools should be flagged properly
|
2012-03-17 11:16:10 -05:00
|
|
|
build_deps = %w{autoconf automake boost-build bsdmake
|
|
|
|
cmake imake libtool pkg-config scons smake}
|
2012-02-28 11:31:20 -06:00
|
|
|
if text =~ /depends_on ['"](#{build_deps*'|'})['"]$/
|
2011-11-29 19:37:39 -06:00
|
|
|
problems << " * #{$1} dependency should be \"depends_on '#{$1}' => :build\""
|
|
|
|
end
|
|
|
|
|
2010-08-15 15:19:19 -07:00
|
|
|
# FileUtils is included in Formula
|
|
|
|
if text =~ /FileUtils\.(\w+)/
|
|
|
|
problems << " * Don't need 'FileUtils.' before #{$1}."
|
|
|
|
end
|
|
|
|
|
2010-09-08 09:07:59 -07:00
|
|
|
# Check for long inreplace block vars
|
|
|
|
if text =~ /inreplace .* do \|(.{2,})\|/
|
|
|
|
problems << " * \"inreplace <filenames> do |s|\" is preferred over \"|#{$1}|\"."
|
|
|
|
end
|
|
|
|
|
2010-09-07 14:34:39 -07:00
|
|
|
# Check for string interpolation of single values.
|
2012-06-17 17:58:49 -05:00
|
|
|
if text =~ /(system|inreplace|gsub!|change_make_var!) .* ['"]#\{(\w+(\.\w+)?)\}['"]/
|
2010-09-07 14:34:39 -07:00
|
|
|
problems << " * Don't need to interpolate \"#{$2}\" with #{$1}"
|
|
|
|
end
|
|
|
|
|
2010-08-08 10:17:53 -07:00
|
|
|
# Check for string concatenation; prefer interpolation
|
2010-08-07 15:23:13 -07:00
|
|
|
if text =~ /(#\{\w+\s*\+\s*['"][^}]+\})/
|
2010-07-12 10:34:12 -07:00
|
|
|
problems << " * Try not to concatenate paths in string interpolation:\n #{$1}"
|
2010-06-23 11:20:47 -07:00
|
|
|
end
|
|
|
|
|
2010-08-08 10:17:53 -07:00
|
|
|
# Prefer formula path shortcuts in Pathname+
|
2012-04-29 09:32:39 -07:00
|
|
|
if text =~ %r{\(\s*(prefix\s*\+\s*(['"])(bin|include|libexec|lib|sbin|share)[/'"])}
|
2010-08-08 10:17:53 -07:00
|
|
|
problems << " * \"(#{$1}...#{$2})\" should be \"(#{$3}+...)\""
|
|
|
|
end
|
|
|
|
|
2010-08-15 11:32:45 -07:00
|
|
|
if text =~ %r[((man)\s*\+\s*(['"])(man[1-8])(['"]))]
|
|
|
|
problems << " * \"#{$1}\" should be \"#{$4}\""
|
|
|
|
end
|
|
|
|
|
2010-08-08 10:17:53 -07:00
|
|
|
# Prefer formula path shortcuts in strings
|
2011-09-28 14:28:14 -05:00
|
|
|
if text =~ %r[(\#\{prefix\}/(bin|include|libexec|lib|sbin|share))]
|
2010-08-08 10:17:53 -07:00
|
|
|
problems << " * \"#{$1}\" should be \"\#{#{$2}}\""
|
|
|
|
end
|
|
|
|
|
2010-08-09 11:59:16 -07:00
|
|
|
if text =~ %r[((\#\{prefix\}/share/man/|\#\{man\}/)(man[1-8]))]
|
|
|
|
problems << " * \"#{$1}\" should be \"\#{#{$3}}\""
|
2010-08-08 10:17:53 -07:00
|
|
|
end
|
|
|
|
|
2010-09-08 09:22:48 -07:00
|
|
|
if text =~ %r[((\#\{share\}/(man)))[/'"]]
|
|
|
|
problems << " * \"#{$1}\" should be \"\#{#{$3}}\""
|
|
|
|
end
|
|
|
|
|
2010-08-08 10:17:53 -07:00
|
|
|
if text =~ %r[(\#\{prefix\}/share/(info|man))]
|
|
|
|
problems << " * \"#{$1}\" should be \"\#{#{$2}}\""
|
|
|
|
end
|
|
|
|
|
2010-09-13 15:16:09 -07:00
|
|
|
# Commented-out depends_on
|
|
|
|
if text =~ /#\s*depends_on\s+(.+)\s*$/
|
|
|
|
problems << " * Commented-out dep #{$1}."
|
|
|
|
end
|
|
|
|
|
2010-08-10 11:52:03 -07:00
|
|
|
# No trailing whitespace, please
|
2010-10-21 07:51:47 -07:00
|
|
|
if text =~ /(\t|[ ])+$/
|
2010-08-07 15:23:13 -07:00
|
|
|
problems << " * Trailing whitespace was found."
|
|
|
|
end
|
|
|
|
|
2012-02-16 23:43:07 -06:00
|
|
|
if text =~ /if\s+ARGV\.include\?\s+'--(HEAD|devel)'/
|
|
|
|
problems << " * Use \"if ARGV.build_#{$1.downcase}?\" instead"
|
2010-10-08 20:11:40 -07:00
|
|
|
end
|
|
|
|
|
2010-10-30 08:26:36 -07:00
|
|
|
if text =~ /make && make/
|
|
|
|
problems << " * Use separate make calls."
|
|
|
|
end
|
|
|
|
|
2011-10-18 10:00:45 -07:00
|
|
|
if text =~ /^[ ]*\t/
|
2011-03-21 13:06:16 -07:00
|
|
|
problems << " * Use spaces instead of tabs for indentation"
|
2011-10-18 10:00:45 -07:00
|
|
|
end
|
2010-10-21 07:51:47 -07:00
|
|
|
|
2011-03-21 12:54:00 -07:00
|
|
|
# xcodebuild should specify SYMROOT
|
2012-02-18 20:46:26 -06:00
|
|
|
if text =~ /system\s+['"]xcodebuild/ and not text =~ /SYMROOT=/
|
2011-03-21 12:54:00 -07:00
|
|
|
problems << " * xcodebuild should be passed an explicit \"SYMROOT\""
|
2011-11-27 08:39:44 -08:00
|
|
|
end
|
2011-03-21 12:54:00 -07:00
|
|
|
|
2011-11-18 15:22:04 -06:00
|
|
|
# using ARGV.flag? for formula options is generally a bad thing
|
|
|
|
if text =~ /ARGV\.flag\?/
|
|
|
|
problems << " * Use 'ARGV.include?' instead of 'ARGV.flag?'"
|
|
|
|
end
|
|
|
|
|
2011-12-08 22:16:19 -06:00
|
|
|
# Avoid hard-coding compilers
|
|
|
|
if text =~ %r[(system|ENV\[.+\]\s?=)\s?['"](/usr/bin/)?(gcc|llvm-gcc|clang)['" ]]
|
|
|
|
problems << " * Use \"\#{ENV.cc}\" instead of hard-coding \"#{$3}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
if text =~ %r[(system|ENV\[.+\]\s?=)\s?['"](/usr/bin/)?((g|llvm-g|clang)\+\+)['" ]]
|
|
|
|
problems << " * Use \"\#{ENV.cxx}\" instead of hard-coding \"#{$3}\""
|
|
|
|
end
|
|
|
|
|
2012-06-17 17:59:30 -05:00
|
|
|
if text =~ /system\s+['"](env|export)/
|
|
|
|
problems << " * Use ENV instead of invoking '#{$1}' to modify the environment"
|
|
|
|
end
|
|
|
|
|
2012-06-17 18:01:22 -05:00
|
|
|
if text =~ /version == ['"]HEAD['"]/
|
|
|
|
problems << " * Use 'ARGV.build_head?' instead of inspecting 'version'"
|
|
|
|
end
|
|
|
|
|
2010-08-09 11:59:16 -07:00
|
|
|
return problems
|
|
|
|
end
|
|
|
|
|
2012-03-17 08:45:51 -07:00
|
|
|
INSTALL_OPTIONS = %W[
|
|
|
|
--build-from-source
|
|
|
|
--debug
|
|
|
|
--devel
|
|
|
|
--force
|
|
|
|
--fresh
|
|
|
|
--HEAD
|
|
|
|
--ignore-dependencies
|
|
|
|
--interactive
|
|
|
|
--use-clang
|
|
|
|
--use-gcc
|
|
|
|
--use-llvm
|
|
|
|
--verbose
|
|
|
|
].freeze
|
|
|
|
|
2012-03-17 19:49:49 -07:00
|
|
|
def audit_formula_patches f
|
|
|
|
problems = []
|
|
|
|
patches = Patches.new(f.patches)
|
|
|
|
patches.each do |p|
|
|
|
|
next unless p.external?
|
2012-05-07 17:44:57 -05:00
|
|
|
case p.url
|
|
|
|
when %r[raw\.github\.com], %r[gist\.github\.com/raw]
|
2012-03-17 19:49:49 -07:00
|
|
|
problems << " * Using raw GitHub URLs is not recommended:"
|
2012-05-07 17:44:57 -05:00
|
|
|
problems << " #{p.url}"
|
|
|
|
when %r[macports/trunk]
|
|
|
|
problems << " * MacPorts patches should specify a revision instead of trunk:"
|
|
|
|
problems << " #{p.url}"
|
2012-03-17 19:49:49 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return problems
|
|
|
|
end
|
|
|
|
|
2010-09-09 14:16:05 -07:00
|
|
|
def audit_formula_urls f
|
|
|
|
problems = []
|
|
|
|
|
2011-03-15 21:40:09 -07:00
|
|
|
unless f.homepage =~ %r[^https?://]
|
|
|
|
problems << " * The homepage should start with http or https."
|
|
|
|
end
|
|
|
|
|
2012-01-25 22:41:53 -06:00
|
|
|
# Google Code homepages should end in a slash
|
|
|
|
if f.homepage =~ %r[^https?://code\.google\.com/p/[^/]+[^/]$]
|
|
|
|
problems << " * Google Code homepage should end with a slash."
|
|
|
|
end
|
|
|
|
|
2012-04-05 21:12:02 -05:00
|
|
|
urls = [(f.stable.url rescue nil), (f.devel.url rescue nil), (f.head.url rescue nil)].reject {|p| p.nil?}
|
2010-09-29 10:21:44 -07:00
|
|
|
|
2011-11-30 13:14:24 -06:00
|
|
|
# Check GNU urls; doesn't apply to mirrors
|
|
|
|
urls.each do |p|
|
|
|
|
if p =~ %r[^(https?|ftp)://(.+)/gnu/]
|
|
|
|
problems << " * \"ftpmirror.gnu.org\" is preferred for GNU software."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# the rest of the checks apply to mirrors as well
|
2012-04-05 21:12:02 -05:00
|
|
|
mirrors = [(f.stable.mirrors rescue []), (f.devel.mirrors rescue [])].flatten.reject { |m| m.nil? }
|
|
|
|
urls.concat mirrors.map { |m| m[:url] }
|
2011-10-15 00:39:05 -05:00
|
|
|
|
2010-09-09 14:16:05 -07:00
|
|
|
# Check SourceForge urls
|
2010-09-29 10:21:44 -07:00
|
|
|
urls.each do |p|
|
2010-09-09 14:16:05 -07:00
|
|
|
# Is it a filedownload (instead of svnroot)
|
|
|
|
next if p =~ %r[/svnroot/]
|
|
|
|
next if p =~ %r[svn\.sourceforge]
|
|
|
|
|
|
|
|
# Is it a sourceforge http(s) URL?
|
2011-09-13 23:27:59 -05:00
|
|
|
next unless p =~ %r[^https?://.*\bsourceforge\.]
|
2010-09-09 14:16:05 -07:00
|
|
|
|
2011-10-02 15:46:09 -05:00
|
|
|
if p =~ /(\?|&)use_mirror=/
|
|
|
|
problems << " * Update this url (don't use #{$1}use_mirror)."
|
2010-09-09 14:16:05 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if p =~ /\/download$/
|
|
|
|
problems << " * Update this url (don't use /download)."
|
|
|
|
end
|
|
|
|
|
|
|
|
if p =~ %r[^http://prdownloads\.]
|
|
|
|
problems << " * Update this url (don't use prdownloads)."
|
|
|
|
end
|
|
|
|
|
|
|
|
if p =~ %r[^http://\w+\.dl\.]
|
|
|
|
problems << " * Update this url (don't use specific dl mirrors)."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-13 14:20:55 -07:00
|
|
|
# Check for git:// urls; https:// is preferred.
|
|
|
|
urls.each do |p|
|
|
|
|
if p =~ %r[^git://github\.com/]
|
|
|
|
problems << " * Use https:// URLs for accessing repositories on GitHub."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-09 14:16:05 -07:00
|
|
|
return problems
|
|
|
|
end
|
|
|
|
|
2012-04-05 21:12:02 -05:00
|
|
|
def audit_formula_specs f
|
2012-01-25 22:41:53 -06:00
|
|
|
problems = []
|
|
|
|
|
2012-04-05 21:12:02 -05:00
|
|
|
[:stable, :devel].each do |spec|
|
|
|
|
s = f.send(spec)
|
|
|
|
next if s.nil?
|
2012-01-25 22:41:53 -06:00
|
|
|
|
2012-04-05 21:12:02 -05:00
|
|
|
if s.version.to_s.empty?
|
|
|
|
problems << " * invalid or missing #{spec} version"
|
|
|
|
else
|
|
|
|
version_text = s.version if s.explicit_version?
|
|
|
|
version_url = Pathname.new(s.url).version
|
|
|
|
if version_url == version_text
|
|
|
|
problems << " * #{spec} version #{version_text} is redundant with version scanned from URL"
|
|
|
|
end
|
|
|
|
end
|
2012-01-25 22:41:53 -06:00
|
|
|
|
2012-06-18 19:58:35 -05:00
|
|
|
cksum = s.checksum
|
|
|
|
next if cksum.nil?
|
|
|
|
|
|
|
|
len = case cksum.hash_type
|
2012-04-05 21:12:02 -05:00
|
|
|
when :md5 then 32
|
|
|
|
when :sha1 then 40
|
|
|
|
when :sha256 then 64
|
|
|
|
end
|
|
|
|
|
2012-06-18 19:58:35 -05:00
|
|
|
if cksum.empty?
|
|
|
|
problems << " * #{cksum.hash_type} is empty"
|
|
|
|
else
|
|
|
|
problems << " * #{cksum.hash_type} should be #{len} characters" unless cksum.hexdigest.length == len
|
|
|
|
problems << " * #{cksum.hash_type} contains invalid characters" unless cksum.hexdigest =~ /^[a-fA-F0-9]+$/
|
|
|
|
problems << " * #{cksum.hash_type} should be lowercase" unless cksum.hexdigest == cksum.hexdigest.downcase
|
2012-04-05 21:12:02 -05:00
|
|
|
end
|
2012-01-25 22:41:53 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
return problems
|
|
|
|
end
|
|
|
|
|
2010-08-21 11:55:57 -07:00
|
|
|
def audit_formula_instance f
|
|
|
|
problems = []
|
|
|
|
|
|
|
|
# Don't depend_on aliases; use full name
|
|
|
|
aliases = Formula.aliases
|
|
|
|
f.deps.select {|d| aliases.include? d}.each do |d|
|
|
|
|
problems << " * Dep #{d} is an alias; switch to the real name."
|
|
|
|
end
|
|
|
|
|
2010-09-07 09:23:29 -07:00
|
|
|
# Check for things we don't like to depend on.
|
2011-11-29 19:54:16 -06:00
|
|
|
# We allow non-Homebrew installs whenever possible.
|
2010-09-07 09:23:29 -07:00
|
|
|
f.deps.each do |d|
|
2011-03-12 17:40:40 -08:00
|
|
|
begin
|
|
|
|
dep_f = Formula.factory d
|
|
|
|
rescue
|
|
|
|
problems << " * Can't find dependency \"#{d}\"."
|
|
|
|
end
|
|
|
|
|
2012-05-15 17:44:15 -05:00
|
|
|
case d.name
|
2012-01-17 21:03:11 -06:00
|
|
|
when "git", "python", "ruby", "emacs", "mysql", "postgresql", "mercurial"
|
2012-02-18 20:46:52 -06:00
|
|
|
problems << <<-EOS
|
|
|
|
* Don't use #{d} as a dependency. We allow non-Homebrew
|
|
|
|
#{d} installations.
|
|
|
|
EOS
|
2012-03-01 19:56:09 -06:00
|
|
|
when 'gfortran'
|
|
|
|
problems << " * Use ENV.fortran during install instead of depends_on 'gfortran'"
|
2012-07-06 14:25:02 -08:00
|
|
|
|
|
|
|
when 'open-mpi', 'mpich2'
|
|
|
|
problems << <<-EOS.undent
|
|
|
|
* There are multiple conflicting ways to install MPI. Use a MPIDependency:
|
|
|
|
depends_on MPIDependency.new(<lang list>)
|
|
|
|
Where <lang list> is a comma delimited list that can include:
|
|
|
|
:cc, :cxx, :f90, :f77
|
|
|
|
EOS
|
2010-09-07 09:23:29 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-21 11:55:57 -07:00
|
|
|
return problems
|
|
|
|
end
|
|
|
|
|
2012-03-17 19:49:49 -07:00
|
|
|
# Formula extensions for auditing
|
|
|
|
class Formula
|
|
|
|
def head_only?
|
2012-04-05 21:12:02 -05:00
|
|
|
@head and @stable.nil?
|
2012-03-17 19:49:49 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def formula_text
|
|
|
|
File.open(@path, "r") { |afile| return afile.read }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-12 21:10:23 -08:00
|
|
|
module Homebrew extend self
|
2010-11-09 13:00:33 +00:00
|
|
|
def audit
|
2011-05-31 13:23:42 -07:00
|
|
|
errors = false
|
|
|
|
|
2012-02-16 20:27:08 -08:00
|
|
|
brew_count = 0
|
|
|
|
problem_count = 0
|
|
|
|
|
2010-11-09 13:00:33 +00:00
|
|
|
ff.each do |f|
|
2012-03-17 19:49:49 -07:00
|
|
|
# We need to do this in case the formula defines a patch that uses DATA.
|
|
|
|
f.class.redefine_const :DATA, ""
|
2011-12-18 09:10:59 -08:00
|
|
|
|
2012-03-17 19:49:49 -07:00
|
|
|
problems = []
|
|
|
|
problems += [' * head-only formula'] if f.head_only?
|
2010-11-09 13:00:33 +00:00
|
|
|
problems += audit_formula_instance f
|
|
|
|
problems += audit_formula_urls f
|
2012-03-17 19:49:49 -07:00
|
|
|
problems += audit_formula_patches f
|
2010-11-09 13:00:33 +00:00
|
|
|
|
2011-01-22 09:32:16 -08:00
|
|
|
perms = File.stat(f.path).mode
|
|
|
|
if perms.to_s(8) != "100644"
|
|
|
|
problems << " * permissions wrong; chmod 644 #{f.path}"
|
|
|
|
end
|
|
|
|
|
2012-03-17 19:49:49 -07:00
|
|
|
text = f.formula_text
|
2010-08-10 11:52:03 -07:00
|
|
|
|
2010-11-09 13:00:33 +00:00
|
|
|
# DATA with no __END__
|
|
|
|
if (text =~ /\bDATA\b/) and not (text =~ /^\s*__END__\s*$/)
|
|
|
|
problems << " * 'DATA' was found, but no '__END__'"
|
|
|
|
end
|
2011-02-20 15:03:15 -08:00
|
|
|
|
2012-01-25 22:41:53 -06:00
|
|
|
# files should end with a newline
|
|
|
|
if text =~ /.+\z/
|
|
|
|
problems << " * File should end with a newline"
|
|
|
|
end
|
2012-01-22 22:32:15 -06:00
|
|
|
|
2010-11-09 13:00:33 +00:00
|
|
|
# Don't try remaining audits on text in __END__
|
|
|
|
text_without_patch = (text.split("__END__")[0]).strip()
|
2010-08-09 11:59:16 -07:00
|
|
|
|
2011-03-10 12:26:29 -08:00
|
|
|
problems += audit_formula_text(f.name, text_without_patch)
|
2012-04-05 21:12:02 -05:00
|
|
|
problems += audit_formula_specs(f)
|
2010-11-09 13:00:33 +00:00
|
|
|
|
|
|
|
unless problems.empty?
|
2011-05-31 13:23:42 -07:00
|
|
|
errors = true
|
2010-11-09 13:00:33 +00:00
|
|
|
puts "#{f.name}:"
|
|
|
|
puts problems * "\n"
|
|
|
|
puts
|
2012-02-16 20:27:08 -08:00
|
|
|
brew_count += 1
|
2012-05-07 17:50:05 -05:00
|
|
|
problem_count += problems.select{ |p| p.start_with? ' *' }.size
|
2010-11-09 13:00:33 +00:00
|
|
|
end
|
2010-08-09 11:59:16 -07:00
|
|
|
end
|
2011-05-31 13:23:42 -07:00
|
|
|
|
2012-04-30 14:08:59 +10:00
|
|
|
ofail "#{problem_count} problems in #{brew_count} brews" if errors
|
2010-06-23 11:20:47 -07:00
|
|
|
end
|
|
|
|
end
|