2021-02-08 17:53:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-11-03 18:19:40 +00:00
|
|
|
# Copyright 2022 Oliver Smith
|
2021-02-08 17:53:21 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2022-11-03 18:19:40 +00:00
|
|
|
# Various checks on source= in the APKBUILDs
|
2021-02-08 17:53:21 +00:00
|
|
|
|
|
|
|
import glob
|
2022-11-03 18:50:09 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2021-02-08 17:53:21 +00:00
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import add_pmbootstrap_to_import_path
|
|
|
|
import pmb.parse
|
|
|
|
import pmb.parse._apkbuild
|
|
|
|
import pmb.parse.apkindex
|
|
|
|
import pmb.helpers.repo
|
|
|
|
|
|
|
|
|
|
|
|
def parse_source_from_checksums(args, apkbuild_path):
|
|
|
|
"""
|
|
|
|
Read the APKBUILD file and parse source files from the checksums at the
|
|
|
|
bottom. This has always the same format, even if $source is built with hard
|
|
|
|
to parse shell code (like in postmarketos-base).
|
|
|
|
|
|
|
|
:param apkbuild_path: full path to the APKBUILD
|
2022-11-03 18:19:40 +00:00
|
|
|
:returns: dict of parsed "source" filenames and checksums, e.g.:
|
|
|
|
{"first.patch": "b4dc4f3…",
|
|
|
|
"second.patch": "b4dc4f3…"}
|
2021-02-08 17:53:21 +00:00
|
|
|
"""
|
|
|
|
start = 'sha512sums="'
|
|
|
|
in_block = False
|
2022-11-03 18:19:40 +00:00
|
|
|
ret = {}
|
2021-02-08 17:53:21 +00:00
|
|
|
|
|
|
|
with open(apkbuild_path, encoding="utf-8") as handle:
|
|
|
|
for line in handle.readlines():
|
|
|
|
# Find start
|
|
|
|
if not in_block:
|
|
|
|
if line.startswith(start):
|
|
|
|
in_block = True
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# sha512sums may have lines without checksums:
|
|
|
|
# https://gitlab.alpinelinux.org/alpine/abuild/-/merge_requests/73
|
|
|
|
if " " not in line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2022-11-03 18:19:40 +00:00
|
|
|
checksum, filename = line.rstrip().split(" ", 2)
|
2021-02-08 17:53:21 +00:00
|
|
|
except ValueError:
|
|
|
|
raise ValueError("Failed to parse checksums. Try to delete the"
|
|
|
|
" checksums and generate them again with"
|
|
|
|
f" 'pmbootstrap checksum': {apkbuild_path}")
|
|
|
|
|
2022-11-03 18:19:40 +00:00
|
|
|
# Cut off 'sha512sums="' if the first checksum is in that line
|
|
|
|
if checksum.startswith(start):
|
|
|
|
checksum = checksum[len(start):]
|
|
|
|
|
2021-02-08 17:53:21 +00:00
|
|
|
# Find end
|
|
|
|
if filename.endswith('"'):
|
2022-11-03 18:19:40 +00:00
|
|
|
filename = filename[:-1]
|
2021-02-08 17:53:21 +00:00
|
|
|
|
2022-11-03 18:19:40 +00:00
|
|
|
ret[filename] = checksum
|
2021-02-08 17:53:21 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def test_aports_unreferenced_files(args):
|
|
|
|
"""
|
|
|
|
Raise an error if an unreferenced file is found
|
|
|
|
"""
|
|
|
|
for apkbuild_path in glob.iglob(args.aports + "/**/APKBUILD", recursive=True):
|
2022-10-15 21:25:54 +00:00
|
|
|
# pmbootstrap parser has some issues with complicated APKBUILDs, skip those.
|
|
|
|
if apkbuild_path.startswith(args.aports + "/cross/"):
|
|
|
|
continue
|
|
|
|
|
2021-11-27 14:08:36 +00:00
|
|
|
apkbuild = pmb.parse.apkbuild(apkbuild_path)
|
2021-02-08 17:53:21 +00:00
|
|
|
sources_chk = parse_source_from_checksums(args, apkbuild_path)
|
|
|
|
|
2022-09-11 11:44:56 +00:00
|
|
|
# Collect files from subpackages
|
2021-02-08 17:53:21 +00:00
|
|
|
subpackage_installs = []
|
2022-09-11 11:44:56 +00:00
|
|
|
subpackage_triggers = []
|
2021-02-08 17:53:21 +00:00
|
|
|
if apkbuild["subpackages"]:
|
|
|
|
for subpackage in apkbuild["subpackages"].values():
|
|
|
|
if not subpackage:
|
|
|
|
continue
|
2022-09-11 11:31:46 +00:00
|
|
|
subpackage_installs += subpackage.get("install", [])
|
2022-09-11 11:44:56 +00:00
|
|
|
subpackage_triggers += subpackage.get("triggers", [])
|
2021-02-08 17:53:21 +00:00
|
|
|
|
|
|
|
# Collect trigger files
|
|
|
|
trigger_sources = []
|
2022-09-11 11:44:56 +00:00
|
|
|
for trigger in apkbuild["triggers"] + subpackage_triggers:
|
|
|
|
trigger_sources.append(trigger.split("=")[0])
|
2021-02-08 17:53:21 +00:00
|
|
|
|
|
|
|
dirname = os.path.dirname(apkbuild_path)
|
|
|
|
for file in glob.iglob(dirname + "/**", recursive=True):
|
|
|
|
rel_file_path = os.path.relpath(file, dirname)
|
|
|
|
# Skip APKBUILDs and directories
|
|
|
|
if rel_file_path == "APKBUILD" or os.path.isdir(file):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.basename(rel_file_path) not in sources_chk \
|
|
|
|
and rel_file_path not in apkbuild["install"] \
|
|
|
|
and rel_file_path not in subpackage_installs \
|
|
|
|
and rel_file_path not in trigger_sources:
|
|
|
|
raise RuntimeError(f"{apkbuild_path}: found unreferenced file: {rel_file_path}")
|
2022-11-03 18:50:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_distfiles_conflict(args):
|
|
|
|
"""
|
|
|
|
Make sure that each filename mentioned in any source= of any APKBUILD
|
|
|
|
always has the same checksum. This is important because apk caches
|
|
|
|
downloaded source files in a flat distfiles directory. So if two APKBUILDs
|
|
|
|
both download a file with the same filename but different checksum, and the
|
|
|
|
user builds both after each other, abuild will fail on the second build
|
|
|
|
with a checksum error.
|
|
|
|
"""
|
|
|
|
source_all = {}
|
|
|
|
for apkbuild_path in glob.iglob(f"{args.aports}/**/APKBUILD", recursive=True):
|
|
|
|
source = parse_source_from_checksums(args, apkbuild_path)
|
|
|
|
dir_path = os.path.dirname(apkbuild_path)
|
|
|
|
apkbuild_rel = os.path.relpath(apkbuild_path, args.aports)
|
|
|
|
for filename, checksum in source.items():
|
|
|
|
# Files bundled with the APKBUILD don't get copied to the distfiles
|
|
|
|
# cache, so not relevant for this check. Use glob.glob here and not
|
|
|
|
# iglob, because we don't want an iterator.
|
|
|
|
if glob.glob(f"{dir_path}/**/{filename}", recursive=True):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# First time seeing this file
|
|
|
|
if filename not in source_all:
|
|
|
|
source_all[filename] = {"checksum": checksum,
|
|
|
|
"apkbuild_rel": apkbuild_rel}
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Saw this file already with same checksum
|
|
|
|
if checksum == source_all[filename]["checksum"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Saw this file already with different checksum
|
|
|
|
logging.error("")
|
|
|
|
logging.error(f"ERROR: the source file '{filename}' has different"
|
|
|
|
" checksums in the following files:")
|
|
|
|
logging.error(f"- {source_all[filename]['apkbuild_rel']}:")
|
|
|
|
logging.error(f" {source_all[filename]['checksum']}")
|
|
|
|
logging.error(f"- {apkbuild_rel}:")
|
|
|
|
logging.error(f" {checksum}")
|
|
|
|
logging.error("")
|
|
|
|
logging.error("Fix this by setting a different target filename in"
|
|
|
|
" the package you modified:")
|
|
|
|
logging.error("https://wiki.alpinelinux.org/wiki/APKBUILD_Reference#source")
|
|
|
|
logging.error("")
|
|
|
|
raise RuntimeError(f"Conflict with source file '{filename}'")
|