2019-05-19 20:18:12 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-01-07 23:16:08 +00:00
|
|
|
# Copyright 2021 Oliver Smith
|
2019-05-19 20:18:12 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
# Various functions used in CI scripts
|
|
|
|
|
2020-06-16 17:13:33 +00:00
|
|
|
import configparser
|
2019-05-19 20:18:12 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2020-06-16 17:12:52 +00:00
|
|
|
cache = {}
|
|
|
|
|
|
|
|
|
2019-05-19 20:18:12 +00:00
|
|
|
def get_pmaports_dir():
|
2020-06-16 17:12:52 +00:00
|
|
|
global cache
|
|
|
|
if "pmaports_dir" in cache:
|
|
|
|
return cache["pmaports_dir"]
|
2022-10-30 15:48:16 +00:00
|
|
|
ret = os.path.realpath(os.path.join(os.path.dirname(__file__) + "/../.."))
|
2020-06-16 17:12:52 +00:00
|
|
|
cache["pmaports_dir"] = ret
|
|
|
|
return ret
|
2019-05-19 20:18:12 +00:00
|
|
|
|
|
|
|
|
2019-05-19 20:20:39 +00:00
|
|
|
def run_git(parameters, check=True, stderr=None):
|
2019-05-19 20:18:12 +00:00
|
|
|
""" Run git in the pmaports dir and return the output """
|
|
|
|
cmd = ["git", "-C", get_pmaports_dir()] + parameters
|
|
|
|
try:
|
2019-05-19 20:20:39 +00:00
|
|
|
return subprocess.check_output(cmd, stderr=stderr).decode()
|
2019-05-19 20:18:12 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
if check:
|
|
|
|
raise
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def add_upstream_git_remote():
|
|
|
|
""" Add a remote pointing to postmarketOS/pmaports. """
|
|
|
|
run_git(["remote", "add", "upstream",
|
|
|
|
"https://gitlab.com/postmarketOS/pmaports.git"], False)
|
|
|
|
run_git(["fetch", "-q", "upstream"])
|
|
|
|
|
|
|
|
|
|
|
|
def commit_message_has_string(needle):
|
|
|
|
return needle in run_git(["show", "-s", "--format=full", "HEAD"])
|
|
|
|
|
|
|
|
|
2020-04-06 21:19:09 +00:00
|
|
|
def run_pmbootstrap(parameters, output_return=False):
|
2019-05-19 20:18:12 +00:00
|
|
|
""" Run pmbootstrap with the pmaports dir as --aports """
|
|
|
|
cmd = ["pmbootstrap", "--aports", get_pmaports_dir()] + parameters
|
2020-04-06 21:19:09 +00:00
|
|
|
stdout = subprocess.PIPE if output_return else None
|
|
|
|
result = subprocess.run(cmd, stdout=stdout, universal_newlines=True)
|
|
|
|
result.check_returncode()
|
|
|
|
if output_return:
|
|
|
|
return result.stdout
|
2019-05-19 20:18:12 +00:00
|
|
|
|
|
|
|
|
2020-06-16 17:13:33 +00:00
|
|
|
def get_upstream_branch():
|
|
|
|
""" Use pmaports.cfg from current branch (e.g. "v20.05_fix-ci") and
|
|
|
|
channels.cfg from master to retrieve the upstream branch.
|
|
|
|
|
|
|
|
:returns: branch name, e.g. "v20.05" """
|
|
|
|
global cache
|
|
|
|
if "upstream_branch" in cache:
|
|
|
|
return cache["upstream_branch"]
|
|
|
|
|
|
|
|
# Get channel (e.g. "stable") from pmaports.cfg
|
|
|
|
# https://postmarketos.org/pmaports.cfg
|
|
|
|
pmaports_dir = get_pmaports_dir()
|
|
|
|
pmaports_cfg = configparser.ConfigParser()
|
|
|
|
pmaports_cfg.read(f"{pmaports_dir}/pmaports.cfg")
|
|
|
|
channel = pmaports_cfg["pmaports"]["channel"]
|
|
|
|
|
|
|
|
# Get branch_pmaports (e.g. "v20.05") from channels.cfg
|
|
|
|
# https://postmarketos.org/channels.cfg
|
|
|
|
channels_cfg_str = run_git(["show", "upstream/master:channels.cfg"])
|
|
|
|
channels_cfg = configparser.ConfigParser()
|
|
|
|
channels_cfg.read_string(channels_cfg_str)
|
|
|
|
assert channel in channels_cfg, \
|
|
|
|
f"Channel '{channel}' from pmaports.cfg in your branch is unknown." \
|
|
|
|
" This appears to be an old branch, consider recreating your change" \
|
|
|
|
" on top of master."
|
|
|
|
|
|
|
|
ret = channels_cfg[channel]["branch_pmaports"]
|
|
|
|
cache["upstream_branch"] = ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2020-03-14 11:17:50 +00:00
|
|
|
def get_changed_files(removed=True):
|
2019-05-19 20:18:12 +00:00
|
|
|
""" Get all changed files and print them, as well as the branch and the
|
|
|
|
commit that was used for the diff.
|
2020-03-14 11:17:50 +00:00
|
|
|
:param removed: also return removed files (default: True)
|
|
|
|
:returns: set of changed files
|
2019-05-19 20:18:12 +00:00
|
|
|
"""
|
2020-06-16 17:13:33 +00:00
|
|
|
branch_upstream = f"upstream/{get_upstream_branch()}"
|
2019-05-19 20:18:12 +00:00
|
|
|
commit_head = run_git(["rev-parse", "HEAD"])[:-1]
|
2020-06-16 17:13:33 +00:00
|
|
|
commit_upstream = run_git(["rev-parse", branch_upstream])[:-1]
|
2019-05-19 20:18:12 +00:00
|
|
|
print("commit HEAD: " + commit_head)
|
2020-06-16 17:13:33 +00:00
|
|
|
print(f"commit {branch_upstream}: f{commit_upstream}")
|
2019-05-19 20:18:12 +00:00
|
|
|
|
2020-06-16 17:13:33 +00:00
|
|
|
# Check if we are HEAD on the upstream branch
|
|
|
|
if commit_head == commit_upstream:
|
2019-05-19 20:18:12 +00:00
|
|
|
# then compare with previous commit
|
|
|
|
commit = "HEAD~1"
|
|
|
|
else:
|
|
|
|
# otherwise compare with latest common ancestor
|
2020-06-16 17:13:33 +00:00
|
|
|
commit = run_git(["merge-base", branch_upstream, "HEAD"])[:-1]
|
2019-05-19 20:18:12 +00:00
|
|
|
print("comparing HEAD with: " + commit)
|
|
|
|
|
|
|
|
# Changed files
|
2020-03-14 11:17:50 +00:00
|
|
|
ret = set()
|
2019-05-19 20:18:12 +00:00
|
|
|
print("changed file(s):")
|
2020-03-14 11:17:50 +00:00
|
|
|
for file in run_git(["diff", "--name-only", commit, "HEAD"]).splitlines():
|
2019-05-19 20:18:12 +00:00
|
|
|
message = " " + file
|
|
|
|
if not os.path.exists(file):
|
|
|
|
message += " (deleted)"
|
2020-03-14 11:17:50 +00:00
|
|
|
if removed:
|
|
|
|
ret.add(file)
|
|
|
|
else:
|
|
|
|
ret.add(file)
|
2019-05-19 20:18:12 +00:00
|
|
|
print(message)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def get_changed_packages_sanity_check(count):
|
2019-05-30 22:33:03 +00:00
|
|
|
for mark in ["[ci:ignore-count]", "[ci:skip-build]"]:
|
|
|
|
if commit_message_has_string(mark):
|
|
|
|
print("NOTE: package count sanity check skipped (" + mark + ").")
|
|
|
|
return
|
2019-05-19 20:18:12 +00:00
|
|
|
if count <= 10:
|
|
|
|
return
|
|
|
|
|
2020-06-16 17:13:33 +00:00
|
|
|
branch = get_upstream_branch()
|
2019-05-19 20:18:12 +00:00
|
|
|
print()
|
|
|
|
print("ERROR: Too many packages have changed!")
|
|
|
|
print()
|
|
|
|
print("This is a sanity check, so we don't end up building packages that")
|
2023-05-30 07:12:52 +00:00
|
|
|
print("have not been modified. CI won't run for more than three hours")
|
2019-05-19 20:18:12 +00:00
|
|
|
print("anyway.")
|
|
|
|
print()
|
|
|
|
print("Your options:")
|
|
|
|
print("a) If you *did not* modify everything listed above, then rebase")
|
2020-06-16 17:13:33 +00:00
|
|
|
print(f" your branch on the official postmarketOS/pmaports.git {branch}")
|
2019-05-19 20:18:12 +00:00
|
|
|
print(" branch. Feel free to ask in the chat for help if you need any.")
|
|
|
|
print("b) If you *did* modify all these packages, and you assume that")
|
|
|
|
print(" they will build within one hour: skip this sanity check by")
|
|
|
|
print(" adding '[ci:ignore-count]' to the commit message (then force")
|
|
|
|
print(" push).")
|
|
|
|
print("c) If you *did* modify all these packages, and you are sure that")
|
|
|
|
print(" they won't build in time, please add '[ci:skip-build]' to the")
|
|
|
|
print(" commit message (then force push). Make sure that all packages")
|
|
|
|
print(" build with 'pmbootstrap build --strict'!")
|
|
|
|
print()
|
|
|
|
print("Thank you and sorry for the inconvenience.")
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2020-03-14 11:22:08 +00:00
|
|
|
def get_changed_packages():
|
2019-05-19 20:18:12 +00:00
|
|
|
ret = set()
|
2020-03-14 11:39:32 +00:00
|
|
|
for file in get_changed_files():
|
|
|
|
dirname, filename = os.path.split(file)
|
|
|
|
|
2019-05-19 20:18:12 +00:00
|
|
|
# Skip files:
|
|
|
|
# * in the root dir of pmaports (e.g. README.md)
|
2021-09-13 16:15:23 +00:00
|
|
|
# * path with a dot (e.g. .ci/, device/.shared-patches/)
|
2020-03-14 11:39:32 +00:00
|
|
|
if not dirname or file.startswith(".") or "/." in file:
|
2019-05-19 20:18:12 +00:00
|
|
|
continue
|
|
|
|
|
2020-03-14 11:39:32 +00:00
|
|
|
if filename != "APKBUILD":
|
|
|
|
# Walk up directories until we (eventually) find the package
|
|
|
|
# the file belongs to (could be in a subdirectory of a package)
|
|
|
|
while dirname and not os.path.exists(os.path.join(dirname, "APKBUILD")):
|
|
|
|
dirname = os.path.dirname(dirname)
|
|
|
|
|
|
|
|
# Unable to find APKBUILD the file belong to
|
|
|
|
if not dirname:
|
|
|
|
# ... maybe the package was deleted entirely?
|
|
|
|
if not os.path.exists(file):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Weird, file does not belong to any package?
|
|
|
|
# Here we just warn, there is an extra check
|
|
|
|
# to make sure that files are organized properly.
|
|
|
|
print(f"WARNING: Changed file {file} does not belong to any package")
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif not os.path.exists(file):
|
|
|
|
continue # APKBUILD was deleted
|
|
|
|
|
|
|
|
ret.add(os.path.basename(dirname))
|
|
|
|
|
2019-05-19 20:18:12 +00:00
|
|
|
return ret
|
2023-03-16 20:49:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_changed_kernels():
|
|
|
|
ret = []
|
|
|
|
for pkgname in get_changed_packages():
|
|
|
|
if pkgname.startswith("linux-"):
|
|
|
|
ret += [pkgname]
|
|
|
|
return ret
|