2018-08-23 21:31:02 +02:00
|
|
|
# Donate CPU
|
|
|
|
#
|
|
|
|
# A script a user can run to donate CPU to cppcheck project
|
|
|
|
#
|
2018-09-06 13:15:54 +02:00
|
|
|
# Syntax: donate-cpu.py [-jN] [--stop-time=HH:MM] [--work-path=path]
|
2018-08-31 14:28:42 +02:00
|
|
|
# -jN Use N threads in compilation/analysis. Default is 1.
|
|
|
|
# --stop-time=HH:MM Stop analysis when time has passed. Default is that you must terminate the script.
|
2018-09-06 13:15:54 +02:00
|
|
|
# --work-path=path Work folder path. Default path is cppcheck-donate-cpu-workfolder in your home folder.
|
2018-08-31 14:28:42 +02:00
|
|
|
#
|
|
|
|
# What this script does:
|
2018-08-23 21:31:02 +02:00
|
|
|
# 1. Check requirements
|
|
|
|
# 2. Pull & compile Cppcheck
|
|
|
|
# 3. Select a package
|
|
|
|
# 4. Download package
|
|
|
|
# 5. Analyze source code
|
|
|
|
# 6. Upload results
|
|
|
|
# 7. Repeat from step 2
|
2018-08-31 14:28:42 +02:00
|
|
|
#
|
|
|
|
# Quick start: just run this script without any arguments
|
2018-08-23 21:31:02 +02:00
|
|
|
|
|
|
|
import shutil
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import time
|
2018-08-24 13:20:38 +02:00
|
|
|
import re
|
2018-09-15 18:56:46 +02:00
|
|
|
import tarfile
|
2018-08-23 21:31:02 +02:00
|
|
|
|
|
|
|
def checkRequirements():
|
|
|
|
result = True
|
2018-09-15 18:56:46 +02:00
|
|
|
for app in ['g++', 'git', 'make', 'wget']:
|
2018-08-23 21:31:02 +02:00
|
|
|
try:
|
|
|
|
subprocess.call([app, '--version'])
|
|
|
|
except OSError:
|
|
|
|
print(app + ' is required')
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2018-08-24 18:49:11 +02:00
|
|
|
def getCppcheck(cppcheckPath):
|
2018-08-23 21:31:02 +02:00
|
|
|
print('Get Cppcheck..')
|
2018-08-26 16:23:42 +02:00
|
|
|
for i in range(5):
|
|
|
|
if os.path.exists(cppcheckPath):
|
|
|
|
os.chdir(cppcheckPath)
|
|
|
|
subprocess.call(['git', 'checkout', '-f'])
|
|
|
|
subprocess.call(['git', 'pull'])
|
|
|
|
else:
|
|
|
|
subprocess.call(['git', 'clone', 'http://github.com/danmar/cppcheck.git', cppcheckPath])
|
|
|
|
if not os.path.exists(cppcheckPath):
|
|
|
|
print('Failed to clone, will try again in 10 minutes..')
|
|
|
|
time.sleep(600)
|
|
|
|
continue
|
|
|
|
time.sleep(2)
|
|
|
|
return True
|
|
|
|
return False
|
2018-08-24 18:49:11 +02:00
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-31 14:28:42 +02:00
|
|
|
def compile_version(workPath, jobs, version):
|
2018-08-24 21:07:50 +02:00
|
|
|
if os.path.isfile(workPath + '/' + version + '/cppcheck'):
|
2018-08-26 16:23:42 +02:00
|
|
|
return True
|
2018-08-24 21:07:50 +02:00
|
|
|
os.chdir(workPath + '/cppcheck')
|
|
|
|
subprocess.call(['git', 'checkout', version])
|
|
|
|
subprocess.call(['make', 'clean'])
|
2018-08-31 14:28:42 +02:00
|
|
|
subprocess.call(['make', jobs, 'SRCDIR=build', 'CXXFLAGS=-O2'])
|
2018-08-24 21:07:50 +02:00
|
|
|
if os.path.isfile(workPath + '/cppcheck/cppcheck'):
|
|
|
|
os.mkdir(workpath + '/' + version)
|
|
|
|
destPath = workpath + '/' + version + '/'
|
|
|
|
subprocess.call(['cp', '-R', workPath + '/cppcheck/cfg', destPath])
|
|
|
|
subprocess.call(['cp', 'cppcheck', destPath])
|
|
|
|
subprocess.call(['git', 'checkout', 'master'])
|
2018-08-26 16:23:42 +02:00
|
|
|
try:
|
|
|
|
subprocess.call([workPath + '/' + version + '/cppcheck', '--version'])
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
return True
|
2018-08-24 21:07:50 +02:00
|
|
|
|
|
|
|
|
2018-08-31 14:28:42 +02:00
|
|
|
def compile(cppcheckPath, jobs):
|
2018-08-23 21:31:02 +02:00
|
|
|
print('Compiling Cppcheck..')
|
2018-08-24 13:20:38 +02:00
|
|
|
try:
|
2018-08-24 18:49:11 +02:00
|
|
|
os.chdir(cppcheckPath)
|
2018-08-31 14:28:42 +02:00
|
|
|
subprocess.call(['make', jobs, 'SRCDIR=build', 'CXXFLAGS=-O2'])
|
2018-08-24 18:49:11 +02:00
|
|
|
subprocess.call([cppcheckPath + '/cppcheck', '--version'])
|
2018-08-24 13:20:38 +02:00
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
return True
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-24 18:49:11 +02:00
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
def getPackage():
|
|
|
|
print('Connecting to server to get assigned work..')
|
|
|
|
package = None
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
server_address = ('cppcheck.osuosl.org', 8000)
|
|
|
|
sock.connect(server_address)
|
|
|
|
try:
|
2018-08-25 20:34:43 +02:00
|
|
|
sock.send(b'get\n')
|
2018-08-23 21:31:02 +02:00
|
|
|
package = sock.recv(256)
|
|
|
|
finally:
|
|
|
|
sock.close()
|
2018-08-25 20:34:43 +02:00
|
|
|
return package.decode('utf-8')
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-24 18:49:11 +02:00
|
|
|
|
2018-09-01 17:04:34 +02:00
|
|
|
|
|
|
|
def handleRemoveReadonly(func, path, exc):
|
|
|
|
import stat
|
|
|
|
if not os.access(path, os.W_OK):
|
|
|
|
# Is the error an access error ?
|
|
|
|
os.chmod(path, stat.S_IWUSR)
|
|
|
|
func(path)
|
|
|
|
|
|
|
|
|
|
|
|
def removeTree(folderName):
|
|
|
|
if not os.path.exists(folderName):
|
|
|
|
return
|
|
|
|
count = 5
|
|
|
|
while count > 0:
|
|
|
|
count -= 1
|
|
|
|
try:
|
|
|
|
shutil.rmtree(folderName, onerror=handleRemoveReadonly)
|
|
|
|
break
|
|
|
|
except OSError as err:
|
|
|
|
time.sleep(30)
|
|
|
|
if count == 0:
|
|
|
|
print('Failed to cleanup {}: {}'.format(folderName, err))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
def wget(url, destfile):
|
2018-09-07 15:59:59 +02:00
|
|
|
if os.path.exists(destfile):
|
|
|
|
if os.path.isfile(destfile):
|
|
|
|
os.remove(destfile)
|
|
|
|
else:
|
|
|
|
print('Error: ' + destfile + ' exists but it is not a file! Please check the path and delete it manually.')
|
|
|
|
sys.exit(1)
|
2018-08-23 21:31:02 +02:00
|
|
|
subprocess.call(
|
|
|
|
['wget', '--tries=10', '--timeout=300', '-O', destfile, url])
|
|
|
|
if os.path.isfile(destfile):
|
|
|
|
return True
|
|
|
|
print('Sleep for 10 seconds..')
|
|
|
|
time.sleep(10)
|
|
|
|
return False
|
|
|
|
|
2018-08-24 21:07:50 +02:00
|
|
|
|
|
|
|
def downloadPackage(workPath, package):
|
2018-08-23 21:31:02 +02:00
|
|
|
print('Download package ' + package)
|
|
|
|
destfile = workPath + '/temp.tgz'
|
|
|
|
if not wget(package, destfile):
|
2018-08-24 14:46:59 +02:00
|
|
|
if not wget(package, destfile):
|
2018-08-23 21:31:02 +02:00
|
|
|
return None
|
2018-08-24 21:07:50 +02:00
|
|
|
return destfile
|
|
|
|
|
|
|
|
|
|
|
|
def unpackPackage(workPath, tgz):
|
2018-08-23 21:31:02 +02:00
|
|
|
print('Unpacking..')
|
2018-08-24 21:07:50 +02:00
|
|
|
tempPath = workPath + '/temp'
|
2018-09-01 17:04:34 +02:00
|
|
|
removeTree(tempPath)
|
2018-08-23 21:31:02 +02:00
|
|
|
os.mkdir(tempPath)
|
|
|
|
os.chdir(tempPath)
|
2018-09-15 18:56:46 +02:00
|
|
|
if tarfile.is_tarfile(tgz):
|
|
|
|
tf = tarfile.open(tgz)
|
|
|
|
for member in tf:
|
|
|
|
if member.name.startswith(('/', '..')):
|
|
|
|
# Skip dangerous file names
|
|
|
|
continue
|
|
|
|
elif member.name.lower().endswith(('.c', '.cl', '.cpp', '.cxx', '.cc', '.c++', '.h', '.hpp', '.hxx', '.hh', '.tpp', '.txx')):
|
|
|
|
tf.extract(member.name)
|
|
|
|
print(member.name)
|
|
|
|
tf.close()
|
2018-08-23 21:31:02 +02:00
|
|
|
os.chdir(workPath)
|
2018-08-24 21:07:50 +02:00
|
|
|
|
|
|
|
|
2018-08-31 14:28:42 +02:00
|
|
|
def scanPackage(workPath, cppcheck, jobs):
|
2018-08-23 21:31:02 +02:00
|
|
|
print('Analyze..')
|
2018-08-24 21:07:50 +02:00
|
|
|
os.chdir(workPath)
|
2018-09-09 13:16:39 +02:00
|
|
|
cmd = 'nice ' + cppcheck + ' ' + jobs + ' -D__GCC__ --inconclusive --enable=style --library=posix --platform=unix64 --template={file}:{line}:{inconclusive:inconclusive:}{message}[{id}] -rp=temp temp'
|
2018-08-23 21:31:02 +02:00
|
|
|
print(cmd)
|
2018-08-29 06:51:33 +02:00
|
|
|
startTime = time.time()
|
2018-08-23 21:31:02 +02:00
|
|
|
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
comm = p.communicate()
|
2018-08-29 06:51:33 +02:00
|
|
|
stopTime = time.time()
|
2018-08-29 22:07:48 +02:00
|
|
|
stdout = comm[0].decode(encoding='utf-8', errors='ignore')
|
|
|
|
stderr = comm[1].decode(encoding='utf-8', errors='ignore')
|
|
|
|
if p.returncode != 0 and 'cppcheck: error: could not find or open any of the paths given.' not in stdout:
|
|
|
|
# Crash!
|
|
|
|
print('Crash!')
|
|
|
|
return -1, '', -1
|
2018-08-29 06:51:33 +02:00
|
|
|
elapsedTime = stopTime - startTime
|
2018-08-24 13:20:38 +02:00
|
|
|
count = 0
|
2018-08-29 22:07:48 +02:00
|
|
|
for line in stderr.split('\n'):
|
2018-08-26 16:23:42 +02:00
|
|
|
if re.match(r'.*:[0-9]+:.*\]$', line):
|
2018-08-24 13:20:38 +02:00
|
|
|
count += 1
|
2018-08-26 16:23:42 +02:00
|
|
|
print('Number of issues: ' + str(count))
|
2018-08-29 22:07:48 +02:00
|
|
|
return count, stderr, elapsedTime
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-24 21:07:50 +02:00
|
|
|
|
2018-08-26 13:42:01 +02:00
|
|
|
def diffResults(workPath, ver1, results1, ver2, results2):
|
|
|
|
print('Diff results..')
|
|
|
|
ret = ''
|
|
|
|
r1 = sorted(results1.split('\n'))
|
|
|
|
r2 = sorted(results2.split('\n'))
|
|
|
|
i1 = 0
|
|
|
|
i2 = 0
|
|
|
|
while i1 < len(r1) and i2 < len(r2):
|
|
|
|
if r1[i1] == r2[i2]:
|
|
|
|
i1 += 1
|
|
|
|
i2 += 1
|
|
|
|
elif r1[i1] < r2[i2]:
|
|
|
|
ret += ver1 + ' ' + r1[i1] + '\n'
|
|
|
|
i1 += 1
|
|
|
|
else:
|
|
|
|
ret += ver2 + ' ' + r2[i2] + '\n'
|
|
|
|
i2 += 1
|
|
|
|
while i1 < len(r1):
|
|
|
|
ret += ver1 + ' ' + r1[i1] + '\n'
|
|
|
|
i1 += 1
|
|
|
|
while i2 < len(r2):
|
|
|
|
ret += ver2 + ' ' + r2[i2] + '\n'
|
|
|
|
i2 += 1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-08-25 10:25:05 +02:00
|
|
|
def sendAll(connection, data):
|
2018-08-25 20:34:43 +02:00
|
|
|
bytes = data.encode()
|
|
|
|
while bytes:
|
|
|
|
num = connection.send(bytes)
|
|
|
|
if num < len(bytes):
|
|
|
|
bytes = bytes[num:]
|
2018-08-25 10:25:05 +02:00
|
|
|
else:
|
2018-08-25 20:34:43 +02:00
|
|
|
bytes = None
|
2018-08-25 10:25:05 +02:00
|
|
|
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
def uploadResults(package, results):
|
|
|
|
print('Uploading results..')
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
server_address = ('cppcheck.osuosl.org', 8000)
|
|
|
|
sock.connect(server_address)
|
|
|
|
try:
|
2018-08-25 18:38:51 +02:00
|
|
|
sendAll(sock, 'write\n' + package + '\n' + results + '\nDONE')
|
2018-08-23 21:31:02 +02:00
|
|
|
sock.close()
|
2018-08-25 20:43:20 +02:00
|
|
|
except socket.error:
|
2018-08-25 18:38:51 +02:00
|
|
|
pass
|
2018-08-23 21:31:02 +02:00
|
|
|
return package
|
|
|
|
|
2018-08-31 14:28:42 +02:00
|
|
|
jobs = '-j1'
|
2018-08-29 11:08:56 +02:00
|
|
|
stopTime = None
|
2018-08-31 14:28:42 +02:00
|
|
|
workpath = os.path.expanduser('~/cppcheck-donate-cpu-workfolder')
|
2018-08-29 11:08:56 +02:00
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
# --stop-time=12:00 => run until ~12:00 and then stop
|
|
|
|
if arg.startswith('--stop-time='):
|
|
|
|
stopTime = arg[-5:]
|
2018-08-31 14:28:42 +02:00
|
|
|
print('Stop time:' + stopTime)
|
|
|
|
elif arg.startswith('-j'):
|
|
|
|
jobs = arg
|
|
|
|
print('Jobs:' + jobs[2:])
|
|
|
|
elif arg.startswith('--work-path='):
|
|
|
|
workpath = arg[arg.find('=')+1:]
|
|
|
|
print('workpath:' + workpath)
|
|
|
|
if not os.path.exists(workpath):
|
|
|
|
print('work path does not exist!')
|
|
|
|
sys.exit(1)
|
|
|
|
elif arg == '--help':
|
|
|
|
print('Donate CPU to Cppcheck project')
|
|
|
|
print('')
|
2018-09-06 13:15:54 +02:00
|
|
|
print('Syntax: donate-cpu.py [-jN] [--stop-time=HH:MM] [--work-path=path]')
|
2018-08-31 14:28:42 +02:00
|
|
|
print(' -jN Use N threads in compilation/analysis. Default is 1.')
|
|
|
|
print(' --stop-time=HH:MM Stop analysis when time has passed. Default is that you must terminate the script.')
|
2018-09-06 13:15:54 +02:00
|
|
|
print(' --work-path=path Work folder path. Default path is ' + workpath)
|
2018-08-31 14:28:42 +02:00
|
|
|
print('')
|
|
|
|
print('Quick start: just run this script without any arguments')
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
print('Unhandled argument: ' + arg)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
print('Thank you!')
|
|
|
|
if not checkRequirements():
|
|
|
|
sys.exit(1)
|
|
|
|
if not os.path.exists(workpath):
|
|
|
|
os.mkdir(workpath)
|
|
|
|
cppcheckPath = workpath + '/cppcheck'
|
|
|
|
while True:
|
2018-08-29 11:08:56 +02:00
|
|
|
if stopTime:
|
|
|
|
print('stopTime:' + stopTime + '. Time:' + time.strftime('%H:%M') + '.')
|
|
|
|
if stopTime < time.strftime('%H:%M'):
|
|
|
|
print('Stopping. Thank you!')
|
|
|
|
sys.exit(0)
|
2018-08-24 18:49:11 +02:00
|
|
|
if not getCppcheck(cppcheckPath):
|
2018-08-26 16:23:42 +02:00
|
|
|
print('Failed to clone Cppcheck, retry later')
|
|
|
|
sys.exit(1)
|
2018-08-31 14:28:42 +02:00
|
|
|
if compile_version(workpath, jobs, '1.84') == False:
|
2018-08-26 16:23:42 +02:00
|
|
|
print('Failed to compile Cppcheck-1.84, retry later')
|
|
|
|
sys.exit(1)
|
2018-08-31 14:28:42 +02:00
|
|
|
if compile(cppcheckPath, jobs) == False:
|
2018-08-24 13:20:38 +02:00
|
|
|
print('Failed to compile Cppcheck, retry later')
|
|
|
|
sys.exit(1)
|
2018-08-23 21:31:02 +02:00
|
|
|
package = getPackage()
|
2018-08-24 21:07:50 +02:00
|
|
|
tgz = downloadPackage(workpath, package)
|
|
|
|
unpackPackage(workpath, tgz)
|
2018-08-29 22:07:48 +02:00
|
|
|
crash = False
|
2018-08-29 06:51:33 +02:00
|
|
|
count = ''
|
|
|
|
elapsedTime = ''
|
2018-08-26 13:42:01 +02:00
|
|
|
resultsToDiff = []
|
2018-08-24 21:07:50 +02:00
|
|
|
for cppcheck in ['cppcheck/cppcheck', '1.84/cppcheck']:
|
2018-08-31 14:28:42 +02:00
|
|
|
c,errout,t = scanPackage(workpath, cppcheck, jobs)
|
2018-08-29 22:07:48 +02:00
|
|
|
if c < 0:
|
|
|
|
crash = True
|
|
|
|
count += ' Crash!'
|
|
|
|
else:
|
|
|
|
count += ' ' + str(c)
|
2018-08-29 06:51:33 +02:00
|
|
|
elapsedTime += " {:.1f}".format(t)
|
2018-08-26 16:23:42 +02:00
|
|
|
resultsToDiff.append(errout)
|
2018-08-29 22:07:48 +02:00
|
|
|
if not crash and len(resultsToDiff[0]) + len(resultsToDiff[1]) == 0:
|
2018-08-26 16:47:20 +02:00
|
|
|
print('No results')
|
|
|
|
continue
|
2018-08-29 06:51:33 +02:00
|
|
|
output = 'cppcheck: head 1.84\n'
|
|
|
|
output += 'count:' + count + '\n'
|
|
|
|
output += 'elapsed-time:' + elapsedTime + '\n'
|
2018-08-29 22:07:48 +02:00
|
|
|
if not crash:
|
|
|
|
output += 'diff:\n' + diffResults(workpath, 'head', resultsToDiff[0], '1.84', resultsToDiff[1]) + '\n'
|
2018-08-26 16:23:42 +02:00
|
|
|
uploadResults(package, output)
|
2018-08-26 13:42:01 +02:00
|
|
|
print('Results have been uploaded')
|
2018-08-26 16:23:42 +02:00
|
|
|
print('Sleep 5 seconds..')
|
2018-08-26 13:42:01 +02:00
|
|
|
time.sleep(5)
|