blob: fc3677c6ed0a41c482f84a45478ab851e27faa8e [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
Patrick Williams92b42cb2022-09-03 06:53:57 -05003# Copyright OpenEmbedded Contributors
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
6#
Patrick Williamsc0f7c042017-02-23 20:41:17 -06007
8import sys, os, subprocess, re, shutil
9
Andrew Geissler9aee5002022-03-30 16:27:02 +000010allowed = (
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011 # type is supported by dash
12 'if type systemctl >/dev/null 2>/dev/null; then',
13 'if type systemd-tmpfiles >/dev/null 2>/dev/null; then',
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014 'type update-rc.d >/dev/null 2>/dev/null; then',
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015 'command -v',
16 # HOSTNAME is set locally
17 'buildhistory_single_commit "$CMDLINE" "$HOSTNAME"',
18 # False-positive, match is a grep not shell expression
19 'grep "^$groupname:[^:]*:[^:]*:\\([^,]*,\\)*$username\\(,[^,]*\\)*"',
20 # TODO verify dash's '. script args' behaviour
21 '. $target_sdk_dir/${oe_init_build_env_path} $target_sdk_dir >> $LOGFILE'
22 )
23
Andrew Geissler9aee5002022-03-30 16:27:02 +000024def is_allowed(s):
25 for w in allowed:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060026 if w in s:
27 return True
28 return False
29
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030SCRIPT_LINENO_RE = re.compile(r' line (\d+) ')
31BASHISM_WARNING = re.compile(r'^(possible bashism in.*)$', re.MULTILINE)
32
33def process(filename, function, lineno, script):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034 import tempfile
35
36 if not script.startswith("#!"):
37 script = "#! /bin/sh\n" + script
38
39 fn = tempfile.NamedTemporaryFile(mode="w+t")
40 fn.write(script)
41 fn.flush()
42
43 try:
44 subprocess.check_output(("checkbashisms.pl", fn.name), universal_newlines=True, stderr=subprocess.STDOUT)
45 # No bashisms, so just return
46 return
47 except subprocess.CalledProcessError as e:
48 # TODO check exit code is 1
49
50 # Replace the temporary filename with the function and split it
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 output = e.output.replace(fn.name, function)
52 if not output or not output.startswith('possible bashism'):
53 # Probably starts with or contains only warnings. Dump verbatim
Andrew Geissler9aee5002022-03-30 16:27:02 +000054 # with one space indention. Can't do the splitting and allowed
Brad Bishop6e60e8b2018-02-01 10:27:11 -050055 # checking below.
56 return '\n'.join([filename,
57 ' Unexpected output from checkbashisms.pl'] +
58 [' ' + x for x in output.splitlines()])
Patrick Williamsc0f7c042017-02-23 20:41:17 -060059
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 # We know that the first line matches and that therefore the first
61 # list entry will be empty - skip it.
62 output = BASHISM_WARNING.split(output)[1:]
63 # Turn the output into a single string like this:
64 # /.../foobar.bb
65 # possible bashism in updatercd_postrm line 2 (type):
66 # if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
67 # ...
68 # ...
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 result = []
Andrew Geissler9aee5002022-03-30 16:27:02 +000070 # Check the results against the allowed list
Patrick Williamsc0f7c042017-02-23 20:41:17 -060071 for message, source in zip(output[0::2], output[1::2]):
72 if not is_whitelisted(source):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 if lineno is not None:
74 message = SCRIPT_LINENO_RE.sub(lambda m: ' line %d ' % (int(m.group(1)) + int(lineno) - 1),
75 message)
76 result.append(' ' + message.strip())
77 result.extend([' %s' % x for x in source.splitlines()])
78 if result:
79 result.insert(0, filename)
80 return '\n'.join(result)
81 else:
82 return None
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083
84def get_tinfoil():
85 scripts_path = os.path.dirname(os.path.realpath(__file__))
86 lib_path = scripts_path + '/lib'
87 sys.path = sys.path + [lib_path]
88 import scriptpath
89 scriptpath.add_bitbake_lib_path()
90 import bb.tinfoil
91 tinfoil = bb.tinfoil.Tinfoil()
92 tinfoil.prepare()
93 # tinfoil.logger.setLevel(logging.WARNING)
94 return tinfoil
95
96if __name__=='__main__':
Brad Bishop316dfdd2018-06-25 12:45:53 -040097 import argparse, shutil
98
99 parser = argparse.ArgumentParser(description='Bashim detector for shell fragments in recipes.')
100 parser.add_argument("recipes", metavar="RECIPE", nargs="*", help="recipes to check (if not specified, all will be checked)")
101 parser.add_argument("--verbose", default=False, action="store_true")
102 args = parser.parse_args()
103
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 if shutil.which("checkbashisms.pl") is None:
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500105 print("Cannot find checkbashisms.pl on $PATH, get it from https://salsa.debian.org/debian/devscripts/raw/master/scripts/checkbashisms.pl")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 sys.exit(1)
107
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 # The order of defining the worker function,
109 # initializing the pool and connecting to the
110 # bitbake server is crucial, don't change it.
111 def func(item):
112 (filename, key, lineno), script = item
Brad Bishop316dfdd2018-06-25 12:45:53 -0400113 if args.verbose:
114 print("Scanning %s:%s" % (filename, key))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 return process(filename, key, lineno, script)
116
117 import multiprocessing
118 pool = multiprocessing.Pool()
119
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 tinfoil = get_tinfoil()
121
122 # This is only the default configuration and should iterate over
123 # recipecaches to handle multiconfig environments
124 pkg_pn = tinfoil.cooker.recipecaches[""].pkg_pn
125
Brad Bishop316dfdd2018-06-25 12:45:53 -0400126 if args.recipes:
127 initial_pns = args.recipes
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600128 else:
129 initial_pns = sorted(pkg_pn)
130
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 pns = set()
132 scripts = {}
133 print("Generating scripts...")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600134 for pn in initial_pns:
135 for fn in pkg_pn[pn]:
136 # There's no point checking multiple BBCLASSEXTENDed variants of the same recipe
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 # (at least in general - there is some risk that the variants contain different scripts)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600138 realfn, _, _ = bb.cache.virtualfn2realfn(fn)
139 if realfn not in pns:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 pns.add(realfn)
141 data = tinfoil.parse_recipe_file(realfn)
142 for key in data.keys():
143 if data.getVarFlag(key, "func") and not data.getVarFlag(key, "python"):
144 script = data.getVar(key, False)
145 if script:
146 filename = data.getVarFlag(key, "filename")
147 lineno = data.getVarFlag(key, "lineno")
148 # There's no point in checking a function multiple
149 # times just because different recipes include it.
150 # We identify unique scripts by file, name, and (just in case)
151 # line number.
152 attributes = (filename or realfn, key, lineno)
153 scripts.setdefault(attributes, script)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600154
155
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 print("Scanning scripts...\n")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500157 for result in pool.imap(func, scripts.items()):
158 if result:
159 print(result)
160 tinfoil.shutdown()