Improvements to the python style in get_cyclictest_snapshot - Add comments to the methods, functions, class and moduleb - Put imports on individual lines - Use "is" and "is not" with None insteald of == or != - Don't compare the length of lists to zero, to determine if they are empty Signed-off-by: John Kacur <jkacur@xxxxxxxxxx> --- src/cyclictest/get_cyclictest_snapshot.py | 38 +++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/cyclictest/get_cyclictest_snapshot.py b/src/cyclictest/get_cyclictest_snapshot.py index 7dddfc5d65b0..27fc629ca53e 100755 --- a/src/cyclictest/get_cyclictest_snapshot.py +++ b/src/cyclictest/get_cyclictest_snapshot.py @@ -1,9 +1,14 @@ #!/usr/bin/env python3 +""" Program to get a snapshot of a running instance of cyclictest """ # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2020 John Kacur <jkacur@xxxxxxxxxx> -import subprocess, signal, argparse, re, glob, sys +import subprocess +import argparse +import re +import glob +import sys parser = argparse.ArgumentParser(description='Get a snapshot of running instances of cyclictest') parser.add_argument('-l', '--list', action='store_true', help='list the main pid(s) of running instances of cyclictest') @@ -11,8 +16,8 @@ parser.add_argument('-s', '--snapshot', nargs='*', metavar='pid', help='take a s parser.add_argument('-p', '--print', nargs='*', metavar='pid', help='print the snapshots') args = parser.parse_args() - class Snapshot: + """ Class for getting a snapshot of a running cyclictest instance """ def __init__(self): self.pids = [] @@ -20,6 +25,7 @@ class Snapshot: self.refresh() def refresh(self): + """ Create a list of running cyclictest instances. """ self.pids = [] self.shm_files = glob.glob('/dev/shm/cyclictest*') self.shm_files.sort() @@ -27,21 +33,21 @@ class Snapshot: pid = re.search('[0-9]*$', shm_file).group() self.pids += [pid] - # Send USR2 to all running instances of cyclictest or just to - # a specific pid (spid) if specified def take_snapshot(self, spids=None): + """ Send USR2 to all running instances of cyclictest, + or just to a specific pid (spids) if specified. """ for pid in self.pids: - if (spids == None) or (pid in spids): - # print("kill -s USR2 ", pid) + if (spids is None) or (pid in spids): subprocess.run(["kill", "-s", "USR2", pid]) def print_pids(self): + """ Print the list of pids of running cyclictest instances. """ for pid in self.pids: print(pid) - # Print the data in /dev/shm/cyclictest* def print(self, spids=None): - if spids == None: + """ Print the data in /dev/shm/cyclictest* """ + if spids is None: for shm_file in self.shm_files: with open(shm_file, 'r') as f: data = f.read() @@ -59,17 +65,17 @@ snapshot = Snapshot() if args.list: snapshot.print_pids() -if args.snapshot != None: - if len(args.snapshot) == 0: - snapshot.take_snapshot() - else: +if args.snapshot is not None: + if args.snapshot: snapshot.take_snapshot(args.snapshot) - -if args.print != None: - if len(args.print) == 0: - snapshot.print() else: + snapshot.take_snapshot() + +if args.print is not None: + if args.print: snapshot.print(args.print) + else: + snapshot.print() if len(sys.argv) == 1: snapshot.take_snapshot() -- 2.21.3