scripts: add tool to scan for bashisms recipe shell scripts
PATCH 1/2 scripts: add tool to scan for bashisms recipe shell scripts
Shell functions in bitbake are executed with /bin/sh so should be POSIX compliant and not use Bash extensions, or at least only use extensions that are implemented in both dash and ash (busybox).
This tool will extract all of the shell scripts from all recipes and run them through checkbashisms (it assumes that checkbashisms is on $PATH).
There is a whitelist to filter out false-positives such as the use of $HOSTNAME (a bashism) in functions where we have defined it, or using the 'type' builtin which is supported by ash/dash.
[ YOCTO #8851 ]
--- scripts/verify-bashisms | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 scripts/verify-bashisms
scripts/verify-bashisms new file +116 -0
@@ -0,0 +1,116 @@ +#!/usr/bin/env python3 + +import sys, os, subprocess, re, shutil + +whitelist = ( + # type is supported by dash + 'if type systemctl >/dev/null 2>/dev/null; then', + 'if type systemd-tmpfiles >/dev/null 2>/dev/null; then', + 'if type update-rc.d >/dev/null 2>/dev/null; then', + 'command -v', + # HOSTNAME is set locally + 'buildhistory_single_commit "$CMDLINE" "$HOSTNAME"', + # False-positive, match is a grep not shell expression + 'grep "^$groupname:[^:]*:[^:]*:\\([^,]*,\\)*$username\\(,[^,]*\\)*"', + # TODO verify dash's '. script args' behaviour + '. $target_sdk_dir/${oe_init_build_env_path} $target_sdk_dir >> $LOGFILE' + ) + +def is_whitelisted(s): + for w in whitelist: + if w in s: + return True + return False + +def process(recipe, function, script): + import tempfile + + if not script.startswith("#!"): + script = "#! /bin/sh\n" + script + + fn = tempfile.NamedTemporaryFile(mode="w+t") + fn.write(script) + fn.flush() + + try: + subprocess.check_output(("checkbashisms.pl", fn.name), universal_newlines=True, stderr=subprocess.STDOUT) + # No bashisms, so just return + return + except subprocess.CalledProcessError as e: + # TODO check exit code is 1 + + # Replace the temporary filename with the function and split it + output = e.output.replace(fn.name, function).splitlines() + if len(results) % 2 != 0: + print("Unexpected output from checkbashism: %s" % str(output)) + return + + # Turn the output into a list of (message, source) values + result = [] + # Check the results against the whitelist + for message, source in zip(output[0::2], output[1::2]): + if not is_whitelisted(source): + result.append((message, source)) + return result + +def get_tinfoil(): + scripts_path = os.path.dirname(os.path.realpath(__file__)) + lib_path = scripts_path + '/lib' + sys.path = sys.path + [lib_path] + import scriptpath + scriptpath.add_bitbake_lib_path() + import bb.tinfoil + tinfoil = bb.tinfoil.Tinfoil() + tinfoil.prepare() + # tinfoil.logger.setLevel(logging.WARNING) + return tinfoil + +if __name__=='__main__': + import shutil + if shutil.which("checkbashisms.pl") is None: + print("Cannot find checkbashisms.pl on $PATH") + sys.exit(1) + + tinfoil = get_tinfoil() + + # This is only the default configuration and should iterate over + # recipecaches to handle multiconfig environments + pkg_pn = tinfoil.cooker.recipecaches[""].pkg_pn + + # TODO: use argparse and have --help + if len(sys.argv) > 1: + initial_pns = sys.argv[1:] + else: + initial_pns = sorted(pkg_pn) + + pns = [] + print("Generating file list...") + for pn in initial_pns: + for fn in pkg_pn[pn]: + # There's no point checking multiple BBCLASSEXTENDed variants of the same recipe + realfn, _, _ = bb.cache.virtualfn2realfn(fn) + if realfn not in pns: + pns.append(realfn) + + + def func(fn): + result = [] + data = tinfoil.parse_recipe_file(fn) + for key in data.keys(): + if data.getVarFlag(key, "func", True) and not data.getVarFlag(key, "python", True): + script = data.getVar(key, False) + if not script: continue + #print ("%s:%s" % (fn, key)) + r = process(fn, key, script) + if r: result.extend(r) + return fn, result + + print("Scanning scripts...\n") + import multiprocessing + pool = multiprocessing.Pool() + for pn,results in pool.imap(func, pns): + if results: + print(pn) + for message,source in results: + print(" %s\n %s" % (message, source)) + print()
Signature · 1 line
2.8.1
PATCH 2/2 rpm: remove redundant removals
--- meta/recipes-devtools/rpm/rpm_5.4.16.bb | 45 --------------------------------- 1 file changed, 45 deletions(-)
meta/recipes-devtools/rpm/rpm_5.4.16.bb +0 -45
@@ -554,54 +554,11 @@ do_install_append() { install -m 0755 ${WORKDIR}/pythondeps.sh ${D}/${libdir}/rpm/pythondeps.sh install -m 0755 ${WORKDIR}/perfile_rpmdeps.sh ${D}/${libdir}/rpm/perfile_rpmdeps.sh - # Remove unpackaged files (based on list in rpm.spec) - rm -f ${D}/${libdir}/rpm/{Specfile.pm,cpanflute,cpanflute2,rpmdiff,rpmdiff.cgi,sql.prov,sql.req,tcl.req,trpm} - - rm -f ${D}/${mandir}/man8/rpmcache.8* - rm -f ${D}/${mandir}/man8/rpmgraph.8* - rm -f ${D}/${mandir}/*/man8/rpmcache.8* - rm -f ${D}/${mandir}/*/man8/rpmgraph.8* - rm -rf ${D}/${mandir}/{fr,ko} - - rm -f ${D}/${includedir}/popt.h - rm -f ${D}/${libdir}/libpopt.* - rm -f ${D}/${libdir}/pkgconfig/popt.pc - rm -f ${D}/${datadir}/locale/*/LC_MESSAGES/popt.mo - rm -f ${D}/${mandir}/man3/popt.3 - - rm -f ${D}/${mandir}/man1/xar.1* - rm -f ${D}/${bindir}/xar - rm -rf ${D}/${includedir}/xar - rm -f ${D}/${libdir}/libxar* - - rm -f ${D}/${bindir}/lz* - rm -f ${D}/${bindir}/unlzma - rm -f ${D}/${bindir}/unxz - rm -f ${D}/${bindir}/xz* - rm -rf ${D}/${includedir}/lzma* - rm -f ${D}/${mandir}/man1/lz*.1 - rm -f ${D}/${libdir}/pkgconfig/liblzma* - - rm -f ${D}${PYTHON_SITEPACKAGES_DIR}/*.a rm -f ${D}${PYTHON_SITEPACKAGES_DIR}/*.la - rm -f ${D}${PYTHON_SITEPACKAGES_DIR}/rpm/*.a rm -f ${D}${PYTHON_SITEPACKAGES_DIR}/rpm/*.la - #find ${D}/${libdir}/perl5 -type f -a \( -name perllocal.pod -o -name .packlist \ - # -o \( -name '*.bs' -a -empty \) \) -exec rm -f {} ';' - #find ${D}/${libdir}/perl5 -type d -depth -exec rmdir {} 2>/dev/null ';' - rm -f ${D}/${libdir}/rpm/dbconvert.sh - rm -f ${D}/${libdir}/rpm/libsqldb.* - - # We don't want, nor need the Mandriva multiarch items - rm -f ${D}/${bindir}/multiarch-dispatch - rm -f ${D}/${bindir}/multiarch-platform - rm -f ${D}/${libdir}/rpm/check-multiarch-files - rm -f ${D}/${libdir}/rpm/mkmultiarch - rm -f ${D}/${includedir}/multiarch-dispatch.h - rm -f ${D}/${libdir}/rpm/gstreamer.sh rm -f ${D}/${libdir}/rpm/gem_helper.rb rm -f ${D}/${libdir}/rpm/rubygems.rb
@@ -610,12 +567,10 @@ do_install_append() { rm -f ${D}/${libdir}/rpm/macros.d/kernel rm -f ${D}/${libdir}/rpm/macros.d/gstreamer rm -f ${D}/${libdir}/rpm/bin/mgo - rm -f ${D}/${libdir}/rpm/bin/dbconvert rm -f ${D}/${libdir}/rpm/bin/pom2spec rm -rf ${D}/var/lib/wdj ${D}/var/cache/wdj rm -f ${D}/${libdir}/rpm/bin/api-sanity-checker.pl - } do_install_append_class-target() {
Signature · 1 line
2.8.1
Re: [PATCH 2/2] rpm: remove redundant removals
Why are these redundant? Need a touch more info.
Cheers,
Richard
Review
Quoted 10 lines · Ross Burton
scripts/verify-bashisms new file +6
-0+ fn = tempfile.NamedTemporaryFile(mode="w+t") + fn.write(script) + fn.flush() + + try: + subprocess.check_output(("checkbashisms.pl", fn.name),universal_newlines=True, stderr=subprocess.STDOUT)
(quoted diff) +2
-0+ # No bashisms, so just return + return
Afaik, you should be using ‘with’ to ensure the temporary files are cleaned up after use.
Signature · 5 lines
Christopher Larson clarson at kergoth dot com Founder - BitBake, OpenEmbedded, OpenZaurus Maintainer - Tslib Senior Software Engineer, Mentor Graphics
Quoted 3 lines · Christopher Larson
Afaik, you should be using ‘with’ to ensure the temporary files are cleaned up after use.
Yes, probably, this code actually predates the Py3 migration. Didn't get any warnings though.
Ross
Re: [PATCH 2/2] rpm: remove redundant removals
In reply to Richard PurdieQuoted 2 lines · Richard Purdie
Why are these redundant? Need a touch more info.
Erm, I appear to have sent the patch from my WIP branch instead of the one with the longer commit message. V2 incoming if I can find it.
Ross