From a63edb989dd104f6eda88a91e7a9a8e3d0496aa0 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 14 Apr 2021 19:12:05 +1000 Subject: [PATCH] add formatting experiment for posterity --- pip/.isort.cfg | 10 ++++ scripts/BUILD.bazel | 6 +++ scripts/format.py | 117 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 pip/.isort.cfg create mode 100755 scripts/format.py diff --git a/pip/.isort.cfg b/pip/.isort.cfg new file mode 100644 index 000000000..903ecee1f --- /dev/null +++ b/pip/.isort.cfg @@ -0,0 +1,10 @@ +[settings] +skip_gitignore=True +profile=black +multi_line_output=3 +include_trailing_comma=True +force_grid_wrap=0 +use_parentheses=True +line_length=88 +ensure_newline_before_comments=true +known_first_party=aqt,anki,tests diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index bfd938208..789ecf996 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -7,3 +7,9 @@ py_binary( stamp = 1, visibility = ["//visibility:public"], ) + +py_binary( + name = "format", + srcs = ["format.py"], + tags = ["manual"], +) diff --git a/scripts/format.py b/scripts/format.py new file mode 100755 index 000000000..104bdf9c1 --- /dev/null +++ b/scripts/format.py @@ -0,0 +1,117 @@ +# Copyright: Ankitects Pty Ltd and contributors +# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +""" +This is an experiment with running some of the formatters outside +of Bazel' testing system. It means we pick up files not explictly +listed as inputs, but also means that work is done even when files +have not changed, and it does not work on Windows. +""" + +import os +import subprocess +import sys +import time +from pathlib import Path + +workspace = Path(os.environ["BUILD_WORKSPACE_DIRECTORY"]) +binroot = workspace / "bazel-bin" +pyroot = binroot / "pip" +tsroot = workspace / "ts" +tsbinroot = tsroot / "node_modules" / ".bin" + +if sys.platform.startswith("win32"): + binext = ".exe" +else: + binext = "" + +# pyargs = ["--check"] +# prettierargs = "--check" +pyargs = [] +prettierargs = "--write" + + +def taken(stamp): + ms = int((time.time() - stamp) * 1000.0) + return f"{ms}ms" + + +failed = False + +print("* Fixing Python formatting...", end="", flush=True) +t = time.time() +if subprocess.run( + [ + str(pyroot / ("black" + binext)), + ] + + pyargs + + [ + "-t", + "py38", + "--exclude='qt/aqt/forms|_gen|bazel-|node_modules'", + "-q", + ".", + ], + cwd=workspace, +).returncode: + failed = True +print(taken(t)) + +print("* Fixing Python imports...", end="", flush=True) +t = time.time() +if subprocess.run( + [ + str(pyroot / ("isort" + binext)), + ] + + pyargs + + [ + "--settings-path", + "pip/.isort.cfg", + "--skip", + "forms", + "--sg", + "*_gen.py", + "--skip", + "generated.py", + "-q", + ".", + ], + cwd=workspace, +).returncode: + failed = True +print(taken(t)) + +print("* Fixing Typescript formatting...", end="\n", flush=True) +t = time.time() +if subprocess.run( + [ + tsbinroot / ("prettier" + binext), + "--config", + tsroot / ".prettierrc", + "--ignore-path", + tsroot / ".prettierignore", + "--loglevel", + "silent", + prettierargs, + "ts", + "qt", + ], + cwd=workspace, +).returncode: + failed = True +print(taken(t)) + +print("* Fixing Fluent formatting...", end="", flush=True) +t = time.time() +if subprocess.run( + [ + binroot / "ftl" / ("format" + binext), + ], + cwd=workspace, +).returncode: + failed = True +print(taken(t)) + + +if failed: + sys.exit(1)