Skip to content

Commit

Permalink
Fix ament_black for new get_sources API (#13)
Browse files Browse the repository at this point in the history
* Fix ament_black for new get_sources API

* Fix import order

* Add backward compatibility with older black versions

* style

* Move global variables inside method to fix flake8

* Import only for newer versions

* Last tweak

---------

Co-authored-by: Ignacio Vizzo <ignacio@dexory.com>
  • Loading branch information
bjsowa and nachovizzo authored Jul 29, 2024
1 parent febe275 commit 4f3cea9
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions ament_black/ament_black/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import argparse
import contextlib
from importlib import metadata
import os
import sys
import tempfile
Expand All @@ -32,6 +33,7 @@
from black.const import DEFAULT_INCLUDES
from black.report import Report
import click
from packaging.version import Version
from unidiff import PatchSet


Expand Down Expand Up @@ -78,18 +80,37 @@ 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(
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='',
)
BLACK_VERSION = Version(metadata.version('black'))
if BLACK_VERSION < Version('23.9.0'):
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:
# 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),
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:
Expand Down Expand Up @@ -238,7 +259,6 @@ def get_xunit_content(report, testname, elapsed, checked_files):
"""
% data
)

xml += '</testsuite>\n'
return xml

Expand Down

0 comments on commit 4f3cea9

Please sign in to comment.