donate_cpu: Use with ... as (#2520)

This commit is contained in:
Rikard Falkeborn 2020-02-03 10:27:32 +01:00 committed by GitHub
parent 488bc9997c
commit b13ae83845
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 42 deletions

View File

@ -14,7 +14,7 @@ import shlex
# 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.2.1" CLIENT_VERSION = "1.2.2"
# Timeout for analysis with Cppcheck in seconds # Timeout for analysis with Cppcheck in seconds
CPPCHECK_TIMEOUT = 60 * 60 CPPCHECK_TIMEOUT = 60 * 60
@ -108,29 +108,27 @@ def compile_cppcheck(cppcheck_path, jobs):
def get_cppcheck_versions(server_address): def get_cppcheck_versions(server_address):
print('Connecting to server to get Cppcheck versions..') print('Connecting to server to get Cppcheck versions..')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
sock.connect(server_address) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.send(b'GetCppcheckVersions\n') sock.connect(server_address)
versions = sock.recv(256) sock.send(b'GetCppcheckVersions\n')
versions = sock.recv(256)
except socket.error as err: except socket.error as err:
print('Failed to get cppcheck versions: ' + str(err)) print('Failed to get cppcheck versions: ' + str(err))
return None return None
sock.close()
return versions.decode('utf-8').split() return versions.decode('utf-8').split()
def get_packages_count(server_address): def get_packages_count(server_address):
print('Connecting to server to get count of packages..') print('Connecting to server to get count of packages..')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
sock.connect(server_address) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.send(b'getPackagesCount\n') sock.connect(server_address)
packages = int(sock.recv(64)) sock.send(b'getPackagesCount\n')
packages = int(sock.recv(64))
except socket.error as err: except socket.error as err:
print('Failed to get count of packages: ' + str(err)) print('Failed to get count of packages: ' + str(err))
return None return None
sock.close()
return packages return packages
@ -213,22 +211,21 @@ def unpack_package(work_path, tgz):
os.chdir(temp_path) os.chdir(temp_path)
found = False found = False
if tarfile.is_tarfile(tgz): if tarfile.is_tarfile(tgz):
tf = tarfile.open(tgz) with tarfile.open(tgz) as tf:
for member in tf: for member in tf:
if member.name.startswith(('/', '..')): if member.name.startswith(('/', '..')):
# Skip dangerous file names # Skip dangerous file names
continue continue
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', '.qml', '.h++', '.hxx', '.hh', '.tpp', '.txx', '.qml',
'.sln', '.vcproj', '.vcxproj')): '.sln', '.vcproj', '.vcxproj')):
try: try:
tf.extract(member.name) tf.extract(member.name)
found = True found = True
except OSError: except OSError:
pass pass
except AttributeError: except AttributeError:
pass pass
tf.close()
os.chdir(work_path) os.chdir(work_path)
return found return found
@ -240,12 +237,8 @@ def has_include(path, includes):
for name in files: for name in files:
filename = os.path.join(root, name) filename = os.path.join(root, name)
try: try:
if sys.version_info.major < 3: with open(filename, 'rt', errors='ignore') as f:
f = open(filename, 'rt') filedata = f.read()
else:
f = open(filename, 'rt', errors='ignore')
filedata = f.read()
f.close()
if re.search(re_expr, filedata, re.MULTILINE): if re.search(re_expr, filedata, re.MULTILINE):
return True return True
except IOError: except IOError:
@ -418,11 +411,10 @@ def upload_results(package, results, server_address):
max_retries = 4 max_retries = 4
for retry in range(max_retries): for retry in range(max_retries):
try: try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(server_address) sock.connect(server_address)
cmd = 'write\n' cmd = 'write\n'
send_all(sock, cmd + package + '\n' + results + '\nDONE') send_all(sock, cmd + package + '\n' + results + '\nDONE')
sock.close()
print('Results have been successfully uploaded.') print('Results have been successfully uploaded.')
return True return True
except socket.error as err: except socket.error as err:
@ -439,10 +431,9 @@ def upload_info(package, info_output, server_address):
max_retries = 3 max_retries = 3
for retry in range(max_retries): for retry in range(max_retries):
try: try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(server_address) sock.connect(server_address)
send_all(sock, 'write_info\n' + package + '\n' + info_output + '\nDONE') send_all(sock, 'write_info\n' + package + '\n' + info_output + '\nDONE')
sock.close()
print('Information output has been successfully uploaded.') print('Information output has been successfully uploaded.')
return True return True
except socket.error as err: except socket.error as err: