You've already forked open-source-firmware-validation
mirror of
https://github.com/Dasharo/open-source-firmware-validation.git
synced 2026-03-06 14:51:55 -08:00
38906581c4
* add git-cliff and reuse Files that do not support comments or that have problems adding comments at the first line should have a separate .license file or a rule inside REUSE.toml (in case there are a lot of such files). .robot files generally support comments at the first line, but robotidy does not want comments to start at first line. It wants so, that everything that is located before first section should be placed inside "Comments" section. But reuse does not support license headers in any sections. So reuse and robotidy have a conflict here. Because there are a lot of .robot files, I have decided to add them into REUSE.toml instead of separate .license files or robotidy exceptions. Signed-off-by: Daniil Klimuk <daniil.klimuk@3mdeb.com> * add LICENSES and license headers to files Files that does not have license headers have either .license file or a rule inside REUSE.toml. Signed-off-by: Daniil Klimuk <daniil.klimuk@3mdeb.com> * .github: ISSUE_TEMPLATE: fix markdownlint Signed-off-by: Daniil Klimuk <daniil.klimuk@3mdeb.com> * README: add git-cliff and reuse Signed-off-by: Daniil Klimuk <daniil.klimuk@3mdeb.com> --------- Signed-off-by: Daniil Klimuk <daniil.klimuk@3mdeb.com>
97 lines
2.9 KiB
Bash
Executable File
97 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# SPDX-FileCopyrightText: 2024 3mdeb <contact@3mdeb.com>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
# Default value for autofix flag
|
|
AUTOFIX="false"
|
|
|
|
usage() {
|
|
echo "Usage: $0 <robot_variable_file1> <robot_variable_file2> ..."
|
|
echo " -f: Autofix mode, removes lines containing unused variables from the robot variable file"
|
|
echo " <robot_variable_file>: Robot file containing variable definitions"
|
|
exit 1
|
|
}
|
|
|
|
# Process command-line options
|
|
while getopts ":f" opt; do
|
|
case $opt in
|
|
f)
|
|
AUTOFIX=true
|
|
;;
|
|
\?)
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Shift the options so that $1 is the first non-option argument
|
|
shift $((OPTIND - 1))
|
|
|
|
if [ "$#" -eq 0 ]; then
|
|
usage
|
|
fi
|
|
|
|
# Loop through each provided robot variable file
|
|
for robot_variable_file in "$@"; do
|
|
# Specify the file paths to search for variable usage
|
|
file_paths=(
|
|
"dasharo-compatibility" "dasharo-security" "dasharo-performance" "dasharo-stability"
|
|
"lib"
|
|
"keywords.robot"
|
|
"platform-configs"
|
|
"trenchboot"
|
|
)
|
|
|
|
# Read variables from the specified robot variable file (only consider the leftmost variable on each line)
|
|
mapfile -t variables < <(awk -F'=' '/^\$\{[^}]+\}=/{gsub(/[[:space:]]/, "", $1); print $1}' "$robot_variable_file" | sort -u)
|
|
|
|
# Initialize an associative array to track variable usage
|
|
declare -A variable_usage
|
|
|
|
# Loop through each file path
|
|
for path in "${file_paths[@]}"; do
|
|
# Loop through each variable
|
|
for var in "${variables[@]}"; do
|
|
# Use grep to check if the variable is used in the files
|
|
grep_result=$(grep "$var" "$path" -ir)
|
|
|
|
# If the grep result is not empty, mark the variable as used
|
|
if [ -n "$grep_result" ]; then
|
|
variable_usage["$var"]=1
|
|
fi
|
|
done
|
|
done
|
|
|
|
# Print unused variables count and list
|
|
unused_count=0
|
|
unused_variables=()
|
|
|
|
for var in "${variables[@]}"; do
|
|
if [ -z "${variable_usage[$var]}" ]; then
|
|
unused_variables+=("$var")
|
|
((unused_count++))
|
|
fi
|
|
done
|
|
|
|
# Autofix: Remove lines containing unused variables from the source file
|
|
if [ "$AUTOFIX" == "true" ] && [ "$unused_count" -gt 0 ]; then
|
|
echo "Autofixing: Removing lines containing unused variables from $robot_variable_file"
|
|
for unused_var in "${unused_variables[@]}"; do
|
|
sed -i "/^$unused_var=/d" "$robot_variable_file"
|
|
done
|
|
fi
|
|
|
|
# Print messages for each file
|
|
if [ "$unused_count" -eq 0 ]; then
|
|
echo "No unused variables found in $robot_variable_file."
|
|
else
|
|
echo "Found $unused_count defined variables not used in $robot_variable_file:"
|
|
for unused_var in "${unused_variables[@]}"; do
|
|
echo "$unused_var"
|
|
done
|
|
fi
|
|
done
|