diff --git a/.github/actions/gitlint/action.yml b/.github/actions/gitlint/action.yml index d1a190c..7085f35 100644 --- a/.github/actions/gitlint/action.yml +++ b/.github/actions/gitlint/action.yml @@ -1,3 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + name: "Gitlint Action" description: "An action to install and run Gitlint on PR commits" inputs: diff --git a/.github/workflows/copyright.yml b/.github/workflows/copyright.yml index 4315b26..3b063af 100644 --- a/.github/workflows/copyright.yml +++ b/.github/workflows/copyright.yml @@ -1,3 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + name: Copyright checks on: pull_request: diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index cf8b378..3c53757 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,3 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + name: Documentation build on: pull_request: diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 6cf2b38..eaf7390 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -1,3 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + name: Formatting checks on: pull_request: diff --git a/.github/workflows/gitlint.yml b/.github/workflows/gitlint.yml index ae7d067..01087a9 100644 --- a/.github/workflows/gitlint.yml +++ b/.github/workflows/gitlint.yml @@ -1,3 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + name: Gitlint check on: pull_request: diff --git a/BUILD b/BUILD index 946c504..b423cbc 100644 --- a/BUILD +++ b/BUILD @@ -11,6 +11,8 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* +load("//tools/cr_checker:cr_checker.bzl", "copyright_checker") + test_suite( name = "format.check", tests = ["//tools/format:format.check"], @@ -21,25 +23,16 @@ alias( actual = "//tools/format:format.fix", ) -alias( - name = "copyright.check", - actual = "//tools/cr_checker:copyright.check", -) - -alias( - name = "copyright.fix", - actual = "//tools/cr_checker:copyright.fix", -) - -filegroup( - name = "repo_directories", +copyright_checker( + name = "copyright", srcs = [ + ".github", "docs", "tools", + "//:BUILD", + "//:MODULE.bazel", ], - visibility = [ - "//tools/cr_checker:__subpackages__", - ], + visibility = ["//visibility:public"], ) exports_files([ diff --git a/MODULE.bazel b/MODULE.bazel index 4d9f2cc..0a05c11 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,3 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + module( name = "score_platform", version = "0.1", diff --git a/tools/cr_checker/BUILD b/tools/cr_checker/BUILD index ac13f73..e69de29 100644 --- a/tools/cr_checker/BUILD +++ b/tools/cr_checker/BUILD @@ -1,35 +0,0 @@ -# ******************************************************************************* -# Copyright (c) 2024 Contributors to the Eclipse Foundation -# -# See the NOTICE file(s) distributed with this work for additional -# information regarding copyright ownership. -# -# This program and the accompanying materials are made available under the -# terms of the Apache License Version 2.0 which is available at -# https://www.apache.org/licenses/LICENSE-2.0 -# -# SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* - -load("//tools/cr_checker:cr_checker.bzl", "check", "fix") - -check( - name = "copyright.check", - srcs = [ - "//:BUILD", - "//:MODULE.bazel", - "//:repo_directories", - ], - offset = 24, - visibility = ["//visibility:public"], -) - -fix( - name = "copyright.fix", - srcs = [ - "//:BUILD", - "//:MODULE.bazel", - "//:repo_directories", - ], - visibility = ["//visibility:public"], -) diff --git a/tools/cr_checker/cr_checker.bzl b/tools/cr_checker/cr_checker.bzl index cdc1fb3..b41ff3c 100644 --- a/tools/cr_checker/cr_checker.bzl +++ b/tools/cr_checker/cr_checker.bzl @@ -13,70 +13,7 @@ """Defines Bazel rules for running copyright checks and fixes.""" -load("@bazel_skylib//lib:paths.bzl", "paths") - -def _cr_impl(ctx): - """ - Implementation function for the copyright rule. This function sets up the - necessary command to execute the Python script with the specified mode and source files. - Args: - ctx (RuleContext): The rule context containing attributes and configuration. - Returns: - DefaultInfo: Bazel default information provider with the output log file. - """ - script = ctx.attr._script - files = [] - - files = ctx.files.srcs - extensions = ctx.attr.extensions - - text_exts = "" - if len(extensions): - text_exts = "-e {exts}".format( - exts = " ".join([exts for exts in ctx.attr.extensions]), - ) - - ctx.actions.write( - output = ctx.outputs.executable, - content = "{tool}.py -t {template} {extensions} {fix} {use_mmap} {debug} {log_file} {offset} {srcs}".format( - tool = script.files_to_run.executable.short_path, - template = ctx.file.template.path, - extensions = text_exts, - fix = "--fix" if ctx.attr.fix else "", - debug = "--verbose" if ctx.attr.debug else "", - use_mmap = "--use_memory_map" if ctx.attr.use_memory_map else "", - log_file = "--log-file log.txt" if ctx.attr.log_file else "", - offset = "--offset %s" % ctx.attr.offset if ctx.attr.offset else "", - srcs = " ".join([f.path for f in files]), - ), - ) - - runfiles = ctx.runfiles( - files = files + [ctx.file.template], - ).merge( - script.default_runfiles, - ) - return [DefaultInfo(runfiles = runfiles)] - -_cr_rule = rule( - implementation = _cr_impl, - executable = True, - attrs = { - "srcs": attr.label_list(allow_files = True, mandatory = False, default = []), - "template": attr.label(allow_single_file = True), - "extensions": attr.string_list(mandatory = False, default = []), - "debug": attr.bool(mandatory = False, default = False), - "fix": attr.bool(mandatory = False, default = False), - "offset": attr.int(mandatory = False, default = 0), - "log_file": attr.bool(mandatory = False, default = False), - "use_memory_map": attr.bool(mandatory = False, default = False), - "_script": attr.label( - default = Label("//tools/cr_checker/tool:cr_checker"), - ), - }, -) - -def check( +def copyright_checker( name, srcs, visibility, @@ -84,49 +21,70 @@ def check( extensions = [], offset = 0, debug = False, - log_file = False, - use_memory_map = False): + use_memory_map = False, + fix = False): """ - Defines a Bazel target for checking files without modifying them. - Args: - name (str): The name of the target. - srcs (list of labels): The source files to check. - """ - _cr_rule( - name = name, - srcs = srcs, - template = template, - offset = offset, - debug = debug, - log_file = log_file, - use_memory_map = use_memory_map, - visibility = visibility, - ) + Defines a custom build rule for checking and optionally fixing files for compliance + with specific requirements, such as copyright headers. -def fix( - name, - srcs, - visibility, - template = "//tools/cr_checker/resources:templates", - extensions = [], - offset = 0, - debug = False, - log_file = False, - use_memory_map = False): - """ - Defines a Bazel target for fixing files by adding missing copyright text. Args: - name (str): The name of the target. - srcs (list of labels): The source files to fix. + name (str): The name of the rule, used as an identifier in the build system. + srcs (list): A list of source file paths to check. + visibility (list): A list defining the visibility of the rule, specifying which + targets can use this rule. + template (str, optional): Path to the template resource used for validation. + Defaults to "//tools/cr_checker/resources:templates". + extensions (list, optional): A list of file extensions to filter the source files. + Defaults to an empty list, meaning all files are checked. + offset (int, optional): The line offset for applying checks or modifications. + Defaults to 0. + debug (bool, optional): Whether to enable debug mode, providing additional logs. + Defaults to False. + use_memory_map (bool, optional): Whether to use memory mapping for large files to + improve performance. Defaults to False. + fix (bool, optional): Whether to apply fixes to files instead of just reporting issues. + Defaults to False. + + Returns: + None: This function defines a rule for a build system and does not return a value. """ - _cr_rule( - name = name, - srcs = srcs, - template = template, - offset = offset, - debug = debug, - log_file = log_file, - use_memory_map = use_memory_map, - fix = True, - visibility = visibility, - ) + t_names = [ + "{}.check".format(name), + "{}.fix".format(name), + ] + + args = ["-t $(location {})".format(template)] + data = [] + if len(extensions): + args.append("-e {exts}".format( + exts = " ".join([exts for exts in extensions]), + )) + + if offset: + args.append("--offset {}".format(offset)) + + if debug: + args.append("-v") + + if use_memory_map: + args.append("--use_memory_map") + + for src in srcs: + args.append("$(locations {})".format(src)) + + for t_name in t_names: + if t_name == "{}.fix".format(name): + args.insert(0, "--fix") + + native.py_binary( + name = t_name, + main = "cr_checker.py", + srcs = [ + "//tools/cr_checker/tool:cr_checker_lib", + ], + args = args, + data = srcs + [ + template, + ], + visibility = visibility, + ) diff --git a/tools/cr_checker/resources/templates.ini b/tools/cr_checker/resources/templates.ini index fc0a711..01cf29c 100644 --- a/tools/cr_checker/resources/templates.ini +++ b/tools/cr_checker/resources/templates.ini @@ -10,7 +10,6 @@ # # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* - [cpp,c,h,hpp] /******************************************************************************** * Copyright (c) 2024 Contributors to the Eclipse Foundation @@ -24,8 +23,7 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ - -[py,sh,bzl,ini,BUILD] +[py,sh,bzl,ini,yml,BUILD,bazel] # ******************************************************************************* # Copyright (c) 2024 Contributors to the Eclipse Foundation # @@ -38,7 +36,6 @@ # # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* - [rst] .. # ******************************************************************************* @@ -53,4 +50,3 @@ # # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* - diff --git a/tools/cr_checker/tool/BUILD b/tools/cr_checker/tool/BUILD index 39ef814..a4f92a1 100644 --- a/tools/cr_checker/tool/BUILD +++ b/tools/cr_checker/tool/BUILD @@ -11,8 +11,8 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -py_binary( - name = "cr_checker", +py_library( + name = "cr_checker_lib", srcs = [ "cr_checker.py", ], diff --git a/tools/cr_checker/tool/cr_checker.py b/tools/cr_checker/tool/cr_checker.py index be3a380..a9603f6 100755 --- a/tools/cr_checker/tool/cr_checker.py +++ b/tools/cr_checker/tool/cr_checker.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 - # ******************************************************************************* # Copyright (c) 2024 Contributors to the Eclipse Foundation # @@ -12,7 +10,6 @@ # # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* - """The tool for checking if artifacts have proper copyright.""" import argparse @@ -278,7 +275,7 @@ def get_files_from_dir(directory, exts=None): collected_files = [] LOGGER.debug("Getting files from directory: %s", directory) for path in directory.rglob("*"): - if path.is_file(): + if path.is_file() and path.stat().st_size != 0: if exts is None or path.suffix[1:] in exts: collected_files.append(path) return collected_files @@ -536,16 +533,24 @@ def main(argv=None): LOGGER.info("Process completed.") LOGGER.info( "Total files without copyright: %s%d%s", - COLORS["GREEN"], + COLORS["RED"] if total_no > 0 else COLORS["GREEN"], total_no, COLORS["ENDC"], ) - LOGGER.info( - "Total files that were fixed: %s%d%s", - COLORS["RED"] if total_fixes > 0 else COLORS["GREEN"], - total_fixes, - COLORS["ENDC"], - ) + if args.fix: + total_not_fixed = total_no - total_fixes + LOGGER.info( + "Total files that were fixed: %s%d%s", + COLORS["GREEN"], + total_fixes, + COLORS["ENDC"], + ) + LOGGER.info( + "Total files that were NOT fixed: %s%d%s", + COLORS["RED"] if total_not_fixed > 0 else COLORS["GREEN"], + total_not_fixed, + COLORS["ENDC"], + ) LOGGER.info("=" * 64) return 0 if total_no == 0 else 1