donate_cpu_lib.py: some cleanups (#3490)

This commit is contained in:
Oliver Stöneberg 2021-10-09 14:51:24 +02:00 committed by GitHub
parent e13eba86e5
commit 3f8e523c71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,13 +10,12 @@ import re
import signal import signal
import tarfile import tarfile
import shlex import shlex
import psutil
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/ # Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic # Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
# changes) # changes)
CLIENT_VERSION = "1.3.12" CLIENT_VERSION = "1.3.13"
# Timeout for analysis with Cppcheck in seconds # Timeout for analysis with Cppcheck in seconds
CPPCHECK_TIMEOUT = 30 * 60 CPPCHECK_TIMEOUT = 30 * 60
@ -91,9 +90,9 @@ def compile_version(work_path, jobs, version):
subprocess.call(['make', jobs, 'MATCHCOMPILER=yes', 'CXXFLAGS=-O2 -g']) subprocess.call(['make', jobs, 'MATCHCOMPILER=yes', 'CXXFLAGS=-O2 -g'])
if os.path.isfile(work_path + '/cppcheck/cppcheck'): if os.path.isfile(work_path + '/cppcheck/cppcheck'):
os.mkdir(work_path + '/' + version) os.mkdir(work_path + '/' + version)
destPath = work_path + '/' + version + '/' dest_path = work_path + '/' + version + '/'
subprocess.call(['cp', '-R', work_path + '/cppcheck/cfg', destPath]) subprocess.call(['cp', '-R', work_path + '/cppcheck/cfg', dest_path])
subprocess.call(['cp', 'cppcheck', destPath]) subprocess.call(['cp', 'cppcheck', dest_path])
subprocess.call(['git', 'checkout', 'main']) subprocess.call(['git', 'checkout', 'main'])
try: try:
subprocess.call([work_path + '/' + version + '/cppcheck', '--version']) subprocess.call([work_path + '/' + version + '/cppcheck', '--version'])
@ -166,19 +165,19 @@ def handle_remove_readonly(func, path, exc):
func(path) func(path)
def remove_tree(folderName): def remove_tree(folder_name):
if not os.path.exists(folderName): if not os.path.exists(folder_name):
return return
count = 5 count = 5
while count > 0: while count > 0:
count -= 1 count -= 1
try: try:
shutil.rmtree(folderName, onerror=handle_remove_readonly) shutil.rmtree(folder_name, onerror=handle_remove_readonly)
break break
except OSError as err: except OSError as err:
time.sleep(30) time.sleep(30)
if count == 0: if count == 0:
print('Failed to cleanup {}: {}'.format(folderName, err)) print('Failed to cleanup {}: {}'.format(folder_name, err))
sys.exit(1) sys.exit(1)
@ -215,7 +214,6 @@ def unpack_package(work_path, tgz):
temp_path = work_path + '/temp' temp_path = work_path + '/temp'
remove_tree(temp_path) remove_tree(temp_path)
os.mkdir(temp_path) os.mkdir(temp_path)
os.chdir(temp_path)
found = False found = False
if tarfile.is_tarfile(tgz): if tarfile.is_tarfile(tgz):
with tarfile.open(tgz) as tf: with tarfile.open(tgz) as tf:
@ -226,13 +224,12 @@ def unpack_package(work_path, tgz):
elif member.name.lower().endswith(('.c', '.cpp', '.cxx', '.cc', '.c++', '.h', '.hpp', elif member.name.lower().endswith(('.c', '.cpp', '.cxx', '.cc', '.c++', '.h', '.hpp',
'.h++', '.hxx', '.hh', '.tpp', '.txx', '.ipp', '.ixx', '.qml')): '.h++', '.hxx', '.hh', '.tpp', '.txx', '.ipp', '.ixx', '.qml')):
try: try:
tf.extract(member.name) tf.extract(member.name, temp_path)
found = True found = True
except OSError: except OSError:
pass pass
except AttributeError: except AttributeError:
pass pass
os.chdir(work_path)
return found return found
@ -254,7 +251,7 @@ def has_include(path, includes):
def run_command(cmd): def run_command(cmd):
print(cmd) print(cmd)
startTime = time.time() start_time = time.time()
comm = None comm = None
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid) p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
try: try:
@ -262,6 +259,7 @@ def run_command(cmd):
return_code = p.returncode return_code = p.returncode
p = None p = None
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
import psutil
return_code = RETURN_CODE_TIMEOUT return_code = RETURN_CODE_TIMEOUT
# terminate all the child processes so we get messages about which files were hanging # terminate all the child processes so we get messages about which files were hanging
child_procs = psutil.Process(p.pid).children(recursive=True) child_procs = psutil.Process(p.pid).children(recursive=True)
@ -281,7 +279,7 @@ def run_command(cmd):
stop_time = time.time() stop_time = time.time()
stdout = comm[0].decode(encoding='utf-8', errors='ignore') stdout = comm[0].decode(encoding='utf-8', errors='ignore')
stderr = comm[1].decode(encoding='utf-8', errors='ignore') stderr = comm[1].decode(encoding='utf-8', errors='ignore')
elapsed_time = stop_time - startTime elapsed_time = stop_time - start_time
return return_code, stdout, stderr, elapsed_time return return_code, stdout, stderr, elapsed_time
@ -293,11 +291,14 @@ def scan_package(work_path, cppcheck_path, jobs, libraries):
if os.path.exists(os.path.join(cppcheck_path, 'cfg', library + '.cfg')): if os.path.exists(os.path.join(cppcheck_path, 'cfg', library + '.cfg')):
libs += '--library=' + library + ' ' libs += '--library=' + library + ' '
dir_to_scan = 'temp'
# Reference for GNU C: https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html # Reference for GNU C: https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
options = libs + jobs + ' --showtime=top5 --check-library --inconclusive --enable=style,information --template=daca2 -rp=temp' options = libs + ' --showtime=top5 --check-library --inconclusive --enable=style,information --template=daca2'
options += ' -D__GNUC__ --platform=unix64 temp' options += ' -D__GNUC__ --platform=unix64'
options += ' -rp={}'.format(dir_to_scan)
cppcheck_cmd = cppcheck_path + '/cppcheck' + ' ' + options cppcheck_cmd = cppcheck_path + '/cppcheck' + ' ' + options
cmd = 'nice ' + cppcheck_cmd cmd = 'nice ' + cppcheck_cmd + ' ' + jobs + ' ' + dir_to_scan
returncode, stdout, stderr, elapsed_time = run_command(cmd) returncode, stdout, stderr, elapsed_time = run_command(cmd)
# collect messages # collect messages
@ -350,7 +351,7 @@ def scan_package(work_path, cppcheck_path, jobs, libraries):
stacktrace = '' stacktrace = ''
if cppcheck_path == 'cppcheck': if cppcheck_path == 'cppcheck':
# re-run within gdb to get a stacktrace # re-run within gdb to get a stacktrace
cmd = 'gdb --batch --eval-command=run --eval-command="bt 50" --return-child-result --args ' + cppcheck_cmd + " -j1" cmd = 'gdb --batch --eval-command=run --eval-command="bt 50" --return-child-result --args ' + cppcheck_cmd + " -j1 " + dir_to_scan
_, st_stdout, _, _ = run_command(cmd) _, st_stdout, _, _ = run_command(cmd)
gdb_pos = st_stdout.find(" received signal") gdb_pos = st_stdout.find(" received signal")
if not gdb_pos == -1: if not gdb_pos == -1:
@ -476,11 +477,11 @@ def upload_info(package, info_output, server_address):
def get_libraries(): def get_libraries():
libraries = ['posix', 'gnu'] libraries = ['posix', 'gnu']
library_includes = {'boost': ['<boost/'], library_includes = {'boost': ['<boost/'],
'bsd': ['<sys/queue.h>','<sys/tree.h>','<bsd/','<fts.h>','<db.h>','<err.h>','<vis.h>'], 'bsd': ['<sys/queue.h>', '<sys/tree.h>', '<bsd/', '<fts.h>', '<db.h>', '<err.h>', '<vis.h>'],
'cairo': ['<cairo.h>'], 'cairo': ['<cairo.h>'],
'cppunit': ['<cppunit/'], 'cppunit': ['<cppunit/'],
'icu': ['<unicode/', '"unicode/'], 'icu': ['<unicode/', '"unicode/'],
'ginac': ['<ginac/','"ginac/'], 'ginac': ['<ginac/', '"ginac/'],
'googletest': ['<gtest/gtest.h>'], 'googletest': ['<gtest/gtest.h>'],
'gtk': ['<gtk', '<glib.h>', '<glib-', '<glib/', '<gnome'], 'gtk': ['<gtk', '<glib.h>', '<glib-', '<glib/', '<gnome'],
'kde': ['<KGlobal>', '<KApplication>', '<KDE/'], 'kde': ['<KGlobal>', '<KApplication>', '<KDE/'],