Add an additional fake package variable '%SETVARS%' that shows the currently defined variables when processing a PKGBUILD. Use this list in the extravars rule instead of broke-ass regex parsing that will never catch every possible corner case. Also update the tests accordingly. Variables defined inside a function are nothing we are going to worry about at the moment, so safely ignore them. Add a legal variable to ensure we don't have any false positives, and fix our own misgivings in the PKGBUILD parser so we aren't polluting the namespace. Signed-off-by: Dan McGee <dan@archlinux.org> --- Slightly touched-up resend; trying to get the unit tests passing would be a good idea. Namcap/rules/extravars.py | 11 ++++------- Namcap/tests/pkgbuild/test_extravars.py | 7 ++----- parsepkgbuild | 1 - parsepkgbuild.sh | 21 ++++++++++----------- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/Namcap/rules/extravars.py b/Namcap/rules/extravars.py index 01031f5..0fcdb53 100644 --- a/Namcap/rules/extravars.py +++ b/Namcap/rules/extravars.py @@ -31,12 +31,9 @@ class package(PkgbuildRule): 'pkgbase', 'pkgver', 'pkgrel', 'pkgdesc', 'groups', 'url', 'install', 'changelog', 'options', 'optdepends'] - for i in pkginfo.pkgbuild: - m = re.match('[\s]*([a-z][^\s=]*[^+])=', i) - if m: - varname = m.group(1) - if varname not in stdvars: - if not varname.startswith('_'): - self.warnings.append(("extra-var-begins-without-underscore %s", varname)) + for varname in pkginfo["setvars"]: + if varname.islower() and varname not in stdvars \ + and not varname.startswith('_'): + self.warnings.append(("extra-var-begins-without-underscore %s", varname)) # vim: set ts=4 sw=4 noet: diff --git a/Namcap/tests/pkgbuild/test_extravars.py b/Namcap/tests/pkgbuild/test_extravars.py index 8913d87..651ec5a 100644 --- a/Namcap/tests/pkgbuild/test_extravars.py +++ b/Namcap/tests/pkgbuild/test_extravars.py @@ -42,6 +42,7 @@ source=(ftp://ftp.example.com/pub/mypackage-0.1.tar.gz) md5sums=('abcdefabcdef12345678901234567890') mycustomvar="something" +_legalvar="something else" build() { cd "${srcdir}"/${pkgname}-${pkgver} @@ -106,7 +107,6 @@ package() { self.assertEqual(r.errors, []) self.assertEqual(set(r.warnings), set([ ("extra-var-begins-without-underscore %s", "mycustomvar"), - ("extra-var-begins-without-underscore %s", "hello") ])) self.assertEqual(r.infos, []) @@ -114,10 +114,7 @@ package() { "a tricky PKGBUILD with custom variables without underscore" r = self.run_on_pkg(self.pkgbuild2) self.assertEqual(r.errors, []) - self.assertEqual(set(r.warnings), set([ - ("extra-var-begins-without-underscore %s", "mycustomvar"), - ("extra-var-begins-without-underscore %s", "hello") - ])) + self.assertEqual(r.warnings, []) self.assertEqual(r.infos, []) diff --git a/parsepkgbuild b/parsepkgbuild index 8f0f67d..04e71fd 100755 --- a/parsepkgbuild +++ b/parsepkgbuild @@ -9,4 +9,3 @@ export PATH=/tmp/parsepkgbuild PARSE_PKGBUILD_PATH=${PARSE_PKGBUILD_PATH:-/usr/share/namcap} exec /bin/bash --noprofile --norc -r "$PARSE_PKGBUILD_PATH"/parsepkgbuild.sh $1 - diff --git a/parsepkgbuild.sh b/parsepkgbuild.sh index d601579..4df298b 100644 --- a/parsepkgbuild.sh +++ b/parsepkgbuild.sh @@ -124,31 +124,31 @@ if [ -n "$install" ]; then echo -e "%INSTALL%\n$install\n" fi +unset i +echo "%SETVARS%" +compgen -A variable } # is it a split pkgbuild ? if [ -n "${pkgbase}" ]; then - pkgnames=(${pkgname[@]}) + _namcap_pkgnames=(${pkgname[@]}) unset pkgname echo -e "%SPLIT%\n1\n" echo -e "%BASE%\n${pkgbase}\n" echo "%NAMES%" - for i in ${pkgnames[@]}; do echo $i; done + for i in ${_namcap_pkgnames[@]}; do echo $i; done echo "" pkginfo -fi - -# print per package information -if [ -n "${pkgbase}" ]; then - for subpkg in ${pkgnames[@]} + # print per package information + for _namcap_subpkg in ${_namcap_pkgnames[@]} do echo -e '\0' - pkgname=$subpkg - package_$subpkg + pkgname=$_namcap_subpkg + package_$_namcap_subpkg pkginfo # did the function actually exist? echo "%PKGFUNCTION%" - type -t package_$subpkg || echo undefined + type -t package_$_namcap_subpkg || echo undefined echo "" done else @@ -156,4 +156,3 @@ else fi # vim: set noet ts=4 sw=4: - -- 1.7.4.1