Skip to content

Commit

Permalink
refactor: update type hints, arguments list, container engine list
Browse files Browse the repository at this point in the history
  • Loading branch information
seppzer0 committed Apr 21, 2024
1 parent 8c1e10f commit 1e31e3f
Show file tree
Hide file tree
Showing 35 changed files with 450 additions and 514 deletions.
1 change: 1 addition & 0 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
python3 -m poetry config virtualenvs.create false
python3 -m poetry install --no-root
export PYTHONPATH=$(pwd)
python3 scripts/run_tests.py
python3 scripts/multi_build.py
- name: Publish Artifacts
uses: actions/upload-artifact@v3
Expand Down
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ job-build:
- python3 -m poetry config virtualenvs.create false
- python3 -m poetry install --no-root
- export PYTHONPATH=$(pwd)
- python3 scripts/run_tests.py
- python3 scripts/multi_build.py
artifacts:
paths:
Expand Down
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ COPY . ${WDIR}
WORKDIR ${WDIR}
ENV PYTHONPATH ${WDIR}

# install system packages;
# NeoVim is added for debugging sessions.
# install system packages
RUN \
apt-get update \
&& \
apt-get install -y \
neovim \
curl \
wget \
git \
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ The custom build wrapper (aka "builder") consists of 2 core components and 3 pri

Components:

- kernel builder;
- assets collector.
- `kernel_builder`;
- `assets_collector`.

Commands:

- kernel;
- assets;
- bundle.
- `kernel`;
- `assets`;
- `bundle`.

```help
$ python3 builder --help
Expand Down
70 changes: 22 additions & 48 deletions builder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import json
import argparse

from builder.tools import cleaning as cm, messages as msg
from builder.tools import cleaning as cm, messages as msg, commands as ccmd
from builder.configs import ArgumentConfig, DirectoryConfig as dcfg
from builder.engines import DockerEngine, PodmanEngine
from builder.engines import GenericContainerEngine
from builder.commands import KernelCommand, AssetsCommand, BundleCommand


def parse_args() -> argparse.Namespace:
"""Parse the script arguments."""
# show the 'help' message if no arguments supplied
Expand All @@ -31,14 +32,11 @@ def parse_args() -> argparse.Namespace:
help_codename = "select device codename"
help_benv = "select build environment"
help_clean = "remove Docker/Podman image from the host machine after build"
help_loglvl = "select log level"
choices_benv = ("local", "docker", "podman")
choices_loglvl = ("normal", "verbose", "quiet")
choices_base = ("los", "pa", "x", "aosp")
help_logfile = "save logs to a file"
help_ksu = "add KernelSU support"
help_lkv = "select Linux Kernel Version"
default_loglvl = "normal"
# kernel
parser_kernel.add_argument(
"--build-env",
Expand Down Expand Up @@ -75,13 +73,6 @@ def parse_args() -> argparse.Namespace:
dest="clean_image",
help=help_clean
)
parser_kernel.add_argument(
"--log-level",
dest="loglvl",
choices=choices_loglvl,
default=default_loglvl,
help=help_loglvl
)
parser_kernel.add_argument(
"-o", "--output",
dest="outlog",
Expand Down Expand Up @@ -136,13 +127,6 @@ def parse_args() -> argparse.Namespace:
action="store_true",
help="autoclean 'assets' folder if it exists"
)
parser_assets.add_argument(
"--log-level",
dest="loglvl",
choices=choices_loglvl,
default=default_loglvl,
help=help_loglvl
)
parser_assets.add_argument(
"-o", "--output",
dest="outlog",
Expand Down Expand Up @@ -197,13 +181,6 @@ def parse_args() -> argparse.Namespace:
dest="clean_image",
help=help_clean
)
parser_bundle.add_argument(
"--log-level",
dest="loglvl",
choices=choices_loglvl,
default=default_loglvl,
help=help_loglvl
)
parser_bundle.add_argument(
"-o", "--output",
dest="outlog",
Expand All @@ -224,7 +201,6 @@ def main(args: argparse.Namespace) -> None:
if args.clean_root:
cm.root()
sys.exit(0)
os.environ["LOGLEVEL"] = args.loglvl
# define env variable with kernel version
with open(dcfg.root / "pyproject.toml", encoding="utf-8") as f:
os.environ["KVERSION"] = f.read().split("version = \"")[1].split("\"")[0]
Expand All @@ -238,39 +214,37 @@ def main(args: argparse.Namespace) -> None:
if args.outlog in os.listdir():
os.remove(args.outlog)
os.environ["OSTREAM"] = args.outlog
msg.outputstream()
# determine the build
match args.benv:
case "docker":
DockerEngine(**json.loads(acfg.model_dump_json())).run()
case "podman":
PodmanEngine(**json.loads(acfg.model_dump_json())).run()
case "docker" | "podman":
with GenericContainerEngine(**json.loads(acfg.model_dump_json())) as engined_cmd:
ccmd.launch(engined_cmd)
case "local":
match args.command:
case "kernel":
KernelCommand(
codename = acfg.codename,
base = acfg.base,
lkv = acfg.lkv,
clean_kernel = acfg.clean_kernel,
ksu = acfg.ksu,
codename = args.codename,
base = args.base,
lkv = args.lkv,
clean_kernel = args.clean_kernel,
ksu = args.ksu,
).run()
case "assets":
AssetsCommand(
codename = acfg.codename,
base = acfg.base,
chroot = acfg.chroot,
clean_assets = acfg.clean_assets,
rom_only = acfg.rom_only,
ksu = acfg.ksu,
codename = args.codename,
base = args.base,
chroot = args.chroot,
clean_assets = args.clean_assets,
rom_only = args.rom_only,
ksu = args.ksu,
).run()
case "bundle":
BundleCommand(
codename = acfg.codename,
base = acfg.base,
lkv = acfg.lkv,
package_type = acfg.package_type,
ksu = acfg.ksu,
codename = args.codename,
base = args.base,
lkv = args.lkv,
package_type = args.package_type,
ksu = args.ksu,
).run()


Expand Down
6 changes: 2 additions & 4 deletions builder/clients/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
class GitHubApi(BaseModel):
"""Limited interaction with GitHub API.
:param _endpoint: API endpoint to interact with.
:param _direct_url: Direct URL for the specified repo.
:param project: GitHub project name (owner/repo).
:param file_filter: A filter to select specific files from project's artifacts.
:param str project: GitHub project name (owner/repo).
:param Optional[str]=None file_filter: A filter to select specific files from project's artifacts.
"""

_endpoint = "https://api.github.com/repos/{}/releases/latest"
Expand Down
1 change: 0 additions & 1 deletion builder/clients/pa.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ParanoidAndroidApi(RomApi):
rom_name: str = "PA"

@override
@property
def codename_mapper(self) -> str:
# specific rules for PA's API
specials = {
Expand Down
13 changes: 6 additions & 7 deletions builder/clients/rom_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class RomApi(BaseModel, IRomApi):
"""A generic class for interacting with ROMs' APIs.
:param endpoint: API endpoint to interact with.
:param json_key: A JSON key to look for in the response.
:param rom_name: ROM project's name.
:param rom_only: Flag indicating ROM-only asset collection.
:param str endpoint: API endpoint to interact with.
:param str json_key: A JSON key to look for in the response.
:param str rom_name: ROM project's name.
:param bool rom_only: Flag indicating ROM-only asset collection.
"""

endpoint: str
Expand All @@ -22,9 +22,8 @@ class RomApi(BaseModel, IRomApi):

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.endpoint = self.endpoint.format(self.codename_mapper)
self.endpoint = self.endpoint.format(self.codename_mapper())

@property
def codename_mapper(self) -> str:
# by default, codename is devicename
return self.codename
Expand All @@ -39,4 +38,4 @@ def run(self) -> str:
f"Could not connect to {self.rom_name} API, HTTP status code: {data.status_code}",
dont_exit=exit_flag
)
return data
return str(data)
3 changes: 0 additions & 3 deletions builder/commands/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@

class AssetsCommand(AssetsCollector):
"""A command responsible for launching the 'assets_collector' core module directly."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
37 changes: 19 additions & 18 deletions builder/commands/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import shutil
import itertools
from typing import Literal
from pydantic import BaseModel

from builder.core import KernelBuilder, AssetsCollector
Expand All @@ -14,10 +15,10 @@ class BundleCommand(BaseModel, IBundleCommand):
"""A command that packages the artifacts produced both
by 'kernel_builder' and 'assets_collector' core modules.
:param base: Kernel source base.
:param lkv: Linux kernel version.
:param package_type: Package type.
:param ksu: Flag indicating KernelSU support.
:param str base: Kernel source base.
:param str lkv: Linux kernel version.
:param str package_type: Package type.
:param bool ksu: Flag indicating KernelSU support.
"""

codename: str
Expand All @@ -26,7 +27,7 @@ class BundleCommand(BaseModel, IBundleCommand):
package_type: str
ksu: bool

def _build_kernel(self, rom_name: str, clean_only: bool = False) -> None:
def build_kernel(self, rom_name: str, clean_only: bool = False) -> None:
if not dcfg.kernel.is_dir() or clean_only is True:
KernelBuilder(
codename = self.codename,
Expand All @@ -40,7 +41,7 @@ def _build_kernel(self, rom_name: str, clean_only: bool = False) -> None:
def _rom_only_flag(self) -> bool:
return True if "full" not in self.package_type else False

def _collect_assets(self, rom_name: str, chroot: str) -> None:
def collect_assets(self, rom_name: str, chroot: Literal["full", "minimal"]) -> None:
AssetsCollector(
codename = self.codename,
base = rom_name,
Expand All @@ -50,7 +51,7 @@ def _collect_assets(self, rom_name: str, chroot: str) -> None:
ksu = self.ksu,
).run()

def _conan_sources(self) -> None:
def conan_sources(self) -> None:
sourcedir = dcfg.root / "source"
print("\n", end="")
msg.note("Copying sources for Conan packaging..")
Expand All @@ -71,12 +72,12 @@ def _conan_sources(self) -> None:
msg.done("Done!")

@staticmethod
def _conan_options(json_file: str) -> dict:
def conan_options(json_file: str) -> dict:
with open(json_file, encoding="utf-8") as f:
json_data = json.load(f)
return json_data

def _conan_package(self, options: list[str], reference: str) -> None:
def conan_package(self, options: tuple[str, ...], reference: str) -> None:
cmd = f"conan export-pkg . {reference}"
for option_value in options:
# not the best solution, but will work temporarily for 'rom' and 'chroot' options
Expand All @@ -87,7 +88,7 @@ def _conan_package(self, options: list[str], reference: str) -> None:
ccmd.launch(cmd)

@staticmethod
def _conan_upload(reference: str) -> None:
def conan_upload(reference: str) -> None:
# configure Conan client and upload packages
url = "https://gitlab.com/api/v4/projects/40803264/packages/conan"
alias = "zero-kernel-conan"
Expand All @@ -101,9 +102,9 @@ def run(self) -> None:
# determine the bundle type and process it
match self.package_type:
case "slim" | "full":
self._build_kernel(self.base)
self.build_kernel(self.base)
# "full" chroot is hardcoded here
self._collect_assets(self.base, "full")
self.collect_assets(self.base, "full")
# clean up
if dcfg.bundle.is_dir():
contents = dcfg.bundle.glob("*")
Expand Down Expand Up @@ -134,13 +135,13 @@ def run(self) -> None:
option_sets = list(itertools.product([self.base], chroot))
# build and upload Conan packages
for opset in option_sets:
self._build_kernel(opset[0])
self._build_kernel(opset[0], True)
self._conan_sources()
self._collect_assets(opset[0], opset[1])
self._conan_package(opset, reference)
self.build_kernel(opset[0])
self.build_kernel(opset[0], True)
self.conan_sources()
self.collect_assets(opset[0], opset[1])
self.conan_package(opset, reference)
# upload packages
if os.getenv("CONAN_UPLOAD_CUSTOM") == "1":
self._conan_upload(reference)
self.conan_upload(reference)
# navigate back to root directory
os.chdir(dcfg.root)
4 changes: 2 additions & 2 deletions builder/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .arguments import ArgumentConfig
from .directories import DirectoryConfig
from .argument import ArgumentConfig
from .directory import DirectoryConfig
Loading

0 comments on commit 1e31e3f

Please sign in to comment.