|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # |
3 | | -# SPDX-FileCopyrightText: 2021-2024 The LineageOS Project |
| 3 | +# SPDX-FileCopyrightText: 2021-2025 The LineageOS Project |
4 | 4 | # SPDX-License-Identifier: Apache-2.0 |
5 | 5 | # |
6 | 6 |
|
7 | 7 | import re |
8 | 8 | from argparse import ArgumentParser |
9 | 9 | from functools import cmp_to_key |
| 10 | +from itertools import groupby |
10 | 11 | from locale import LC_ALL, setlocale, strcoll |
11 | 12 | from pathlib import Path |
12 | 13 |
|
13 | 14 |
|
| 15 | +def is_blob(line: str) -> bool: |
| 16 | + line = line.strip() |
| 17 | + return line and not line.startswith('#') |
| 18 | + |
| 19 | + |
14 | 20 | def get_source_file_name(line: str) -> str: |
15 | 21 | # Remove '-' from strings if there, |
16 | 22 | # it is used to indicate a build target |
@@ -88,13 +94,14 @@ def strcoll_extract_utils( |
88 | 94 | continue |
89 | 95 |
|
90 | 96 | with open(file, 'r', encoding='utf-8') as f: |
91 | | - sections = f.read().split('\n\n') |
| 97 | + sections = groupby(f.readlines(), is_blob) |
92 | 98 |
|
93 | 99 | ordered_sections = [] |
94 | | - for section in sections: |
95 | | - section_list = [line.strip() for line in section.splitlines()] |
96 | | - section_list.sort(key=sort_key) |
97 | | - ordered_sections.append('\n'.join(section_list)) |
| 100 | + for sort, section in sections: |
| 101 | + if sort: |
| 102 | + ordered_sections.append(''.join(sorted(section, key=sort_key))) |
| 103 | + else: |
| 104 | + ordered_sections.append(''.join(section)) |
98 | 105 |
|
99 | 106 | with open(file, 'w', encoding='utf-8') as f: |
100 | | - f.write('\n\n'.join(ordered_sections).strip() + '\n') |
| 107 | + f.write(''.join(ordered_sections)) |
0 commit comments