Improved audit of python stuff

This commit is contained in:
Samuel John 2013-06-06 16:41:14 +02:00
parent e7838bdebb
commit 031a0ae54e

View File

@ -465,15 +465,6 @@ class FormulaAuditor
end end
def audit_python def audit_python
if text =~ /system\(?\s*['"]python/
# Todo: In `def test` it is okay to do it this way. It's even recommended!
problem "Instead of `system 'python', ...`, call `system python, ...`."
end
if text =~ /system\(?\s*python\.binary/
problem "Instead of `system python.binary, ...`, call `system python, ...`."
end
if text =~ /(def\s*)?which_python/ if text =~ /(def\s*)?which_python/
problem "Replace `which_python` by `python.xy`, which returns e.g. 'python2.7'." problem "Replace `which_python` by `python.xy`, which returns e.g. 'python2.7'."
end end
@ -482,17 +473,23 @@ class FormulaAuditor
problem "Don't locate python with `which 'python'`, use `python.binary` instead" problem "Don't locate python with `which 'python'`, use `python.binary` instead"
end end
if f.requirements.any?{ |r| r.kind_of?(PythonInstalled) } # Checks that apply only to code in def install
# Don't check this for all formulae, because some are allowed to set the if text =~ /(\s*)def\s+install((.*\n)*?)(\1end)/
# PYTHONPATH. E.g. python.rb itself needs to set it. install_body = $2
if text =~ /ENV\.append.*PYTHONPATH/ || text =~ /ENV\[['"]PYTHONPATH['"]\]\s*=[^=]/
problem "Don't set the PYTHONPATH, instead declare `depends_on :python`." if install_body =~ /system\(?\s*['"]python/
problem "Instead of `system 'python', ...`, call `system python, ...`."
end
if text =~ /system\(?\s*python\.binary/
problem "Instead of `system python.binary, ...`, call `system python, ...`."
end end
end end
# Checks that apply only to code in def caveats
if text =~ /(\s*)def\s+caveats((.*\n)*?)(\1end)/ || /(\s*)def\s+caveats;(.*?)end/ if text =~ /(\s*)def\s+caveats((.*\n)*?)(\1end)/ || /(\s*)def\s+caveats;(.*?)end/
caveats_body = $2 caveats_body = $2
if caveats_body =~ /(python[23]?)\.(.*\w)/ if caveats_body =~ /[ \{=](python[23]?)\.(.*\w)/
# So if in the body of caveats there is a `python.whatever` called, # So if in the body of caveats there is a `python.whatever` called,
# check that there is a guard like `if python` or similiar: # check that there is a guard like `if python` or similiar:
python = $1 python = $1
@ -503,6 +500,27 @@ class FormulaAuditor
end end
end end
if f.requirements.any?{ |r| r.kind_of?(PythonInstalled) }
# Don't check this for all formulae, because some are allowed to set the
# PYTHONPATH. E.g. python.rb itself needs to set it.
if text =~ /ENV\.append.*PYTHONPATH/ || text =~ /ENV\[['"]PYTHONPATH['"]\]\s*=[^=]/
problem "Don't set the PYTHONPATH, instead declare `depends_on :python`."
end
else
# So if there is no PythonInstalled requirement, we can check if the
# formula still uses python and should add a `depends_on :python`
unless f.name.to_s =~ /(pypy[0-9]*)|(python[0-9]*)/
if text =~ /system.["' ]?python([0-9"'])?/
problem "If the formula uses Python, it should declare so by `depends_on :python#{$1}`"
end
if text =~ /setup\.py/
problem <<-EOS.undent
If the formula installs Python bindings you should declare `depends_on :python[3]`"
EOS
end
end
end
# Todo: # Todo:
# The python do ... end block is possibly executed twice. Once for # The python do ... end block is possibly executed twice. Once for
# python 2.x and once for 3.x. So if a `system 'make'` is called, a # python 2.x and once for 3.x. So if a `system 'make'` is called, a