diff options
author | Sebastiano Barezzi <barezzisebastiano@gmail.com> | 2022-05-28 15:13:49 +0200 |
---|---|---|
committer | LuK1337 <priv.luk@gmail.com> | 2022-06-09 17:58:21 +0200 |
commit | 58851a06e5984e69e6219388c7e18bdd31cd44a5 (patch) | |
tree | cb760439b81ba1f544769172a9a52d7c4affddb9 | |
parent | 7ae0b6f6ecd46cfb11b08bf0ba9ad08d716fe9fc (diff) |
extract_utils: Add a script to reorder blobs list
Change-Id: I76e85e07f5a562b7e2a3683da264ccebff5c3613
-rwxr-xr-x | sort-blobs-list.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/sort-blobs-list.py b/sort-blobs-list.py new file mode 100755 index 0000000..a0581d4 --- /dev/null +++ b/sort-blobs-list.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2021-2022 The LineageOS Project +# +# SPDX-License-Identifier: Apache-2.0 +# + +import re, sys +from functools import cmp_to_key +from locale import LC_ALL, setlocale, strcoll +from pathlib import Path + + +def strcoll_extract_utils(string1: str, string2: str) -> int: + # Skip logic if one of the string if empty + if not string1 or not string2: + return strcoll(string1, string2) + + # Remove '-' from strings if there, + # it is used to indicate a build target + string1 = re.sub("^-", "", string1) + string2 = re.sub("^-", "", string2) + + # If no directories, compare normally + if "/" not in string1 and "/" not in string2: + return strcoll(string1, string2) + + string1_dir = string1.rsplit("/", 1)[0] + "/" + string2_dir = string2.rsplit("/", 1)[0] + "/" + if string1_dir == string2_dir: + # Same directory, compare normally + return strcoll(string1, string2) + + if string1_dir.startswith(string2_dir): + # First string dir is a subdirectory of the second one, + # return string1 > string2 + return -1 + + if string2_dir.startswith(string1_dir): + # Second string dir is a subdirectory of the first one, + # return string2 > string1 + return 1 + + # Compare normally + return strcoll(string1, string2) + + +if __name__ == "__main__": + setlocale(LC_ALL, "C") + + for file in sys.argv[1:] or ["proprietary-files.txt"]: + if not Path(file).is_file(): + print(f"File {file} not found") + continue + + with open(file, "r") as f: + sections = f.read().split("\n\n") + + ordered_sections = [] + for section in sections: + section_list = [line.strip() for line in section.splitlines()] + section_list.sort(key=cmp_to_key(strcoll_extract_utils)) + ordered_sections.append("\n".join(section_list)) + + with open(file, "w") as f: + f.write("\n\n".join(ordered_sections).strip() + "\n") |