WIP: start ripping out args (MR 2252)

Cease merging pmbootstrap.cfg into args, implement a Context type to let
us pull globals out of thin air (as an intermediate workaround) and rip
args out of a lot of the codebase.

This is just a first pass, after this we can split all the state that
leaked over into Context into types with narrower scopes (like a
BuildContext(), etc).

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-05-25 03:59:04 +02:00 committed by Oliver Smith
parent bfea00e03a
commit 34dd9d42ba
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
129 changed files with 1393 additions and 1300 deletions

View file

@ -16,7 +16,7 @@ import pmb.helpers.pmaports
import pmb.helpers.run
def get_path(context: Context, name_repo):
def get_path(name_repo: str):
"""Get the path to the repository.
The path is either the default one in the work dir, or a user-specified one in args.
@ -24,8 +24,8 @@ def get_path(context: Context, name_repo):
:returns: full path to repository
"""
if name_repo == "pmaports":
return context.aports
return pmb.config.work / "cache_git" / name_repo
return get_context().config.aports
return get_context().config.work / "cache_git" / name_repo
def clone(name_repo):
@ -40,7 +40,7 @@ def clone(name_repo):
if name_repo not in pmb.config.git_repos:
raise ValueError("No git repository configured for " + name_repo)
path = get_path(get_context(), name_repo)
path = get_path(name_repo)
if not os.path.exists(path):
# Build git command
url = pmb.config.git_repos[name_repo][0]
@ -49,7 +49,7 @@ def clone(name_repo):
# Create parent dir and clone
logging.info("Clone git repository: " + url)
os.makedirs(pmb.config.work / "cache_git", exist_ok=True)
os.makedirs(get_context().config.work / "cache_git", exist_ok=True)
pmb.helpers.run.user(command, output="stdout")
# FETCH_HEAD does not exist after initial clone. Create it, so
@ -59,7 +59,7 @@ def clone(name_repo):
open(fetch_head, "w").close()
def rev_parse(path, revision="HEAD", extra_args: list = []):
def rev_parse(path: Path, revision="HEAD", extra_args: list = []):
"""Run "git rev-parse" in a specific repository dir.
:param path: to the git repository
@ -84,29 +84,29 @@ def can_fast_forward(path, branch_upstream, branch="HEAD"):
raise RuntimeError("Unexpected exit code from git: " + str(ret))
def clean_worktree(path):
def clean_worktree(path: Path):
"""Check if there are not any modified files in the git dir."""
command = ["git", "status", "--porcelain"]
return pmb.helpers.run.user_output(command, path) == ""
def get_upstream_remote(context: Context, name_repo):
def get_upstream_remote(aports: Path):
"""Find the remote, which matches the git URL from the config.
Usually "origin", but the user may have set up their git repository differently.
"""
name_repo = aports.parts[-1]
urls = pmb.config.git_repos[name_repo]
path = get_path(context, name_repo)
command = ["git", "remote", "-v"]
output = pmb.helpers.run.user_output(command, path)
output = pmb.helpers.run.user_output(command, aports)
for line in output.split("\n"):
if any(u in line for u in urls):
return line.split("\t", 1)[0]
raise RuntimeError("{}: could not find remote name for any URL '{}' in git"
" repository: {}".format(name_repo, urls, path))
" repository: {}".format(name_repo, urls, aports))
def parse_channels_cfg():
def parse_channels_cfg(aports: Path):
"""Parse channels.cfg from pmaports.git, origin/master branch.
Reference: https://postmarketos.org/channels.cfg
@ -122,14 +122,12 @@ def parse_channels_cfg():
cache_key = "pmb.helpers.git.parse_channels_cfg"
if pmb.helpers.other.cache[cache_key]:
return pmb.helpers.other.cache[cache_key]
context = get_context()
# Read with configparser
cfg = configparser.ConfigParser()
remote = get_upstream_remote(context, "pmaports")
remote = get_upstream_remote(aports)
command = ["git", "show", f"{remote}/master:channels.cfg"]
stdout = pmb.helpers.run.user_output(command, context.aports,
stdout = pmb.helpers.run.user_output(command, aports,
check=False)
try:
cfg.read_string(stdout)
@ -162,7 +160,7 @@ def parse_channels_cfg():
return ret
def get_branches_official(name_repo):
def get_branches_official(repo: Path):
"""Get all branches that point to official release channels.
:returns: list of supported branches, e.g. ["master", "3.11"]
@ -170,17 +168,17 @@ def get_branches_official(name_repo):
# This functions gets called with pmaports and aports_upstream, because
# both are displayed in "pmbootstrap status". But it only makes sense
# to display pmaports there, related code will be refactored soon (#1903).
if name_repo != "pmaports":
if repo.parts[-1] != "pmaports":
return ["master"]
channels_cfg = parse_channels_cfg()
channels_cfg = parse_channels_cfg(repo)
ret = []
for channel, channel_data in channels_cfg["channels"].items():
ret.append(channel_data["branch_pmaports"])
return ret
def pull(name_repo):
def pull(repo_name: str):
"""Check if on official branch and essentially try ``git pull --ff-only``.
Instead of really doing ``git pull --ff-only``, do it in multiple steps
@ -189,18 +187,17 @@ def pull(name_repo):
:returns: integer, >= 0 on success, < 0 on error
"""
branches_official = get_branches_official(name_repo)
context = get_context()
repo = get_path(repo_name)
branches_official = get_branches_official(repo)
# Skip if repo wasn't cloned
path = get_path(context, name_repo)
if not os.path.exists(path):
logging.debug(name_repo + ": repo was not cloned, skipping pull!")
if not os.path.exists(repo):
logging.debug(repo_name + ": repo was not cloned, skipping pull!")
return 1
# Skip if not on official branch
branch = rev_parse(path, extra_args=["--abbrev-ref"])
msg_start = "{} (branch: {}):".format(name_repo, branch)
branch = rev_parse(repo, extra_args=["--abbrev-ref"])
msg_start = "{} (branch: {}):".format(repo_name, branch)
if branch not in branches_official:
logging.warning("{} not on one of the official branches ({}), skipping"
" pull!"
@ -208,13 +205,13 @@ def pull(name_repo):
return -1
# Skip if workdir is not clean
if not clean_worktree(path):
if not clean_worktree(repo):
logging.warning(msg_start + " workdir is not clean, skipping pull!")
return -2
# Skip if branch is tracking different remote
branch_upstream = get_upstream_remote(context, name_repo) + "/" + branch
remote_ref = rev_parse(path, branch + "@{u}", ["--abbrev-ref"])
branch_upstream = get_upstream_remote(repo) + "/" + branch
remote_ref = rev_parse(repo, branch + "@{u}", ["--abbrev-ref"])
if remote_ref != branch_upstream:
logging.warning("{} is tracking unexpected remote branch '{}' instead"
" of '{}'".format(msg_start, remote_ref,
@ -223,16 +220,16 @@ def pull(name_repo):
# Fetch (exception on failure, meaning connection to server broke)
logging.info(msg_start + " git pull --ff-only")
if not context.offline:
pmb.helpers.run.user(["git", "fetch"], path)
if not get_context().offline:
pmb.helpers.run.user(["git", "fetch"], repo)
# Skip if already up to date
if rev_parse(path, branch) == rev_parse(path, branch_upstream):
if rev_parse(repo, branch) == rev_parse(repo, branch_upstream):
logging.info(msg_start + " already up to date")
return 2
# Skip if we can't fast-forward
if not can_fast_forward(path, branch_upstream):
if not can_fast_forward(repo, branch_upstream):
logging.warning("{} can't fast-forward to {}, looks like you changed"
" the git history of your local branch. Skipping pull!"
"".format(msg_start, branch_upstream))
@ -241,21 +238,21 @@ def pull(name_repo):
# Fast-forward now (should not fail due to checks above, so it's fine to
# throw an exception on error)
command = ["git", "merge", "--ff-only", branch_upstream]
pmb.helpers.run.user(command, path, "stdout")
pmb.helpers.run.user(command, repo, "stdout")
return 0
def get_topdir(path: Path):
def get_topdir(repo: Path):
"""Get top-dir of git repo.
:returns: a string with the top dir of the git repository,
or an empty string if it's not a git repository.
"""
return pmb.helpers.run.user(["git", "rev-parse", "--show-toplevel"],
path, output_return=True, check=False).rstrip()
repo, output_return=True, check=False).rstrip()
def get_files(path):
def get_files(repo: Path):
"""Get all files inside a git repository, that are either already in the git tree or are not in gitignore.
Do not list deleted files. To be used for creating a tarball of the git repository.
@ -265,12 +262,12 @@ def get_files(path):
:returns: all files in a git repository as list, relative to path
"""
ret = []
files = pmb.helpers.run.user_output(["git", "ls-files"], path).split("\n")
files = pmb.helpers.run.user_output(["git", "ls-files"], repo).split("\n")
files += pmb.helpers.run.user_output(["git", "ls-files",
"--exclude-standard", "--other"],
path).split("\n")
repo).split("\n")
for file in files:
if os.path.exists(f"{path}/{file}"):
if os.path.exists(f"{repo}/{file}"):
ret += [file]
return ret