scripts: fix lint errors

utils: lint

helium_version: fix lint errors

name_substitution: fix lint errors

devutils: lint
This commit is contained in:
jj
2025-07-27 20:34:47 +00:00
parent 8013b3af73
commit 7b231012ef
6 changed files with 102 additions and 45 deletions

View File

@@ -7,7 +7,6 @@
# Copyright (c) 2019 The ungoogled-chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE.ungoogled_chromium file.
"""
Update binary pruning and domain substitution lists automatically.

View File

@@ -8,7 +8,6 @@
# Copyright (c) 2019 The ungoogled-chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE.ungoogled_chromium file.
"""
Utility to ease the updating of platform patches against ungoogled-chromium's patches
"""

View File

@@ -8,7 +8,6 @@
# Copyright (c) 2019 The ungoogled-chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE.ungoogled_chromium file.
"""
Substitute domain names in the source tree with blockable strings.
"""

View File

@@ -1,21 +1,42 @@
#!/usr/bin/env python3
# Copyright 2025 The Helium Authors
# You can use, redistribute, and/or modify this source code under
# the terms of the GPL-3.0 license that can be found in the LICENSE file.
"""
Script to generate Helium version numbers
and inject them into the Chromium build tree.
"""
import argparse
from pathlib import Path
CHROME_VERSION_BASE = 136
def get_version_part(path):
with open(path, "r") as file:
"""
Gets the (first) digit representing a part of
the version from a particular file.
"""
with open(path, "r", encoding="utf-8") as file:
return int(file.readline().split(".")[0].strip())
def append_version(file, name, version):
"""Appends a version part to the chromium VERSION file"""
file.write(f"{name}={version}\n")
def check_existing_version(path):
with open(path, "r") as f:
if "HELIUM" in f.read():
raise Exception("file already contains helium versioning")
"""Verifies that the version has not yet been added to the build tree"""
with open(path, "r", encoding="utf-8") as file:
if "HELIUM" in file.read():
raise ValueError("file already contains helium versioning")
def parse_args():
"""Argument parsing"""
parser = argparse.ArgumentParser()
parser.add_argument("--tree", type=Path, required=True)
parser.add_argument("--platform-tree", type=Path, required=True)
@@ -24,21 +45,25 @@ def parse_args():
args = parser.parse_args()
return (args.tree, args.platform_tree, args.chromium_tree, args.print)
def get_version_parts(tree, platform_tree):
"""Compiles all the version parts into the full version"""
version_paths = {
"HELIUM_MAJOR": tree / "version.txt",
"HELIUM_MINOR": tree / "chromium_version.txt",
"HELIUM_PATCH": tree / "revision.txt",
"HELIUM_PLATFORM": platform_tree / "revision.txt",
}
version_parts = {}
for name, path in version_paths.items():
delta = 0 if name != "HELIUM_MINOR" else -CHROME_VERSION_BASE
version_parts[name] = get_version_part(path) + delta
return version_parts
def main():
"""CLI entrypoint"""
tree, platform_tree, chromium_tree, should_print = parse_args()
version_parts = get_version_parts(tree, platform_tree)
@@ -48,9 +73,10 @@ def main():
else:
chrome_version_path = chromium_tree / "chrome/VERSION"
check_existing_version(chrome_version_path)
with open(chrome_version_path, "a") as f:
with open(chrome_version_path, "a", encoding="utf-8") as file:
for name, version in version_parts.items():
append_version(f, name, version)
append_version(file, name, version)
if __name__ == "__main__":
main()

View File

@@ -1,7 +1,9 @@
#!/usr/bin/env python3
# Copyright 2025 The Helium Authors
# You can use, redistribute, and/or modify this source code under
# the terms of the GPL-3.0 license that can be found in the LICENSE file.
"""Script to replace instances of Chrome/Chromium with Helium"""
from pathlib import Path
import argparse
@@ -12,8 +14,8 @@ import re
REPLACEMENT_REGEXES_STR = [
# stuff we don't want to replace
(r'(\w+) Root Program', r'\1_unreplace Root Program'),
(r'(\w+) Web( S|s)tore', r'\1_unreplace Web Store'),
(r'(\w+) Root Program', r'\1_unreplace Root Program'),
(r'(\w+) Web( S|s)tore', r'\1_unreplace Web Store'),
(r'(\w+) Remote Desktop', r'\1_unreplace Remote Desktop'),
(r'("BEGIN_LINK_CHROMIUM")(.*?Chromium)(.*?<ph name="END_LINK_CHROMIUM")', r'\1\2_unreplace\3'),
@@ -25,19 +27,19 @@ REPLACEMENT_REGEXES_STR = [
(r'_unreplace', r'')
]
REPLACEMENT_REGEXES = list(
map(
lambda line: (re.compile(line[0]), line[1]),
REPLACEMENT_REGEXES_STR
)
)
REPLACEMENT_REGEXES = list(map(lambda line: (re.compile(line[0]), line[1]),
REPLACEMENT_REGEXES_STR))
def replace(str):
def replace(text):
"""Replaces instances of Chrom(e | ium) with Helium, where desired"""
for regex, replacement in REPLACEMENT_REGEXES:
str = re.sub(regex, replacement, str)
return str
text = re.sub(regex, replacement, text)
return text
def replacement_sanity():
"""Sanity check to ensure replacement regexes are working as intended"""
before_after = [
('Chrome Root Program', 'Chrome Root Program'),
('Chrome Web Store', 'Chrome Web Store'),
@@ -49,10 +51,12 @@ def replacement_sanity():
('Chromium', 'Helium'),
]
for a, b in before_after:
assert(replace(a) == b)
for source, expected in before_after:
assert replace(source) == expected
def parse_args():
"""CLI argument parsing logic"""
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--sub', action='store_true')
@@ -63,11 +67,24 @@ def parse_args():
args = parser.parse_args()
if args.unsub and not args.backup_path:
raise Exception("backup_path is missing, but unsub was specified")
raise ValueError("backup_path is missing, but unsub was specified")
return args
def is_substitutable(filename):
"""Determines whether a file should be name-substituted"""
if filename.startswith('.'):
return False
return filename.split('.')[-1].lower() in ['xtb', 'grd', 'grdp']
def get_substitutable_files(tree):
"""
Finds all candidates for substitution, which are source
string files (.grd*), or localization files (.xtb).
"""
out = tree / 'out'
for root, _, files in os.walk(tree):
@@ -75,54 +92,57 @@ def get_substitutable_files(tree):
if out in root.parents:
continue
yield from map(
lambda filename: root / filename,
filter(
lambda filename: not filename.startswith('.') and (
filename.endswith('.xtb') or filename.endswith('.grd') or filename.endswith('.grdp')
),
files
)
)
yield from map(lambda filename, root=root: root / filename, filter(is_substitutable, files))
async def substitute_file(tree, path, tarball = None):
async def substitute_file(tree, path, tarball=None):
"""
Replaces strings in a particular file,
if there is anything to replace.
"""
arcname = str(path.relative_to(tree))
text = None
with open(path, 'r', encoding='utf-8') as f:
text = f.read()
with open(path, 'r', encoding='utf-8') as file:
text = file.read()
replaced = replace(text)
if text != replaced:
print(f"Replaced strings in {arcname}")
if tarball:
tarball.add(path, arcname=arcname, recursive=False)
with open(path, 'w', encoding='utf-8') as f:
f.write(replaced)
with open(path, 'w', encoding='utf-8') as file:
file.write(replaced)
def do_unsubstitution(tree, tarpath):
"""Reverts name substitutions from the backup tarball"""
with tarfile.open(str(tarpath), 'r:gz') as tar:
tar.extractall(path=tree, filter='fully_trusted')
tarpath.unlink()
async def do_substitution(tree, tarpath):
with tarfile.open(str(tarpath), 'w:gz') if tarpath else open(os.devnull, 'w') as cache_tar:
"""Performs name substitutions on all candidate files"""
with tarfile.open(str(tarpath), 'w:gz') if tarpath else open(os.devnull, 'w',
encoding="utf-8") as cache_tar:
pending_substitutions = map(
lambda path: substitute_file(tree, path, cache_tar if tarpath else None),
get_substitutable_files(tree)
)
get_substitutable_files(tree))
await asyncio.gather(*pending_substitutions)
async def main():
"""CLI entrypoint"""
replacement_sanity()
args = parse_args()
if not (args.t / "OWNERS").exists():
raise Exception("wrong src directory")
raise ValueError("wrong src directory")
if args.sub:
if args.backup_path is not None and args.backup_path.exists():
raise Exception("unsub tarball already exists, aborting")
raise FileExistsError("unsub tarball already exists, aborting")
await do_substitution(args.t, args.backup_path)
elif args.unsub and args.backup_path:
do_unsubstitution(args.t, args.backup_path)

View File

@@ -2,11 +2,20 @@
# You can use, redistribute, and/or modify this source code under
# the terms of the GPL-3.0 license that can be found in the LICENSE file.
"""
Replaces resources (such as icons) with Helium branding.
"""
import os
import shutil
import sys
def copy_resources(resource_list, resource_dir, chromium_dir):
"""
Handles copying resources from the source tree into the build
tree based on a resources list.
"""
if not os.path.isfile(resource_list):
print(f"Resource list '{resource_list}' does not exist, skipping")
return
@@ -17,7 +26,7 @@ def copy_resources(resource_list, resource_dir, chromium_dir):
print(f"Chromium dir '{chromium_dir}' does not exist.")
sys.exit(1)
with open(resource_list, 'r') as file:
with open(resource_list, 'r', encoding='utf-8') as file:
for line_number, line in enumerate(file, start=1):
line = line.strip()
if not line or line.startswith('#'):
@@ -45,9 +54,13 @@ def copy_resources(resource_list, resource_dir, chromium_dir):
except Exception as e:
print(f"Error copying '{source}' to '{dest}': {e}")
def main():
if len(sys.argv) != 4:
print("Usage: python3 replace_resources.py <helium_resources.txt> <resources_dir> <chromium_src_dir>")
print(
"Usage: python3 replace_resources.py <helium_resources.txt> " \
"<resources_dir> <chromium_src_dir>"
)
sys.exit(1)
resource_list = sys.argv[1]
@@ -56,5 +69,6 @@ def main():
copy_resources(resource_list, resource_dir, chromium_dir)
if __name__ == "__main__":
main()