From aaead0e08715c012ee31e9f0a6d983cd8bcfc652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Mon, 29 Apr 2024 00:13:05 +0000 Subject: [PATCH 1/7] Fix ament_black for new get_sources API --- ament_black/ament_black/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index af913c5..43bd625 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -24,14 +24,13 @@ from xml.sax.saxutils import escape from xml.sax.saxutils import quoteattr -from black import get_sources +from black import get_sources, find_project_root from black import main as black from black import re_compile_maybe_verbose from black.concurrency import maybe_install_uvloop from black.const import DEFAULT_EXCLUDES from black.const import DEFAULT_INCLUDES from black.report import Report -import click from unidiff import PatchSet @@ -79,7 +78,7 @@ def main(argv=sys.argv[1:]): # TODO(Nacho): Inject the config file results into the ctx (use read_pyproject_toml) sources = get_sources( - ctx=click.Context(black), + root=find_project_root(tuple(args.paths))[0], src=tuple(args.paths), quiet=True, verbose=False, From 582d6715c5206366d17b63889cecb07ebce1e6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Mon, 29 Apr 2024 00:18:24 +0000 Subject: [PATCH 2/7] Fix import order --- ament_black/ament_black/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index 43bd625..a4b5b91 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -24,7 +24,7 @@ from xml.sax.saxutils import escape from xml.sax.saxutils import quoteattr -from black import get_sources, find_project_root +from black import find_project_root, get_sources from black import main as black from black import re_compile_maybe_verbose from black.concurrency import maybe_install_uvloop From de4a9c127af9a9a224c9451ce5ef56520cd4fe44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Sat, 27 Jul 2024 08:39:16 +0000 Subject: [PATCH 3/7] Add backward compatibility with older black versions --- ament_black/ament_black/main.py | 46 ++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index a4b5b91..67e8b19 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -21,6 +21,13 @@ import sys import tempfile import time + +from packaging.version import Version +from importlib import metadata + +BLACK_VERSION = metadata.version('black') +BLACK_OLD_GET_SOURCES_API = Version(BLACK_VERSION) < Version("23.9.0") + from xml.sax.saxutils import escape from xml.sax.saxutils import quoteattr @@ -31,6 +38,7 @@ from black.const import DEFAULT_EXCLUDES from black.const import DEFAULT_INCLUDES from black.report import Report +import click from unidiff import PatchSet @@ -77,18 +85,32 @@ def main(argv=sys.argv[1:]): return 1 # TODO(Nacho): Inject the config file results into the ctx (use read_pyproject_toml) - sources = get_sources( - root=find_project_root(tuple(args.paths))[0], - src=tuple(args.paths), - quiet=True, - verbose=False, - include=re_compile_maybe_verbose(DEFAULT_INCLUDES), - exclude=re_compile_maybe_verbose(DEFAULT_EXCLUDES), - extend_exclude=None, - force_exclude=None, - report=Report(), - stdin_filename='', - ) + if BLACK_OLD_GET_SOURCES_API: + sources = get_sources( + ctx=click.Context(black), + src=tuple(args.paths), + quiet=True, + verbose=False, + include=re_compile_maybe_verbose(DEFAULT_INCLUDES), + exclude=re_compile_maybe_verbose(DEFAULT_EXCLUDES), + extend_exclude=None, + force_exclude=None, + report=Report(), + stdin_filename='', + ) + else: + sources = get_sources( + root=find_project_root(tuple(args.paths))[0], + src=tuple(args.paths), + quiet=True, + verbose=False, + include=re_compile_maybe_verbose(DEFAULT_INCLUDES), + exclude=re_compile_maybe_verbose(DEFAULT_EXCLUDES), + extend_exclude=None, + force_exclude=None, + report=Report(), + stdin_filename='', + ) checked_files = [str(path) for path in sources] if args.xunit_file: From 4df9c372a8a622862baf9321813c2c59e17251e2 Mon Sep 17 00:00:00 2001 From: Ignacio Vizzo Date: Mon, 29 Jul 2024 11:35:09 +0200 Subject: [PATCH 4/7] style --- ament_black/ament_black/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index 67e8b19..81aff0c 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -17,21 +17,22 @@ import argparse import contextlib +from importlib import metadata import os import sys import tempfile import time from packaging.version import Version -from importlib import metadata BLACK_VERSION = metadata.version('black') -BLACK_OLD_GET_SOURCES_API = Version(BLACK_VERSION) < Version("23.9.0") +BLACK_OLD_GET_SOURCES_API = Version(BLACK_VERSION) < Version('23.9.0') from xml.sax.saxutils import escape from xml.sax.saxutils import quoteattr -from black import find_project_root, get_sources +from black import find_project_root +from black import get_sources from black import main as black from black import re_compile_maybe_verbose from black.concurrency import maybe_install_uvloop From c6d2c946aac098d2b64634285ca58cf1fabeea96 Mon Sep 17 00:00:00 2001 From: Ignacio Vizzo Date: Mon, 29 Jul 2024 11:41:51 +0200 Subject: [PATCH 5/7] Move global variables inside method to fix flake8 --- ament_black/ament_black/main.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index 81aff0c..f294818 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -22,12 +22,6 @@ import sys import tempfile import time - -from packaging.version import Version - -BLACK_VERSION = metadata.version('black') -BLACK_OLD_GET_SOURCES_API = Version(BLACK_VERSION) < Version('23.9.0') - from xml.sax.saxutils import escape from xml.sax.saxutils import quoteattr @@ -40,6 +34,7 @@ from black.const import DEFAULT_INCLUDES from black.report import Report import click +from packaging.version import Version from unidiff import PatchSet @@ -86,6 +81,8 @@ def main(argv=sys.argv[1:]): return 1 # TODO(Nacho): Inject the config file results into the ctx (use read_pyproject_toml) + BLACK_VERSION = metadata.version('black') + BLACK_OLD_GET_SOURCES_API = Version(BLACK_VERSION) < Version('23.9.0') if BLACK_OLD_GET_SOURCES_API: sources = get_sources( ctx=click.Context(black), @@ -260,7 +257,6 @@ def get_xunit_content(report, testname, elapsed, checked_files): """ % data ) - xml += '\n' return xml From d7647593fe65c1e22de6f6ea243c6fb103de7e31 Mon Sep 17 00:00:00 2001 From: Ignacio Vizzo Date: Mon, 29 Jul 2024 11:45:30 +0200 Subject: [PATCH 6/7] Import only for newer versions --- ament_black/ament_black/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index f294818..9fd2b06 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -25,7 +25,6 @@ from xml.sax.saxutils import escape from xml.sax.saxutils import quoteattr -from black import find_project_root from black import get_sources from black import main as black from black import re_compile_maybe_verbose @@ -97,6 +96,7 @@ def main(argv=sys.argv[1:]): stdin_filename='', ) else: + from black import find_project_root sources = get_sources( root=find_project_root(tuple(args.paths))[0], src=tuple(args.paths), From 58b7d8120c68b853dabd8e871c045da1e410e6ef Mon Sep 17 00:00:00 2001 From: Ignacio Vizzo Date: Mon, 29 Jul 2024 11:48:12 +0200 Subject: [PATCH 7/7] Last tweak --- ament_black/ament_black/main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ament_black/ament_black/main.py b/ament_black/ament_black/main.py index 9fd2b06..e210991 100755 --- a/ament_black/ament_black/main.py +++ b/ament_black/ament_black/main.py @@ -80,9 +80,8 @@ def main(argv=sys.argv[1:]): return 1 # TODO(Nacho): Inject the config file results into the ctx (use read_pyproject_toml) - BLACK_VERSION = metadata.version('black') - BLACK_OLD_GET_SOURCES_API = Version(BLACK_VERSION) < Version('23.9.0') - if BLACK_OLD_GET_SOURCES_API: + BLACK_VERSION = Version(metadata.version('black')) + if BLACK_VERSION < Version('23.9.0'): sources = get_sources( ctx=click.Context(black), src=tuple(args.paths), @@ -96,7 +95,10 @@ def main(argv=sys.argv[1:]): stdin_filename='', ) else: + # Hack to support newer versions of black in ROS Jazzy + # https://github.com/botsandus/ament_black/issues/12 from black import find_project_root + sources = get_sources( root=find_project_root(tuple(args.paths))[0], src=tuple(args.paths),