Skip to content

Commit

Permalink
Merge pull request eclipse-score#11 from nradakovic/nira_issue_6
Browse files Browse the repository at this point in the history
ch_checker: Add copyright check for yml files
  • Loading branch information
ltekieli authored Dec 2, 2024
2 parents b439e5c + 0cdf60d commit 3670d55
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 174 deletions.
13 changes: 13 additions & 0 deletions .github/actions/gitlint/action.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/copyright.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/gitlint.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
23 changes: 8 additions & 15 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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([
Expand Down
13 changes: 13 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
35 changes: 0 additions & 35 deletions tools/cr_checker/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
170 changes: 64 additions & 106 deletions tools/cr_checker/cr_checker.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,120 +13,78 @@

"""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,
template = "//tools/cr_checker/resources:templates",
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,
)
6 changes: 1 addition & 5 deletions tools/cr_checker/resources/templates.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

[cpp,c,h,hpp]
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
Expand All @@ -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
#
Expand All @@ -38,7 +36,6 @@
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

[rst]
..
# *******************************************************************************
Expand All @@ -53,4 +50,3 @@
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

Loading

0 comments on commit 3670d55

Please sign in to comment.