Bug 1654103: Standardize on Black for Python code in mozilla-central.

Allow-list all Python code in tree for use with the black linter, and re-format all code in-tree accordingly.

To produce this patch I did all of the following:

1. Make changes to tools/lint/black.yml to remove include: stanza and update list of source extensions.

2. Run ./mach lint --linter black --fix

3. Make some ad-hoc manual updates to python/mozbuild/mozbuild/test/configure/test_configure.py -- it has some hard-coded line numbers that the reformat breaks.

4. Make some ad-hoc manual updates to `testing/marionette/client/setup.py`, `testing/marionette/harness/setup.py`, and `testing/firefox-ui/harness/setup.py`, which have hard-coded regexes that break after the reformat.

5. Add a set of exclusions to black.yml. These will be deleted in a follow-up bug (1672023).

# ignore-this-changeset

Differential Revision: https://phabricator.services.mozilla.com/D94045
This commit is contained in:
Ricky Stewart
2020-10-26 18:34:53 +00:00
parent b959dcf2b1
commit 31b8ca9716
3432 changed files with 164119 additions and 128510 deletions

View File

@@ -21,18 +21,16 @@ except ImportError:
base_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(base_dir, 'python', 'mozboot'))
sys.path.insert(0, os.path.join(base_dir, 'python', 'mozbuild'))
sys.path.insert(0, os.path.join(base_dir, 'third_party', 'python', 'six'))
sys.path.insert(0, os.path.join(base_dir, "python", "mozboot"))
sys.path.insert(0, os.path.join(base_dir, "python", "mozbuild"))
sys.path.insert(0, os.path.join(base_dir, "third_party", "python", "six"))
from mozbuild.configure import (
ConfigureSandbox,
TRACE,
)
from mozbuild.pythonutil import iter_modules_in_path
from mozbuild.backend.configenvironment import PartialConfigEnvironment
from mozbuild.util import (
write_indented_repr,
)
from mozbuild.util import write_indented_repr
import mozpack.path as mozpath
import six
@@ -40,46 +38,46 @@ import six
def main(argv):
config = {}
if 'OLD_CONFIGURE' not in os.environ:
os.environ['OLD_CONFIGURE'] = os.path.join(base_dir, 'old-configure')
if "OLD_CONFIGURE" not in os.environ:
os.environ["OLD_CONFIGURE"] = os.path.join(base_dir, "old-configure")
sandbox = ConfigureSandbox(config, os.environ, argv)
clobber_file = 'CLOBBER'
clobber_file = "CLOBBER"
if not os.path.exists(clobber_file):
# Simply touch the file.
with open(clobber_file, 'a'):
with open(clobber_file, "a"):
pass
if os.environ.get('MOZ_CONFIGURE_TRACE'):
if os.environ.get("MOZ_CONFIGURE_TRACE"):
sandbox._logger.setLevel(TRACE)
sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
sandbox.run(os.path.join(os.path.dirname(__file__), "moz.configure"))
if sandbox._help:
return 0
logging.getLogger('moz.configure').info('Creating config.status')
logging.getLogger("moz.configure").info("Creating config.status")
old_js_configure_substs = config.pop('OLD_JS_CONFIGURE_SUBSTS', None)
old_js_configure_defines = config.pop('OLD_JS_CONFIGURE_DEFINES', None)
old_js_configure_substs = config.pop("OLD_JS_CONFIGURE_SUBSTS", None)
old_js_configure_defines = config.pop("OLD_JS_CONFIGURE_DEFINES", None)
if old_js_configure_substs or old_js_configure_defines:
js_config = config.copy()
pwd = os.getcwd()
try:
try:
os.makedirs('js/src')
os.makedirs("js/src")
except OSError as e:
if e.errno != errno.EEXIST:
raise
os.chdir('js/src')
js_config['OLD_CONFIGURE_SUBSTS'] = old_js_configure_substs
js_config['OLD_CONFIGURE_DEFINES'] = old_js_configure_defines
os.chdir("js/src")
js_config["OLD_CONFIGURE_SUBSTS"] = old_js_configure_substs
js_config["OLD_CONFIGURE_DEFINES"] = old_js_configure_defines
# The build system frontend expects $objdir/js/src/config.status
# to have $objdir/js/src as topobjdir.
# We want forward slashes on all platforms.
js_config['TOPOBJDIR'] += '/js/src'
js_config["TOPOBJDIR"] += "/js/src"
config_status(js_config, execute=False)
finally:
os.chdir(pwd)
@@ -88,7 +86,7 @@ def main(argv):
def check_unicode(obj):
'''Recursively check that all strings in the object are unicode strings.'''
"""Recursively check that all strings in the object are unicode strings."""
if isinstance(obj, dict):
result = True
for k, v in six.iteritems(obj):
@@ -115,30 +113,38 @@ def config_status(config, execute=True):
# untouched for now.
def sanitize_config(v):
if v is True:
return '1'
return "1"
if v is False:
return ''
return ""
# Serialize types that look like lists and tuples as lists.
if not isinstance(v, (bytes, six.text_type, dict)) and isinstance(v, Iterable):
return list(v)
return v
sanitized_config = {}
sanitized_config['substs'] = {
k: sanitize_config(v) for k, v in six.iteritems(config)
if k not in ('DEFINES', 'TOPSRCDIR', 'TOPOBJDIR', 'CONFIG_STATUS_DEPS',
'OLD_CONFIGURE_SUBSTS', 'OLD_CONFIGURE_DEFINES')
sanitized_config["substs"] = {
k: sanitize_config(v)
for k, v in six.iteritems(config)
if k
not in (
"DEFINES",
"TOPSRCDIR",
"TOPOBJDIR",
"CONFIG_STATUS_DEPS",
"OLD_CONFIGURE_SUBSTS",
"OLD_CONFIGURE_DEFINES",
)
}
for k, v in config['OLD_CONFIGURE_SUBSTS']:
sanitized_config['substs'][k] = sanitize_config(v)
sanitized_config['defines'] = {
k: sanitize_config(v) for k, v in six.iteritems(config['DEFINES'])
for k, v in config["OLD_CONFIGURE_SUBSTS"]:
sanitized_config["substs"][k] = sanitize_config(v)
sanitized_config["defines"] = {
k: sanitize_config(v) for k, v in six.iteritems(config["DEFINES"])
}
for k, v in config['OLD_CONFIGURE_DEFINES']:
sanitized_config['defines'][k] = sanitize_config(v)
sanitized_config['topsrcdir'] = config['TOPSRCDIR']
sanitized_config['topobjdir'] = config['TOPOBJDIR']
sanitized_config['mozconfig'] = config.get('MOZCONFIG')
for k, v in config["OLD_CONFIGURE_DEFINES"]:
sanitized_config["defines"][k] = sanitize_config(v)
sanitized_config["topsrcdir"] = config["TOPSRCDIR"]
sanitized_config["topobjdir"] = config["TOPOBJDIR"]
sanitized_config["mozconfig"] = config.get("MOZCONFIG")
if not check_unicode(sanitized_config):
print("Configuration should be all unicode.", file=sys.stderr)
@@ -153,10 +159,7 @@ def config_status(config, execute=True):
# converted to lists.
def normalize(obj):
if isinstance(obj, dict):
return {
k: normalize(v)
for k, v in six.iteritems(obj)
}
return {k: normalize(v) for k, v in six.iteritems(obj)}
if isinstance(obj, six.text_type):
return six.text_type(obj)
if isinstance(obj, Iterable):
@@ -168,49 +171,61 @@ def config_status(config, execute=True):
# Create config.status. Eventually, we'll want to just do the work it does
# here, when we're able to skip configure tests/use cached results/not rely
# on autoconf.
with codecs.open('config.status', 'w', 'utf-8') as fh:
fh.write(textwrap.dedent('''\
with codecs.open("config.status", "w", "utf-8") as fh:
fh.write(
textwrap.dedent(
"""\
#!%(python)s
# coding=utf-8
from __future__ import unicode_literals
''') % {'python': config['PYTHON3']})
"""
)
% {"python": config["PYTHON3"]}
)
for k, v in sorted(six.iteritems(sanitized_config)):
fh.write('%s = ' % k)
fh.write("%s = " % k)
write_indented_repr(fh, v)
fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
"'substs', 'mozconfig']")
fh.write(
"__all__ = ['topobjdir', 'topsrcdir', 'defines', " "'substs', 'mozconfig']"
)
if execute:
fh.write(textwrap.dedent('''
fh.write(
textwrap.dedent(
"""
if __name__ == '__main__':
from mozbuild.util import patch_main
patch_main()
from mozbuild.config_status import config_status
args = dict([(name, globals()[name]) for name in __all__])
config_status(**args)
'''))
"""
)
)
partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
partial_config = PartialConfigEnvironment(config["TOPOBJDIR"])
partial_config.write_vars(sanitized_config)
# Write out a file so the build backend knows to re-run configure when
# relevant Python changes.
with io.open('config_status_deps.in', 'w', encoding='utf-8',
newline='\n') as fh:
with io.open("config_status_deps.in", "w", encoding="utf-8", newline="\n") as fh:
for f in sorted(
itertools.chain(config['CONFIG_STATUS_DEPS'],
iter_modules_in_path(config['TOPOBJDIR'],
config['TOPSRCDIR']))):
fh.write('%s\n' % mozpath.normpath(f))
itertools.chain(
config["CONFIG_STATUS_DEPS"],
iter_modules_in_path(config["TOPOBJDIR"], config["TOPSRCDIR"]),
)
):
fh.write("%s\n" % mozpath.normpath(f))
# Other things than us are going to run this file, so we need to give it
# executable permissions.
os.chmod('config.status', 0o755)
os.chmod("config.status", 0o755)
if execute:
from mozbuild.config_status import config_status
return config_status(args=[], **sanitized_config)
return 0
if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main(sys.argv))