python: Apply Black formatter to Python files

The command executed was `black src configs tests util`.

Change-Id: I8dfaa6ab04658fea37618127d6ac19270028d771
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/47024
Maintainer: Bobby Bruce <bbruce@ucdavis.edu>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Bobby R. Bruce
2022-07-05 11:02:25 -07:00
committed by Giacomo Travaglini
parent 1cfaa8da83
commit 787204c92d
980 changed files with 35668 additions and 22233 deletions

View File

@@ -60,7 +60,7 @@ BlockSize = 512
MB = 1024 * 1024
# Setup PATH to look in the sbins.
env['PATH'] += ':/sbin:/usr/sbin'
env["PATH"] += ":/sbin:/usr/sbin"
# Whether to print debug output.
debug = False
@@ -69,7 +69,7 @@ debug = False
def chsFromSize(sizeInBlocks):
if sizeInBlocks >= MaxLBABlocks:
sizeInMBs = (sizeInBlocks * BlockSize) / MB
print('%d MB is too big for LBA, truncating file.' % sizeInMBs)
print("%d MB is too big for LBA, truncating file." % sizeInMBs)
return (MaxLBACylinders, MaxLBAHeads, MaxLBASectors)
sectors = sizeInBlocks
@@ -88,85 +88,90 @@ def chsFromSize(sizeInBlocks):
# Figure out if we should use sudo.
def needSudo():
if not hasattr(needSudo, 'notRoot'):
needSudo.notRoot = (os.geteuid() != 0)
if not hasattr(needSudo, "notRoot"):
needSudo.notRoot = os.geteuid() != 0
if needSudo.notRoot:
print('You are not root. Using sudo.')
print("You are not root. Using sudo.")
return needSudo.notRoot
# Run an external command.
def runCommand(command, inputVal=''):
print("%>", ' '.join(command))
def runCommand(command, inputVal=""):
print("%>", " ".join(command))
proc = Popen(command, stdin=PIPE)
proc.communicate(inputVal.encode())
return proc.returncode
# Run an external command and capture its output. This is intended to be
# used with non-interactive commands where the output is for internal use.
def getOutput(command, inputVal=''):
def getOutput(command, inputVal=""):
global debug
if debug:
print("%>", ' '.join(command))
proc = Popen(command, stderr=STDOUT,
stdin=PIPE, stdout=PIPE)
print("%>", " ".join(command))
proc = Popen(command, stderr=STDOUT, stdin=PIPE, stdout=PIPE)
(out, err) = proc.communicate(inputVal)
return (out.decode(), proc.returncode)
# Run a command as root, using sudo if necessary.
def runPriv(command, inputVal=''):
def runPriv(command, inputVal=""):
realCommand = command
if needSudo():
realCommand = [findProg('sudo')] + command
realCommand = [findProg("sudo")] + command
return runCommand(realCommand, inputVal)
def privOutput(command, inputVal=''):
def privOutput(command, inputVal=""):
realCommand = command
if needSudo():
realCommand = [findProg('sudo')] + command
realCommand = [findProg("sudo")] + command
return getOutput(realCommand, inputVal)
# Find the path to a program.
def findProg(program, cleanupDev=None):
(out, returncode) = getOutput(['which', program])
(out, returncode) = getOutput(["which", program])
if returncode != 0:
if cleanupDev:
cleanupDev.destroy()
exit("Unable to find program %s, check your PATH variable." % program)
return out.strip()
class LoopbackDevice(object):
def __init__(self, devFile=None):
self.devFile = devFile
def __str__(self):
return str(self.devFile)
def setup(self, fileName, offset=False):
assert not self.devFile
(out, returncode) = privOutput([findProg('losetup'), '-f'])
(out, returncode) = privOutput([findProg("losetup"), "-f"])
if returncode != 0:
print(out)
return returncode
self.devFile = out.strip()
command = [findProg('losetup'), self.devFile, fileName]
command = [findProg("losetup"), self.devFile, fileName]
if offset:
off = findPartOffset(self.devFile, fileName, 0)
command = command[:1] + \
["-o", "%d" % off] + \
command[1:]
command = command[:1] + ["-o", "%d" % off] + command[1:]
return runPriv(command)
def destroy(self):
assert self.devFile
returncode = runPriv([findProg('losetup'), '-d', self.devFile])
returncode = runPriv([findProg("losetup"), "-d", self.devFile])
self.devFile = None
return returncode
def findPartOffset(devFile, fileName, partition):
# Attach a loopback device to the file so we can use sfdisk on it.
dev = LoopbackDevice()
dev.setup(fileName)
# Dump the partition information.
command = [findProg('sfdisk'), '-d', dev.devFile]
command = [findProg("sfdisk"), "-d", dev.devFile]
(out, returncode) = privOutput(command)
if returncode != 0:
print(out)
@@ -175,16 +180,16 @@ def findPartOffset(devFile, fileName, partition):
# Parse each line of the sfdisk output looking for the first
# partition description.
SFDISK_PARTITION_INFO_RE = re.compile(
r"^\s*" # Start of line
r"(?P<name>\S+)" # Name
r"\s*:\s*" # Separator
r"start=\s*(?P<start>\d+),\s*" # Partition start record
r"size=\s*(?P<size>\d+),\s*" # Partition size record
r"type=(?P<type>\d+)" # Partition type record
r"\s*$" # End of line
r"^\s*" # Start of line
r"(?P<name>\S+)" # Name
r"\s*:\s*" # Separator
r"start=\s*(?P<start>\d+),\s*" # Partition start record
r"size=\s*(?P<size>\d+),\s*" # Partition size record
r"type=(?P<type>\d+)" # Partition type record
r"\s*$" # End of line
)
lines = out.splitlines()
for line in lines :
for line in lines:
match = SFDISK_PARTITION_INFO_RE.match(line)
if match:
sectors = int(match.group("start"))
@@ -200,8 +205,9 @@ def findPartOffset(devFile, fileName, partition):
dev.destroy()
return sectors * BlockSize
def mountPointToDev(mountPoint):
(mountTable, returncode) = getOutput([findProg('mount')])
(mountTable, returncode) = getOutput([findProg("mount")])
if returncode != 0:
print(mountTable)
exit(returncode)
@@ -220,6 +226,7 @@ def mountPointToDev(mountPoint):
commands = {}
commandOrder = []
class Command(object):
def addArgument(self, *args, **kargs):
self.parser.add_argument(*args, **kargs)
@@ -231,46 +238,64 @@ class Command(object):
self.posArgs = posArgs
commands[self.name] = self
commandOrder.append(self.name)
usage = '%(prog)s [options]'
posUsage = ''
usage = "%(prog)s [options]"
posUsage = ""
for posArg in posArgs:
(argName, argDesc) = posArg
usage += ' %s' % argName
posUsage += '\n %s: %s' % posArg
usage += " %s" % argName
posUsage += "\n %s: %s" % posArg
usage += posUsage
self.parser = ArgumentParser(usage=usage, description=description)
self.addArgument('-d', '--debug', dest='debug', action='store_true',
help='Verbose output.')
self.addArgument('pos', nargs='*')
self.addArgument(
"-d",
"--debug",
dest="debug",
action="store_true",
help="Verbose output.",
)
self.addArgument("pos", nargs="*")
def parseArgs(self, argv):
self.options = self.parser.parse_args(argv[2:])
self.args = self.options.pos
if len(self.args) != len(self.posArgs):
self.parser.error('Incorrect number of arguments')
self.parser.error("Incorrect number of arguments")
global debug
if self.options.debug:
debug = True
def runCom(self):
if not self.func:
exit('Unimplemented command %s!' % self.name)
exit("Unimplemented command %s!" % self.name)
self.func(self.options, self.args)
# A command which prepares an image with an partition table and an empty file
# system.
initCom = Command('init', 'Create an image with an empty file system.',
[('file', 'Name of the image file.'),
('mb', 'Size of the file in MB.')])
initCom.addArgument('-t', '--type', dest='fstype', action='store',
default='ext2',
help='Type of file system to use. Appended to mkfs.')
initCom = Command(
"init",
"Create an image with an empty file system.",
[("file", "Name of the image file."), ("mb", "Size of the file in MB.")],
)
initCom.addArgument(
"-t",
"--type",
dest="fstype",
action="store",
default="ext2",
help="Type of file system to use. Appended to mkfs.",
)
# A command to mount the first partition in the image.
mountCom = Command('mount', 'Mount the first partition in the disk image.',
[('file', 'Name of the image file.'),
('mount point', 'Where to mount the image.')])
mountCom = Command(
"mount",
"Mount the first partition in the disk image.",
[
("file", "Name of the image file."),
("mount point", "Where to mount the image."),
],
)
def mountComFunc(options, args):
(path, mountPoint) = args
@@ -281,15 +306,20 @@ def mountComFunc(options, args):
if dev.setup(path, offset=True) != 0:
exit(1)
if runPriv([findProg('mount'), str(dev), mountPoint]) != 0:
if runPriv([findProg("mount"), str(dev), mountPoint]) != 0:
dev.destroy()
exit(1)
mountCom.func = mountComFunc
# A command to unmount the first partition in the image.
umountCom = Command('umount', 'Unmount the disk image mounted at mount_point.',
[('mount_point', 'What mount point to unmount.')])
umountCom = Command(
"umount",
"Unmount the disk image mounted at mount_point.",
[("mount_point", "What mount point to unmount.")],
)
def umountComFunc(options, args):
(mountPoint,) = args
@@ -302,19 +332,23 @@ def umountComFunc(options, args):
print("Unable to find mount information for %s." % mountPoint)
# Unmount the loopback device.
if runPriv([findProg('umount'), mountPoint]) != 0:
if runPriv([findProg("umount"), mountPoint]) != 0:
exit(1)
# Destroy the loopback device.
dev.destroy()
umountCom.func = umountComFunc
# A command to create an empty file to hold the image.
newCom = Command('new', 'File creation part of "init".',
[('file', 'Name of the image file.'),
('mb', 'Size of the file in MB.')])
newCom = Command(
"new",
'File creation part of "init".',
[("file", "Name of the image file."), ("mb", "Size of the file in MB.")],
)
def newImage(file, mb):
(cylinders, heads, sectors) = chsFromSize((mb * MB) / BlockSize)
@@ -325,7 +359,8 @@ def newImage(file, mb):
# store to disk and which is defined to read as zero.
fd = os.open(file, os.O_WRONLY | os.O_CREAT)
os.lseek(fd, size - 1, os.SEEK_SET)
os.write(fd, b'\0')
os.write(fd, b"\0")
def newComFunc(options, args):
(file, mb) = args
@@ -336,16 +371,23 @@ def newComFunc(options, args):
newCom.func = newComFunc
# A command to partition the image file like a raw disk device.
partitionCom = Command('partition', 'Partition part of "init".',
[('file', 'Name of the image file.')])
partitionCom = Command(
"partition",
'Partition part of "init".',
[("file", "Name of the image file.")],
)
def partition(dev, cylinders, heads, sectors):
# Use sfdisk to partition the device
# The specified options are intended to work with both new and old
# versions of sfdisk (see https://askubuntu.com/a/819614)
comStr = ';'
return runPriv([findProg('sfdisk'), '--no-reread', '-u', 'S', '-L', \
str(dev)], inputVal=comStr)
comStr = ";"
return runPriv(
[findProg("sfdisk"), "--no-reread", "-u", "S", "-L", str(dev)],
inputVal=comStr,
)
def partitionComFunc(options, args):
(path,) = args
@@ -362,17 +404,28 @@ def partitionComFunc(options, args):
dev.destroy()
partitionCom.func = partitionComFunc
# A command to format the first partition in the image.
formatCom = Command('format', 'Formatting part of "init".',
[('file', 'Name of the image file.')])
formatCom.addArgument('-t', '--type', dest='fstype', action='store',
default='ext2',
help='Type of file system to use. Appended to mkfs.')
formatCom = Command(
"format",
'Formatting part of "init".',
[("file", "Name of the image file.")],
)
formatCom.addArgument(
"-t",
"--type",
dest="fstype",
action="store",
default="ext2",
help="Type of file system to use. Appended to mkfs.",
)
def formatImage(dev, fsType):
return runPriv([findProg('mkfs.%s' % fsType, dev), str(dev)])
return runPriv([findProg("mkfs.%s" % fsType, dev), str(dev)])
def formatComFunc(options, args):
(path,) = args
@@ -388,8 +441,10 @@ def formatComFunc(options, args):
dev.destroy()
formatCom.func = formatComFunc
def initComFunc(options, args):
(path, mb) = args
mb = int(mb)
@@ -409,19 +464,20 @@ def initComFunc(options, args):
exit(1)
dev.destroy()
initCom.func = initComFunc
# Figure out what command was requested and execute it.
if len(argv) < 2 or argv[1] not in commands:
print('Usage: %s [command] <command arguments>')
print('where [command] is one of ')
print("Usage: %s [command] <command arguments>")
print("where [command] is one of ")
for name in commandOrder:
command = commands[name]
print(' %s: %s' % (command.name, command.description))
print('Watch for orphaned loopback devices and delete them with')
print('losetup -d. Mounted images will belong to root, so you may need')
print('to use sudo to modify their contents.')
print(" %s: %s" % (command.name, command.description))
print("Watch for orphaned loopback devices and delete them with")
print("losetup -d. Mounted images will belong to root, so you may need")
print("to use sudo to modify their contents.")
exit(1)
command = commands[argv[1]]