summaryrefslogtreecommitdiff
path: root/mdbp/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'mdbp/common.py')
-rw-r--r--mdbp/common.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/mdbp/common.py b/mdbp/common.py
index c03ab37..f22cfdb 100644
--- a/mdbp/common.py
+++ b/mdbp/common.py
@@ -9,12 +9,14 @@ import importlib.resources
import json
import multiprocessing
import pathlib
+import shlex
import tarfile
import tempfile
import typing
import urllib.parse
import debian.deb822
+import debian.debian_support
import requests
try:
@@ -170,6 +172,14 @@ def get_dsc_files(dscpath: pathlib.Path,
return [dscpath.parent / item["name"]
for item in (dscobj or parse_dsc(dscpath))["Files"]]
+
+def build_subdir(source: str, version: str) -> str:
+ """Compute the subdirectory that dpkg-source normally extracts to."""
+ return "%s-%s" % \
+ (source,
+ debian.debian_support.BaseVersion(version).upstream_version)
+
+
def make_option(optname: str, value: typing.Optional[str]) -> typing.List[str]:
"""Construct a valued option if a value is given."""
if not value:
@@ -219,3 +229,24 @@ class AddSpaceSeparatedValues(argparse.Action):
option_string: typing.Optional[str] = None) -> None:
assert isinstance(values, str)
getattr(namespace, self.dest).extend(values.split())
+
+
+def hook_commands(hook: typing.Dict[str, str], sourcetreedir: str) -> \
+ typing.Iterator[str]:
+ """Generate a sequence of shell commands to run the given hook object. The
+ hook object is described in build_schema.yaml. The sourcetreedir parameter
+ specifies the location of the source tree. Its value is assumed to be
+ properly shell quoted such that variables and globs can be used."""
+ user = hook.get("user", "root")
+ cwd = hook.get("cwd", "root")
+ if user != "root" or cwd != "root":
+ yield "cd " + sourcetreedir
+ if user != "root":
+ yield "BUILD_USER=$(stat -c %U .)"
+ if cwd == "root":
+ yield "cd /"
+ if user == "root":
+ yield hook["command"]
+ else:
+ yield 'exec runuser -c %s "$BUILD_USER"' % \
+ shlex.quote(hook["command"])