From 96d8d005658077ee7a64e5728f0abddec93a8569 Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Thu, 9 Nov 2017 14:32:45 +0100
Subject: [PATCH 1/6] Fix code style - spaces instead of tabs, indentaion,
 imports, blank lines etc.

Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
---
 pchrt.py    | 170 +++++++++++++++++++++-------------------
 ptaskset.py | 253 +++++++++++++++++++++++++++++++-----------------------------
 2 files changed, 223 insertions(+), 200 deletions(-)

diff --git a/pchrt.py b/pchrt.py
index 0761792..c590b37 100755
--- a/pchrt.py
+++ b/pchrt.py
@@ -14,10 +14,13 @@
 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 #   General Public License for more details.
 
-import os, schedutils, sys
+import os
+import schedutils
+import sys
+
 
 def usage():
-	print '''pchrt (python-schedutils)
+    print '''pchrt (python-schedutils)
 usage: pchrt [options] [prio] [pid | cmd [args...]]
 manipulate real-time attributes of a process
   -b, --batch                        set policy to SCHED_BATCH
@@ -33,97 +36,106 @@ manipulate real-time attributes of a process
 You must give a priority if changing policy.
 
 Report bugs and send patches to <tuna-devel@lists.fedorahosted.org>'''
-	return
+    return
+
 
 def show_priority_limits(policy):
-	print "%-32.32s: %d/%d" % ("%s min/max priority" % schedutils.schedstr(policy),
-				   schedutils.get_priority_min(policy),
-				   schedutils.get_priority_max(policy))
+    print "%-32.32s: %d/%d" % (
+        "%s min/max priority" % schedutils.schedstr(policy),
+        schedutils.get_priority_min(policy),
+        schedutils.get_priority_max(policy)
+    )
+
 
 def show_all_priority_limits():
-	for policy in (schedutils.SCHED_OTHER, schedutils.SCHED_FIFO,
-		       schedutils.SCHED_RR, schedutils.SCHED_BATCH):
-		show_priority_limits(policy)
+    for policy in (schedutils.SCHED_OTHER, schedutils.SCHED_FIFO,
+                   schedutils.SCHED_RR, schedutils.SCHED_BATCH):
+        show_priority_limits(policy)
+
 
 def show_settings(pid):
-	policy = schedutils.get_scheduler(pid)
-	spolicy = schedutils.schedstr(policy)
-	rtprio = schedutils.get_priority(pid)
-	reset_on_fork = ""
-	if policy & schedutils.SCHED_RESET_ON_FORK:
-		reset_on_fork = "|SCHED_RESET_ON_FORK"
-	print '''pid %d's current scheduling policy: %s%s
+    policy = schedutils.get_scheduler(pid)
+    spolicy = schedutils.schedstr(policy)
+    rtprio = schedutils.get_priority(pid)
+    reset_on_fork = ""
+    if policy & schedutils.SCHED_RESET_ON_FORK:
+        reset_on_fork = "|SCHED_RESET_ON_FORK"
+    print '''pid %d's current scheduling policy: %s%s
 pid %d's current scheduling priority: %d''' % (pid, spolicy, reset_on_fork, pid, rtprio)
 
+
 def valid_policy_flag(policy, policy_flag):
-	if policy_flag == schedutils.SCHED_RESET_ON_FORK and \
-	   policy not in (schedutils.SCHED_RR, schedutils.SCHED_FIFO):
-		print "SCHED_RESET_ON_FORK flag is supported for SCHED_FIFO and SCHED_RR policies only"
-		return False
-	return True
+    if policy_flag == schedutils.SCHED_RESET_ON_FORK and \
+       policy not in (schedutils.SCHED_RR, schedutils.SCHED_FIFO):
+        print "SCHED_RESET_ON_FORK flag is supported for SCHED_FIFO and SCHED_RR policies only"
+        return False
+    return True
+
 
 def change_settings(pid, policy, policy_flag, rtprio):
-	try:
-		schedutils.set_scheduler(pid, policy | policy_flag, rtprio)
-	except SystemError, err:
-		print "sched_setscheduler: %s" % err[1]
-		print "failed to set pid %d's policy" % pid
+    try:
+        schedutils.set_scheduler(pid, policy | policy_flag, rtprio)
+    except SystemError, err:
+        print "sched_setscheduler: %s" % err[1]
+        print "failed to set pid %d's policy" % pid
+
 
 def main():
 
-	args = sys.argv[1:]
-	if not args:
-		usage()
-		return
-
-	policy = schedutils.SCHED_RR
-	policy_flag = 0
-	while True:
-		o = args.pop(0)
-		try:
-			priority = int(o)
-			break
-		except:
-			pass
-
-		if o in ("-h", "--help"):
-			usage()
-			return
-		elif o in ("-b", "--batch"):
-			policy = schedutils.SCHED_BATCH
-		elif o in ("-f", "--fifo"):
-			policy = schedutils.SCHED_FIFO
-		elif o in ("-i", "--idle"):
-			policy = schedutils.SCHED_IDLE
-		elif o in ("-m", "--max"):
-			show_all_priority_limits()
-			return
-		elif o in ("-o", "--other"):
-			policy = schedutils.SCHED_OTHER
-		elif o in ("-r", "--rr"):
-			policy = schedutils.SCHED_RR
-		elif o in ("-R", "--reset-on-fork"):
-			policy_flag |= schedutils.SCHED_RESET_ON_FORK
-		elif o in ("-p", "--pid"):
-			if len(args) > 1:
-				priority = int(args.pop(0))
-				pid = int(args.pop(0))
-				if not valid_policy_flag(policy, policy_flag):
-					return
-				change_settings(pid, policy, policy_flag, priority)
-			else:
-				pid = int(args.pop(0))
-				show_settings(pid)
-			return
-		else:
-			usage()
-			return
-
-	if not valid_policy_flag(policy, policy_flag):
-		return
-
-	schedutils.set_scheduler(0, policy | policy_flag, priority)
-	os.execvp(args[0], args)
+    args = sys.argv[1:]
+    if not args:
+        usage()
+        return
+
+    policy = schedutils.SCHED_RR
+    policy_flag = 0
+    while True:
+        o = args.pop(0)
+        try:
+            priority = int(o)
+            break
+        except:
+            pass
+
+        if o in ("-h", "--help"):
+            usage()
+            return
+        elif o in ("-b", "--batch"):
+            policy = schedutils.SCHED_BATCH
+        elif o in ("-f", "--fifo"):
+            policy = schedutils.SCHED_FIFO
+        elif o in ("-i", "--idle"):
+            policy = schedutils.SCHED_IDLE
+        elif o in ("-m", "--max"):
+            show_all_priority_limits()
+            return
+        elif o in ("-o", "--other"):
+            policy = schedutils.SCHED_OTHER
+        elif o in ("-r", "--rr"):
+            policy = schedutils.SCHED_RR
+        elif o in ("-R", "--reset-on-fork"):
+            policy_flag |= schedutils.SCHED_RESET_ON_FORK
+        elif o in ("-p", "--pid"):
+            if len(args) > 1:
+                priority = int(args.pop(0))
+                pid = int(args.pop(0))
+                if not valid_policy_flag(policy, policy_flag):
+                    return
+                change_settings(pid, policy, policy_flag, priority)
+            else:
+                pid = int(args.pop(0))
+                show_settings(pid)
+            return
+        else:
+            usage()
+            return
+
+    if not valid_policy_flag(policy, policy_flag):
+        return
+
+    schedutils.set_scheduler(0, policy | policy_flag, priority)
+    os.execvp(args[0], args)
+
 
 if __name__ == '__main__':
     main()
diff --git a/ptaskset.py b/ptaskset.py
index afd1847..de925a9 100755
--- a/ptaskset.py
+++ b/ptaskset.py
@@ -14,10 +14,13 @@
 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 #   General Public License for more details.
 
-import os, schedutils, sys
+import os
+import schedutils
+import sys
+
 
 def usage():
-	print '''ptaskset (python-schedutils)
+    print '''ptaskset (python-schedutils)
 usage: ptaskset [options] [mask | cpu-list] [pid | cmd [args...]]
 set or get the affinity of a process
 
@@ -25,138 +28,146 @@ set or get the affinity of a process
   -c, --cpu-list             display and specify cpus in list format
   -h, --help                 display this help'''
 
-	return
+    return
+
 
 def hexbitmask(l):
-	hexbitmask = []
-	bit = 0
-	mask = 0
-	nr_entries = 1 << max(l)
-	for entry in range(nr_entries):
-		if entry in l:
-			mask |= (1 << bit)
-		bit += 1
-		if bit == 32:
-			bit = 0
-			hexbitmask.insert(0, mask)
-			mask = 0
-
-	if bit < 32 and mask != 0:
-		hexbitmask.insert(0, mask)
-
-	return hexbitmask
-
-def find_last_bit(n, wordsize = 32):
-	bit = wordsize - 1
-	while bit != 0:
-		if n & (1 << bit):
-			return bit
-		bit -= 1
-	return 0
+    hexbitmask = []
+    bit = 0
+    mask = 0
+    nr_entries = 1 << max(l)
+    for entry in range(nr_entries):
+        if entry in l:
+            mask |= (1 << bit)
+        bit += 1
+        if bit == 32:
+            bit = 0
+            hexbitmask.insert(0, mask)
+            mask = 0
+
+    if bit < 32 and mask != 0:
+        hexbitmask.insert(0, mask)
+
+    return hexbitmask
+
+
+def find_last_bit(n, wordsize=32):
+    bit = wordsize - 1
+    while bit != 0:
+        if n & (1 << bit):
+            return bit
+        bit -= 1
+    return 0
+
 
 def bitmasklist(line):
-	fields = line.strip().split(",")
-	bitmasklist = []
-	while int(fields[0], 16) == 0:
-		fields.pop(0)
-
-	if not fields:
-		return []
-
-	nr_entries = (len(fields) - 1) * 32 + find_last_bit(int(fields[0], 16)) + 1
-
-	entry = 0
-	for i in range(len(fields) - 1, -1, -1):
-		mask = int(fields[i], 16)
-		while mask != 0:
-			if mask & 1:
-				bitmasklist.append(entry)
-			mask >>= 1
-			entry += 1
-			if entry == nr_entries:
-				break
-		if entry == nr_entries:
-			break
-	return bitmasklist
+    fields = line.strip().split(",")
+    bitmasklist = []
+    while int(fields[0], 16) == 0:
+        fields.pop(0)
+
+    if not fields:
+        return []
+
+    nr_entries = (len(fields) - 1) * 32 + find_last_bit(int(fields[0], 16)) + 1
+
+    entry = 0
+    for i in range(len(fields) - 1, -1, -1):
+        mask = int(fields[i], 16)
+        while mask != 0:
+            if mask & 1:
+                bitmasklist.append(entry)
+            mask >>= 1
+            entry += 1
+            if entry == nr_entries:
+                break
+        if entry == nr_entries:
+            break
+    return bitmasklist
+
 
 def cpustring_to_list(cpustr):
-	"""Convert a string of numbers to an integer list.
-    
-	Given a string of comma-separated numbers and number ranges,
-	return a simple sorted list of the integers it represents.
-
-	This function will throw exceptions for badly-formatted strings.
-    
-	Returns a list of integers."""
-
-	fields = cpustr.strip().split(",")
-	cpu_list = []
-	for field in fields:
-		ends = field.split("-")
-		if len(ends) > 2:
-			raise "Syntax error"
-		if len(ends) == 2:
-			cpu_list += range(int(ends[0]), int(ends[1])+1)
-		else:
-			cpu_list += [int(ends[0])]
-	return list(set(cpu_list))
+    """Convert a string of numbers to an integer list.
+
+    Given a string of comma-separated numbers and number ranges,
+    return a simple sorted list of the integers it represents.
+
+    This function will throw exceptions for badly-formatted strings.
+
+    Returns a list of integers."""
+
+    fields = cpustr.strip().split(",")
+    cpu_list = []
+    for field in fields:
+        ends = field.split("-")
+        if len(ends) > 2:
+            raise "Syntax error"
+        if len(ends) == 2:
+            cpu_list += range(int(ends[0]), int(ends[1]) + 1)
+        else:
+            cpu_list += [int(ends[0])]
+    return list(set(cpu_list))
+
 
 def show_settings(pid, when, cpu_list_mode):
-	affinity = schedutils.get_affinity(pid)
-	if cpu_list_mode:
-		mask = ",".join([str(a) for a in affinity])
-	else:
-		mask = ",".join(["%x" % a for a in hexbitmask(affinity)])
-	print "pid %d's %s affinity mask: %s" % (pid, when, mask)
+    affinity = schedutils.get_affinity(pid)
+    if cpu_list_mode:
+        mask = ",".join([str(a) for a in affinity])
+    else:
+        mask = ",".join(["%x" % a for a in hexbitmask(affinity)])
+    print "pid %d's %s affinity mask: %s" % (pid, when, mask)
+
 
 def change_settings(pid, affinity, cpu_list_mode):
-	if cpu_list_mode:
-		try:
-			affinity = [ int(a) for a in affinity.split(",") ]
-		except:
-			affinity = cpustring_to_list(affinity)
-	else:
-		affinity = bitmasklist(affinity)
-
-	try:
-		schedutils.set_affinity(pid, affinity)
-	except SystemError, err:
-		print "sched_setaffinity: %s" % err[1]
-		print "failed to set pid %d's affinity" % pid
+    if cpu_list_mode:
+        try:
+            affinity = [int(a) for a in affinity.split(",")]
+        except:
+            affinity = cpustring_to_list(affinity)
+    else:
+        affinity = bitmasklist(affinity)
+
+    try:
+        schedutils.set_affinity(pid, affinity)
+    except SystemError, err:
+        print "sched_setaffinity: %s" % err[1]
+        print "failed to set pid %d's affinity" % pid
+
 
 def main():
 
-	args = sys.argv[1:]
-	if not args:
-		usage()
-		return
-
-	cpu_list_mode = False
-	while True:
-		o = args.pop(0)
-
-		if o in ("-h", "--help"):
-			usage()
-			return
-		elif o in ("-c", "--cpu-list"):
-			cpu_list_mode = True
-		elif o in ("-p", "--pid"):
-			if len(args) > 1:
-				affinity = args.pop(0)
-				pid = int(args.pop(0))
-				show_settings(pid, "current", cpu_list_mode)
-				change_settings(pid, affinity, cpu_list_mode)
-				show_settings(pid, "new", cpu_list_mode)
-			else:
-				pid = int(args.pop(0))
-				show_settings(pid, "current", cpu_list_mode)
-			return
-		else:
-			break
-
-	affinity = o
-	change_settings(0, affinity, cpu_list_mode)
-	os.execvp(args[0], args)
+    args = sys.argv[1:]
+    if not args:
+        usage()
+        return
+
+    cpu_list_mode = False
+    while True:
+        o = args.pop(0)
+
+        if o in ("-h", "--help"):
+            usage()
+            return
+        elif o in ("-c", "--cpu-list"):
+            cpu_list_mode = True
+        elif o in ("-p", "--pid"):
+            if len(args) > 1:
+                affinity = args.pop(0)
+                pid = int(args.pop(0))
+                show_settings(pid, "current", cpu_list_mode)
+                change_settings(pid, affinity, cpu_list_mode)
+                show_settings(pid, "new", cpu_list_mode)
+            else:
+                pid = int(args.pop(0))
+                show_settings(pid, "current", cpu_list_mode)
+            return
+        else:
+            break
+
+    affinity = o
+    change_settings(0, affinity, cpu_list_mode)
+    os.execvp(args[0], args)
+
 
 if __name__ == '__main__':
     main()
-- 
2.13.6

