cli: add build command

This commit is contained in:
Rasmus Moorats 2023-08-31 17:38:38 +03:00
parent 870c60e18f
commit b07df579b2
Signed by: xx
GPG key ID: FE14255A6AE7241C
3 changed files with 58 additions and 6 deletions

View file

@ -1,10 +1,11 @@
#!/usr/bin/env python
from cleo.application import Application
from aurtomata.cli import RenderCommand
from aurtomata.cli import RenderCommand, BuildCommand
app = Application("aurtomata")
app.add(RenderCommand())
app.add(BuildCommand())
def run():

View file

@ -30,17 +30,20 @@ class Supervisor:
image_name: str
def __init__(self):
self.dc = docker.from_env()
self.containers = {}
self.done_update = False
self.image_name = "aurtomata-" + "".join(choices(ascii_lowercase + digits, k=6))
self.dc = docker.from_env()
def __del__(self):
for container in self.containers.values():
container.stop()
container.remove()
self.dc.images.remove(image=self.image_name, force=True)
try:
self.dc.images.remove(image=self.image_name, force=True)
except AttributeError:
pass
def ensure_up_to_date(self) -> bool:
# ensures that for this session, the arch base image has been updated.
@ -69,12 +72,15 @@ class Supervisor:
detach=True,
working_dir=repo_mount_point,
volumes=volumes,
name=f"{self.image_name}-{name}",
name=self.name_container(name),
)
self.containers[name] = container
return container
def name_container(self, name: str) -> str:
return f"{self.image_name}-{name}"
def build(self, name: str, dir: str) -> bool:
# attempts to build PKGBUILD in specified "dir" using specified container "name"
container = self.run(name, "makepkg -sfc --noconfirm --noprogressbar", dir)
@ -83,7 +89,8 @@ class Supervisor:
def logs(self, name: str) -> str | None:
# returns logs for container by name
container = self.containers.get(name)
if container is None: return
if container is None:
return
container.wait()
return container.logs(timestamps=True).decode("utf-8")

View file

@ -6,6 +6,7 @@ from shutil import rmtree
from aurtomata.repo import Repo
from aurtomata.template import NoRepoPyException, NoPkgbuildTemplateException
from aurtomata.ci import Supervisor
basedir = Path().home() / ".local/aurtomata"
@ -78,10 +79,53 @@ class RenderCommand(Command):
"template"
)
self.line(
f"<error>Can not find <info>PKGBUILD.jinja</info> in <info>{template_dir}</info></error>"
f"Can not find <info>PKGBUILD.jinja</info> in <info>{template_dir}</info>",
"error",
)
return 1
self.line(
f"Rendered version <info>{version}</info> to <info>{repo.outdir / repo.name}</info>"
)
class BuildCommand(Command):
name = "build"
description = "Try building a single package using 'makepkg'"
arguments = [
argument(
"repo", description="Location of directory where the PKGBUILD is located"
)
]
def handle(self):
fpath = Path(self.argument("repo")).resolve()
if not fpath.exists():
self.line(f"No such file or directory: <info>{fpath}</info>", "error")
return 1
elif not fpath.is_dir():
self.line(f"Path <info>{fpath}</info> is not a directory!", "error")
return 1
elif not (fpath / "PKGBUILD").exists():
self.line(f"No PKGBUILD found in <info>{fpath}</info>", "error")
return 1
basename = fpath.stem
s = Supervisor()
self.line(f"Building fresh Arch Linux image: <info>{s.image_name}</info>")
s.ensure_up_to_date()
self.line(
f"Building <info>{basename}</info> in container <info>{s.name_container(basename)}</info>"
)
success = s.build(basename, fpath)
if success:
self.line("Build successful")
return
self.line("Build failed! Container logs:", "error")
print(s.logs(basename))
return 2