SCons: Format buildsystem files with psf/black

Configured for a max line length of 120 characters.

psf/black is very opinionated and purposely doesn't leave much room for
configuration. The output is mostly OK so that should be fine for us,
but some things worth noting:

- Manually wrapped strings will be reflowed, so by using a line length
  of 120 for the sake of preserving readability for our long command
  calls, it also means that some manually wrapped strings are back on
  the same line and should be manually merged again.

- Code generators using string concatenation extensively look awful,
  since black puts each operand on a single line. We need to refactor
  these generators to use more pythonic string formatting, for which
  many options are available (`%`, `format` or f-strings).

- CI checks and a pre-commit hook will be added to ensure that future
  buildsystem changes are well-formatted.

(cherry picked from commit cd4e46ee65)
This commit is contained in:
Rémi Verschelde 2020-03-30 08:28:32 +02:00
parent c3d04167a4
commit 7bf9787921
189 changed files with 4050 additions and 3315 deletions

View file

@ -14,7 +14,6 @@ else:
def run_in_subprocess(builder_function):
@functools.wraps(builder_function)
def wrapper(target, source, env):
@ -23,38 +22,36 @@ def run_in_subprocess(builder_function):
source = [node.srcnode().abspath for node in source]
# Short circuit on non-Windows platforms, no need to run in subprocess
if sys.platform not in ('win32', 'cygwin'):
if sys.platform not in ("win32", "cygwin"):
return builder_function(target, source, env)
# Identify module
module_name = builder_function.__module__
function_name = builder_function.__name__
module_path = sys.modules[module_name].__file__
if module_path.endswith('.pyc') or module_path.endswith('.pyo'):
if module_path.endswith(".pyc") or module_path.endswith(".pyo"):
module_path = module_path[:-1]
# Subprocess environment
subprocess_env = os.environ.copy()
subprocess_env['PYTHONPATH'] = os.pathsep.join([os.getcwd()] + sys.path)
subprocess_env["PYTHONPATH"] = os.pathsep.join([os.getcwd()] + sys.path)
# Keep only JSON serializable environment items
filtered_env = dict(
(key, value)
for key, value in env.items()
if isinstance(value, JSON_SERIALIZABLE_TYPES)
)
filtered_env = dict((key, value) for key, value in env.items() if isinstance(value, JSON_SERIALIZABLE_TYPES))
# Save parameters
args = (target, source, filtered_env)
data = dict(fn=function_name, args=args)
json_path = os.path.join(os.environ['TMP'], uuid.uuid4().hex + '.json')
with open(json_path, 'wt') as json_file:
json_path = os.path.join(os.environ["TMP"], uuid.uuid4().hex + ".json")
with open(json_path, "wt") as json_file:
json.dump(data, json_file, indent=2)
json_file_size = os.stat(json_path).st_size
print('Executing builder function in subprocess: '
'module_path=%r, parameter_file=%r, parameter_file_size=%r, target=%r, source=%r' % (
module_path, json_path, json_file_size, target, source))
print(
"Executing builder function in subprocess: "
"module_path=%r, parameter_file=%r, parameter_file_size=%r, target=%r, source=%r"
% (module_path, json_path, json_file_size, target, source)
)
try:
exit_code = subprocess.call([sys.executable, module_path, json_path], env=subprocess_env)
finally:
@ -62,13 +59,15 @@ def run_in_subprocess(builder_function):
os.remove(json_path)
except (OSError, IOError) as e:
# Do not fail the entire build if it cannot delete a temporary file
print('WARNING: Could not delete temporary file: path=%r; [%s] %s' %
(json_path, e.__class__.__name__, e))
print(
"WARNING: Could not delete temporary file: path=%r; [%s] %s" % (json_path, e.__class__.__name__, e)
)
# Must succeed
if exit_code:
raise RuntimeError(
'Failed to run builder function in subprocess: module_path=%r; data=%r' % (module_path, data))
"Failed to run builder function in subprocess: module_path=%r; data=%r" % (module_path, data)
)
return wrapper
@ -78,5 +77,5 @@ def subprocess_main(namespace):
with open(sys.argv[1]) as json_file:
data = json.load(json_file)
fn = namespace[data['fn']]
fn(*data['args'])
fn = namespace[data["fn"]]
fn(*data["args"])