Skip to content

Commit

Permalink
misc: Run pre-commit run on all files in repo
Browse files Browse the repository at this point in the history
The following command was run:

```
pre-commit run --all-files
```

This ensures all the files in the repository are formatted to pass our
checks.

Change-Id: Ia2fe3529a50ad925d1076a612d60a4280adc40de
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/62572
Tested-by: kokoro <[email protected]>
Reviewed-by: Andreas Sandberg <[email protected]>
Maintainer: Jason Lowe-Power <[email protected]>
Reviewed-by: Jason Lowe-Power <[email protected]>
  • Loading branch information
BobbyRBruce committed Aug 24, 2022
1 parent 64add0e commit 2bc5a8b
Show file tree
Hide file tree
Showing 181 changed files with 1,453 additions and 1,237 deletions.
1 change: 0 additions & 1 deletion .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,3 @@ Marjan Fariborz <[email protected]> marjanfariborz <[email protected]>
Mike Upton <[email protected]>
seanzw <[email protected]>
Trivikram Reddy <[email protected]> tv-reddy <[email protected]>

3 changes: 1 addition & 2 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,9 @@ maintainer (see MAINTAINERS).*
## Running Tests in Parallel

Whimsy has support for parallel testing baked in. This system supports
running multiple suites at the same time on the same computer. To run
running multiple suites at the same time on the same computer. To run
suites in parallel, supply the `-t <number-tests>` flag to the run command.

For example, to run up to three test suites at the same time::

./main.py run --skip-build -t 3

13 changes: 7 additions & 6 deletions build_tools/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
import array
import functools


def bytesToCppArray(code, symbol, data):
'''
"""
Output an array of bytes to a code formatter as a c++ array declaration.
'''
code('const std::uint8_t ${symbol}[] = {')
"""
code("const std::uint8_t ${symbol}[] = {")
code.indent()
step = 16
for i in range(0, len(data), step):
x = array.array('B', data[i:i+step])
strs = map(lambda i: f'{i},', x)
x = array.array("B", data[i : i + step])
strs = map(lambda i: f"{i},", x)
code(functools.reduce(lambda x, y: x + y, strs))
code.dedent()
code('};')
code("};")
183 changes: 106 additions & 77 deletions build_tools/code_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import os
import re


class lookup(object):
def __init__(self, formatter, frame, *args, **kwargs):
self.frame = frame
Expand All @@ -64,10 +65,10 @@ def __getitem__(self, item):
if item in self.kwargs:
return self.kwargs[item]

if item == '__file__':
if item == "__file__":
return self.frame.f_code.co_filename

if item == '__line__':
if item == "__line__":
return self.frame.f_lineno

if self.formatter.locals and item in self.frame.f_locals:
Expand All @@ -89,6 +90,7 @@ def __getitem__(self, item):
pass
raise IndexError("Could not find '%s'" % item)


class code_formatter_meta(type):
pattern = r"""
(?:
Expand All @@ -102,44 +104,48 @@ class code_formatter_meta(type):
%(delim)s(?P<invalid>) # ill-formed delimiter exprs
)
"""

def __init__(cls, name, bases, dct):
super(code_formatter_meta, cls).__init__(name, bases, dct)
if 'pattern' in dct:
if "pattern" in dct:
pat = cls.pattern
else:
# tuple expansion to ensure strings are proper length
lb,rb = cls.braced
lb1,lb2,rb2,rb1 = cls.double_braced
lb, rb = cls.braced
lb1, lb2, rb2, rb1 = cls.double_braced
pat = code_formatter_meta.pattern % {
'delim' : re.escape(cls.delim),
'ident' : cls.ident,
'pos' : cls.pos,
'lb' : re.escape(lb),
'rb' : re.escape(rb),
'ldb' : re.escape(lb1+lb2),
'rdb' : re.escape(rb2+rb1),
}
"delim": re.escape(cls.delim),
"ident": cls.ident,
"pos": cls.pos,
"lb": re.escape(lb),
"rb": re.escape(rb),
"ldb": re.escape(lb1 + lb2),
"rdb": re.escape(rb2 + rb1),
}
cls.pattern = re.compile(pat, re.VERBOSE | re.DOTALL | re.MULTILINE)


class code_formatter(object, metaclass=code_formatter_meta):
delim = r'$'
ident = r'[_A-z]\w*'
pos = r'[0-9]+'
braced = r'{}'
double_braced = r'{{}}'
delim = r"$"
ident = r"[_A-z]\w*"
pos = r"[0-9]+"
braced = r"{}"
double_braced = r"{{}}"

globals = True
locals = True
fix_newlines = True

def __init__(self, *args, **kwargs):
self._data = []
self._dict = {}
self._indent_level = 0
self._indent_spaces = 4
self.globals = kwargs.pop('globals', type(self).globals)
self.locals = kwargs.pop('locals', type(self).locals)
self._fix_newlines = \
kwargs.pop('fix_newlines', type(self).fix_newlines)
self.globals = kwargs.pop("globals", type(self).globals)
self.locals = kwargs.pop("locals", type(self).locals)
self._fix_newlines = kwargs.pop(
"fix_newlines", type(self).fix_newlines
)

if args:
self.__call__(args)
Expand Down Expand Up @@ -171,38 +177,44 @@ def write(self, *args):
# Add a comment to inform which file generated the generated file
# to make it easier to backtrack and modify generated code
frame = inspect.currentframe().f_back
if re.match(r'^\.(cc|hh|c|h)$', extension) is not None:
f.write(f'''/**
if re.match(r"^\.(cc|hh|c|h)$", extension) is not None:
f.write(
f"""/**
* DO NOT EDIT THIS FILE!
* File automatically generated by
* {frame.f_code.co_filename}:{frame.f_lineno}
*/
''')
elif re.match(r'^\.py$', extension) is not None:
f.write(f'''#
"""
)
elif re.match(r"^\.py$", extension) is not None:
f.write(
f"""#
# DO NOT EDIT THIS FILE!
# File automatically generated by
# {frame.f_code.co_filename}:{frame.f_lineno}
#
''')
elif re.match(r'^\.html$', extension) is not None:
f.write(f'''<!--
"""
)
elif re.match(r"^\.html$", extension) is not None:
f.write(
f"""<!--
DO NOT EDIT THIS FILE!
File automatically generated by
{frame.f_code.co_filename}:{frame.f_lineno}
-->
''')
"""
)

for data in self._data:
f.write(data)
f.close()

def __str__(self):
data = ''.join(self._data)
self._data = [ data ]
data = "".join(self._data)
self._data = [data]
return data

def __getitem__(self, item):
Expand Down Expand Up @@ -231,21 +243,21 @@ def _append(self, data):
self._data.append(data)
return

initial_newline = not self._data or self._data[-1] == '\n'
initial_newline = not self._data or self._data[-1] == "\n"
for line in data.splitlines():
if line:
if self._indent_level:
self._data.append(' ' * self._indent_level)
self._data.append(" " * self._indent_level)
self._data.append(line)

if line or not initial_newline:
self._data.append('\n')
self._data.append("\n")

initial_newline = False

def __call__(self, *args, **kwargs):
if not args:
self._data.append('\n')
self._data.append("\n")
return

format = args[0]
Expand All @@ -254,51 +266,56 @@ def __call__(self, *args, **kwargs):
frame = inspect.currentframe().f_back

l = lookup(self, frame, *args, **kwargs)

def convert(match):
ident = match.group('lone')
ident = match.group("lone")
# check for a lone identifier
if ident:
indent = match.group('indent') # must be spaces
lone = '%s' % (l[ident], )
indent = match.group("indent") # must be spaces
lone = "%s" % (l[ident],)

def indent_lines(gen):
for line in gen:
yield indent
yield line
return ''.join(indent_lines(lone.splitlines(True)))

return "".join(indent_lines(lone.splitlines(True)))

# check for an identifier, braced or not
ident = match.group('ident') or match.group('b_ident')
ident = match.group("ident") or match.group("b_ident")
if ident is not None:
return '%s' % (l[ident], )
return "%s" % (l[ident],)

# check for a positional parameter, braced or not
pos = match.group('pos') or match.group('b_pos')
pos = match.group("pos") or match.group("b_pos")
if pos is not None:
pos = int(pos)
if pos > len(args):
raise ValueError \
('Positional parameter #%d not found in pattern' % pos,
code_formatter.pattern)
return '%s' % (args[int(pos)], )
raise ValueError(
"Positional parameter #%d not found in pattern" % pos,
code_formatter.pattern,
)
return "%s" % (args[int(pos)],)

# check for a double braced expression
eval_expr = match.group('eval')
eval_expr = match.group("eval")
if eval_expr is not None:
result = eval(eval_expr, {}, l)
return '%s' % (result, )
return "%s" % (result,)

# check for an escaped delimiter
if match.group('escaped') is not None:
return '$'
if match.group("escaped") is not None:
return "$"

# At this point, we have to match invalid
if match.group('invalid') is None:
if match.group("invalid") is None:
# didn't match invalid!
raise ValueError('Unrecognized named group in pattern',
code_formatter.pattern)
raise ValueError(
"Unrecognized named group in pattern",
code_formatter.pattern,
)

i = match.start('invalid')
i = match.start("invalid")
if i == 0:
colno = 1
lineno = 1
Expand All @@ -307,52 +324,64 @@ def indent_lines(gen):
colno = i - sum(len(z) for z in lines)
lineno = len(lines)

raise ValueError('Invalid format string: line %d, col %d' %
(lineno, colno))
raise ValueError(
"Invalid format string: line %d, col %d" % (lineno, colno)
)

d = code_formatter.pattern.sub(convert, format)
self._append(d)

__all__ = [ "code_formatter" ]

if __name__ == '__main__':
__all__ = ["code_formatter"]

if __name__ == "__main__":
from .code_formatter import code_formatter

f = code_formatter()

class Foo(dict):
def __init__(self, **kwargs):
self.update(kwargs)

def __getattr__(self, attr):
return self[attr]

x = "this is a test"
l = [ [Foo(x=[Foo(y=9)])] ]
l = [[Foo(x=[Foo(y=9)])]]

y = code_formatter()
y('''
y(
"""
{
this_is_a_test();
}
''')
f(' $y')
f('''$__file__:$__line__
{''')
"""
)
f(" $y")
f(
"""$__file__:$__line__
{"""
)
f("${{', '.join(str(x) for x in range(4))}}")
f('${x}')
f('$x')
f("${x}")
f("$x")
f.indent()
for i in range(5):
f('$x')
f('$i')
f('$0', "zero")
f('$1 $0', "zero", "one")
f('${0}', "he went")
f('${0}asdf', "he went")
f("$x")
f("$i")
f("$0", "zero")
f("$1 $0", "zero", "one")
f("${0}", "he went")
f("${0}asdf", "he went")
f.dedent()

f('''
f(
"""
${{l[0][0]["x"][0].y}}
}
''', 1, 9)
""",
1,
9,
)

print(f, end=' ')
print(f, end=" ")
Loading

0 comments on commit 2bc5a8b

Please sign in to comment.