import logging import os import subprocess # Create Cppcheck project file import sys def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None, addon=None): cppcheck_xml = ('\n' '\n') if root_path: cppcheck_xml += ' \n' if import_project: cppcheck_xml += ' ' + import_project + '\n' if paths: cppcheck_xml += ' \n' for path in paths: cppcheck_xml += ' \n' cppcheck_xml += ' \n' if exclude_paths: cppcheck_xml += ' \n' for path in exclude_paths: cppcheck_xml += ' \n' cppcheck_xml += ' \n' if suppressions: cppcheck_xml += ' \n' for suppression in suppressions: cppcheck_xml += ' \n' cppcheck_xml += ' \n' if addon: cppcheck_xml += ' \n' cppcheck_xml += ' %s\n' % addon cppcheck_xml += ' \n' cppcheck_xml += '\n' f = open(project_file, 'wt') f.write(cppcheck_xml) f.close() def __lookup_cppcheck_exe(): # path the script is located in script_path = os.path.dirname(os.path.realpath(__file__)) exe_name = "cppcheck" if sys.platform == "win32": exe_name += ".exe" for base in (script_path + '/../../', './'): for path in ('', 'bin/', 'bin/debug/'): exe_path = base + path + exe_name if os.path.isfile(exe_path): print("using '{}'".format(exe_path)) return exe_path return None # Run Cppcheck with args def cppcheck(args, env=None): exe = __lookup_cppcheck_exe() assert exe is not None, 'no cppcheck binary found' logging.info(exe + ' ' + ' '.join(args)) p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) comm = p.communicate() stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') if stdout.find('\nActive checkers:') > 0: stdout = stdout[:1 + stdout.find('\nActive checkers:')] return p.returncode, stdout, stderr def assert_cppcheck(args, ec_exp=None, out_exp=None, err_exp=None, env=None): exitcode, stdout, stderr = cppcheck(args, env) if ec_exp is not None: assert exitcode == ec_exp, stdout if out_exp is not None: out_lines = stdout.splitlines() assert out_lines == out_exp, stdout if err_exp is not None: err_lines = stderr.splitlines() assert err_lines == err_exp, stderr