2018-08-30 22:39:24 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-01-02 08:29:47 +00:00
|
|
|
# Copyright 2019 Oliver Smith
|
2018-08-30 22:39:24 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def get_pmaports_dir():
|
|
|
|
return os.path.realpath(os.path.join(os.path.dirname(__file__) + "/.."))
|
|
|
|
|
|
|
|
|
2018-12-13 06:32:17 +00:00
|
|
|
def run_git(parameters, check=True):
|
2018-12-13 06:21:28 +00:00
|
|
|
""" Run git in the pmaports dir and return the output """
|
2018-08-30 22:39:24 +00:00
|
|
|
cmd = ["git", "-C", get_pmaports_dir()] + parameters
|
2018-12-13 06:32:17 +00:00
|
|
|
try:
|
|
|
|
return subprocess.check_output(cmd).decode()
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
if check:
|
|
|
|
raise
|
|
|
|
return None
|
2018-08-30 22:39:24 +00:00
|
|
|
|
|
|
|
|
2019-02-09 21:22:41 +00:00
|
|
|
def commit_message_has_string(needle):
|
|
|
|
return needle in run_git(["show", "-s", "--format=full", "HEAD"])
|
|
|
|
|
|
|
|
|
2018-08-30 22:39:24 +00:00
|
|
|
def run_pmbootstrap(parameters):
|
2018-12-13 06:21:28 +00:00
|
|
|
""" Run pmbootstrap with the pmaports dir as --aports """
|
2018-08-30 22:39:24 +00:00
|
|
|
cmd = ["pmbootstrap", "--aports", get_pmaports_dir()] + parameters
|
|
|
|
process = subprocess.Popen(cmd)
|
|
|
|
process.communicate()
|
|
|
|
if process.returncode != 0:
|
|
|
|
print("** Building failed")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def get_changed_files():
|
|
|
|
""" Get all changed files and print them, as well as the branch and the
|
|
|
|
commit that was used for the diff.
|
|
|
|
:returns: list of changed files
|
|
|
|
"""
|
2018-09-25 05:29:15 +00:00
|
|
|
commit_head = run_git(["rev-parse", "HEAD"])[:-1]
|
|
|
|
commit_upstream_master = run_git(["rev-parse", "upstream/master"])[:-1]
|
|
|
|
print("commit HEAD: " + commit_head)
|
|
|
|
print("commit upstream/master: " + commit_upstream_master)
|
|
|
|
|
|
|
|
# Check if we are latest upstream/master
|
|
|
|
if commit_head == commit_upstream_master:
|
2018-12-13 06:21:28 +00:00
|
|
|
# then compare with previous commit
|
|
|
|
commit = "HEAD~1"
|
2018-09-25 05:29:15 +00:00
|
|
|
else:
|
2018-12-13 06:21:28 +00:00
|
|
|
# otherwise compare with latest common ancestor
|
|
|
|
commit = run_git(["merge-base", "upstream/master", "HEAD"])[:-1]
|
2018-08-30 22:39:24 +00:00
|
|
|
print("comparing HEAD with: " + commit)
|
|
|
|
|
|
|
|
# Changed files
|
|
|
|
ret = run_git(["diff", "--name-only", commit, "HEAD"]).splitlines()
|
|
|
|
print("changed file(s):")
|
|
|
|
for file in ret:
|
2018-12-13 06:29:57 +00:00
|
|
|
message = " " + file
|
|
|
|
if not os.path.exists(file):
|
|
|
|
message += " (deleted)"
|
|
|
|
print(message)
|
2018-08-30 22:39:24 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2019-02-09 21:22:41 +00:00
|
|
|
def get_changed_packages_sanity_check(count):
|
|
|
|
if commit_message_has_string("[ci:ignore-count]"):
|
|
|
|
print("NOTE: package count sanity check skipped ([ci:ignore-count]).")
|
|
|
|
return
|
|
|
|
if count <= 10:
|
|
|
|
return
|
|
|
|
|
|
|
|
print()
|
|
|
|
print("ERROR: Too many packages have changed!")
|
|
|
|
print()
|
|
|
|
print("This is a sanity check, so we don't end up building packages that")
|
|
|
|
print("have not been modified. CI won't run for more than one hour")
|
|
|
|
print("anyway.")
|
|
|
|
print()
|
|
|
|
print("Your options:")
|
|
|
|
print("a) If you *did not* modify everything listed above, then rebase")
|
|
|
|
print(" your branch on the official postmarketOS/pmaports.git master")
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2018-08-30 22:39:24 +00:00
|
|
|
def get_changed_packages():
|
|
|
|
files = get_changed_files()
|
|
|
|
ret = set()
|
|
|
|
for file in files:
|
2018-12-13 06:29:57 +00:00
|
|
|
# Skip files:
|
|
|
|
# * in the root dir of pmaports (e.g. README.md)
|
|
|
|
# * path beginning with a dot (e.g. .gitlab-ci/)
|
|
|
|
# * non-existing files (deleted packages)
|
|
|
|
if "/" not in file or file.startswith(".") or not os.path.exists(file):
|
2018-08-30 22:39:24 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Add to the ret set (removes duplicated automatically)
|
|
|
|
ret.add(file.split("/")[1])
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def check_build(packages):
|
|
|
|
# Initialize build environment with less logging
|
|
|
|
run_pmbootstrap(["build_init"])
|
2019-04-14 02:11:01 +00:00
|
|
|
run_pmbootstrap(["--details-to-stdout", "build", "--strict", "--force"] +
|
|
|
|
list(packages))
|
2018-08-30 22:39:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-09-25 05:29:15 +00:00
|
|
|
# Add a remote pointing to postmarketOS/pmaports for later
|
|
|
|
run_git(["remote", "add", "upstream",
|
2018-12-13 06:32:17 +00:00
|
|
|
"https://gitlab.com/postmarketOS/pmaports.git"], False)
|
2018-09-26 05:57:29 +00:00
|
|
|
run_git(["fetch", "-q", "upstream"])
|
2018-08-30 22:39:24 +00:00
|
|
|
|
2019-02-09 21:22:41 +00:00
|
|
|
# Get and print modified packages
|
2018-09-25 05:29:15 +00:00
|
|
|
packages = get_changed_packages()
|
2019-02-09 21:22:41 +00:00
|
|
|
|
|
|
|
# Stop here if desired
|
|
|
|
if commit_message_has_string("[ci:skip-build]"):
|
|
|
|
print("WARNING: not building changed packages ([ci:skip-build])!")
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
# Build changed packages
|
|
|
|
get_changed_packages_sanity_check(len(packages))
|
2018-08-30 22:39:24 +00:00
|
|
|
if len(packages) == 0:
|
|
|
|
print("no aports changed in this branch")
|
|
|
|
else:
|
|
|
|
print("building in strict mode: " + ", ".join(packages))
|
|
|
|
check_build(packages)
|