Skip to content

Commit 1877476

Browse files
authored
donate-cpu.py: Add new argument --max-packages=N. (#1718)
The client script will exit after the specified number of packages have been processed. 0 means infinitely. Useful for example to regularly quit the script, check for updates to the client and start it again. Or as an alternative to the `--stop-time` argument.
1 parent 9a5fcdd commit 1877476

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

tools/donate-cpu.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# --bandwidth-limit=limit Limit download rate for packages. Format for limit is the same that wget uses.
1212
# Examples: --bandwidth-limit=250k => max. 250 kilobytes per second
1313
# --bandwidth-limit=2m => max. 2 megabytes per second
14+
# --max-packages=N Process N packages and then exit. A value of 0 means infinitely.
1415
#
1516
# What this script does:
1617
# 1. Check requirements
@@ -37,7 +38,7 @@
3738
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
3839
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
3940
# changes)
40-
CLIENT_VERSION = "1.1.10"
41+
CLIENT_VERSION = "1.1.11"
4142

4243

4344
def checkRequirements():
@@ -380,6 +381,7 @@ def uploadInfo(package, info_output, server_address):
380381
packageUrl = None
381382
server_address = ('cppcheck.osuosl.org', 8000)
382383
bandwidth_limit = None
384+
max_packages = None
383385
for arg in sys.argv[1:]:
384386
# --stop-time=12:00 => run until ~12:00 and then stop
385387
if arg.startswith('--stop-time='):
@@ -401,6 +403,20 @@ def uploadInfo(package, info_output, server_address):
401403
server_address = ('localhost', 8001)
402404
elif arg.startswith('--bandwidth-limit='):
403405
bandwidth_limit = arg[arg.find('=')+1:]
406+
elif arg.startswith('--max-packages='):
407+
arg_value = arg[arg.find('=')+1:]
408+
try:
409+
max_packages = int(arg_value)
410+
except ValueError:
411+
max_packages = None
412+
if max_packages < 0:
413+
max_packages = None
414+
if max_packages is None:
415+
print('Error: Max. packages value "{}" is invalid. Must be a positive number or 0.'.format(arg_value))
416+
sys.exit(1)
417+
# 0 means infinitely, no counting needed.
418+
if max_packages == 0:
419+
max_packages = None
404420
elif arg == '--help':
405421
print('Donate CPU to Cppcheck project')
406422
print('')
@@ -413,6 +429,7 @@ def uploadInfo(package, info_output, server_address):
413429
print(' --bandwidth-limit=limit Limit download rate for packages. Format for limit is the same that wget uses.')
414430
print(' Examples: --bandwidth-limit=250k => max. 250 kilobytes per second')
415431
print(' --bandwidth-limit=2m => max. 2 megabytes per second')
432+
print(' --max-packages=N Process N packages and then exit. A value of 0 means infinitely.')
416433
print('')
417434
print('Quick start: just run this script without any arguments')
418435
sys.exit(0)
@@ -429,10 +446,20 @@ def uploadInfo(package, info_output, server_address):
429446
sys.exit(1)
430447
else:
431448
print('Bandwidth-limit: ' + bandwidth_limit)
449+
if max_packages:
450+
print('Maximum number of packages to download and analyze: {}'.format(max_packages))
432451
if not os.path.exists(workpath):
433452
os.mkdir(workpath)
434453
cppcheckPath = workpath + '/cppcheck'
454+
packages_processed = 0
435455
while True:
456+
if max_packages:
457+
if packages_processed >= max_packages:
458+
print('Processed the specified number of {} package(s). Exiting now.'.format(max_packages))
459+
break
460+
else:
461+
print('Processing package {} of the specified {} package(s).'.format(packages_processed + 1, max_packages))
462+
packages_processed += 1
436463
if stopTime:
437464
print('stopTime:' + stopTime + '. Time:' + time.strftime('%H:%M') + '.')
438465
if stopTime < time.strftime('%H:%M'):
@@ -525,5 +552,6 @@ def uploadInfo(package, info_output, server_address):
525552
uploadResults(package, output, server_address)
526553
if info_exists:
527554
uploadInfo(package, info_output, server_address)
528-
print('Sleep 5 seconds..')
529-
time.sleep(5)
555+
if not max_packages or packages_processed < max_packages:
556+
print('Sleep 5 seconds..')
557+
time.sleep(5)

tools/test/run_donate_cpu_client_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ do
3333
${python_exec} ${client_script} --package=${test_package} -j2
3434
${python_exec} ${client_script} --package=${test_package} --bandwidth-limit=250k
3535
${python_exec} ${client_script} --package=${test_package} -j2 --bandwidth-limit=0.5M
36+
${python_exec} ${client_script} --package=${test_package} --max-packages=1
3637
done
3738
done
3839

0 commit comments

Comments
 (0)