From 32bd86c791cf3c72cd2f55d337267eed2000b86b Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 4 Dec 2025 21:16:47 +0100 Subject: [PATCH 1/2] Replace the Python sorting script with a simpler shell version --- sort-licenses.py | 69 ------------------------------------------------ sort-licenses.sh | 20 ++++++++++++++ 2 files changed, 20 insertions(+), 69 deletions(-) delete mode 100755 sort-licenses.py create mode 100755 sort-licenses.sh diff --git a/sort-licenses.py b/sort-licenses.py deleted file mode 100755 index a66db1f..0000000 --- a/sort-licenses.py +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/env python3 - -# SPDX-FileCopyrightText: 2023 Double Open Oy -# SPDX-License-Identifier: CC0-1.0 - -import re -import argparse - -def sort_yaml_blocks(input_file, output_file): - with open(input_file, 'r') as f: - lines = f.readlines() - - prefix = [] - blocks = [] - block = [] - comments = [] - in_categorizations = False - - for line in lines: - if "categorizations:" in line: - in_categorizations = True - prefix.append(line) - continue - - if not in_categorizations: - prefix.append(line) - continue - - if re.match(r"\s*#", line): - comments.append(line) - elif re.match(r"\s*- id:", line): - if block: # Add non-empty block to blocks - blocks.append("".join(block)) - block = comments + [line] - comments = [] - else: - block.append(line) - - # Add the last block if it's non-empty - if block: - blocks.append("".join(block)) - - def sorting_key(x): - match = re.search(r'- id: "(.*)"', x) - if match: - return match.group(1) - else: - raise ValueError(f"Sorting key not found in block:\n{x}") - - try: - blocks.sort(key=sorting_key) - except ValueError as e: - print(f"Exception while sorting: {e}") - - with open(output_file, 'w') as f: - f.writelines(prefix) - f.writelines(blocks) - -def main(): - parser = argparse.ArgumentParser(description="Sort licenses based on 'id' field within 'categorizations'.") - parser.add_argument('input_file', type=str, help='Input YAML file, usually license-classifications.yml') - parser.add_argument('output_file', type=str, help='Output YAML file after sorting. Once verified, you can replace the current license-classifications.yml with this file.') - - args = parser.parse_args() - - sort_yaml_blocks(args.input_file, args.output_file) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/sort-licenses.sh b/sort-licenses.sh new file mode 100755 index 0000000..8b8ea62 --- /dev/null +++ b/sort-licenses.sh @@ -0,0 +1,20 @@ +#!/bin/env bash + +# SPDX-FileCopyrightText: 2025 Double Open Oy +# SPDX-License-Identifier: CC0-1.0 + +# Avoid multi-line strings being unwrapped by `yq`. +# This works around https://github.com/mikefarah/yq/issues/439. +sed -i 's,>-,|,g' license-classifications.yml + +# Replace empty lines with marker comments. +# This works around https://github.com/mikefarah/yq/issues/515. +sed -i 's,^$,#EMPTY_LINE,g' license-classifications.yml + +yq -i '.categorizations |= sort_by(.id)' license-classifications.yml + +# Replace marker comments (with potential indentation added by `yq`) with empty lines. +sed -i 's, *#EMPTY_LINE,,g' license-classifications.yml + +# Restore original folded blocks with stripped newlines. +sed -i 's,|,>-,g' license-classifications.yml From 7e25e0f3bbcabde639cfdd95abe6636ef23b4f27 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 4 Dec 2025 21:23:36 +0100 Subject: [PATCH 2/2] Add a check for licenses to be sorted --- .github/workflows/check.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 40cdda6..d1d96bd 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -37,6 +37,17 @@ jobs: - name: Lint YAML files run: yamllint license-classifications.yml + Sort: + runs-on: ubuntu-latest + + steps: + - name: Clone the repository + uses: actions/checkout@v4 + - name: Sort YAML files + run: | + ./sort-licenses.sh + git diff --quiet + REUSE: runs-on: ubuntu-latest