Skip to content

Commit

Permalink
fix: update anykernel3 config, use venv properly, add ruff linting
Browse files Browse the repository at this point in the history
  • Loading branch information
seppzer0 authored Jan 18, 2025
1 parent 516634f commit 9e87ad1
Show file tree
Hide file tree
Showing 27 changed files with 594 additions and 291 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__
.vscode
.coverage
.pytest_cache
.venv

# git subrepos
# (uncomment below if you explicitely want Docker/Podman to not use locally downloaded dirs)
Expand Down
23 changes: 19 additions & 4 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,56 @@ jobs:
uses: actions/checkout@v3
with:
submodules: recursive

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Build And Assemble

- name: Activate .venv
run: |
python3 -m pip install -r requirement-uv.txt
uv sync --frozen --no-install-project
source .venv/bin/activate
- name: Build And Assemble
run: |
export PYTHONPATH=$(pwd)
uv run scripts/common/py/run_tests.py
uv run scripts/multi_build.py
python3 scripts/common/py/run_tests.py
python3 scripts/multi_build.py
- name: Publish Artifacts
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: "multi-build"
retention-days: 1
if-no-files-found: "error"

- name: Deactivate .venv
run: deactivate

release:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Retrieve artifacts
uses: actions/download-artifact@v3
with:
name: build-artifacts
path: "multi-build"

- name: Get current version
id: version
run: echo "version=$(python3 scripts/get_version.py)" >> $GITHUB_OUTPUT
run: echo "version=$(python3 scripts/common/py/get_version.py)" >> $GITHUB_OUTPUT

- name: Form a tag name
id: tagname
run: echo "tagname=v${{ steps.version.outputs.version }}" >> $GITHUB_OUTPUT

- name: Release
uses: ncipollo/release-action@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ __pycache__
.vscode
.coverage
.pytest_cache
.ruff_cache

# git subrepos
/android_*
Expand Down
7 changes: 5 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ job-build:
- apk update && apk add python3 py3-pip
- python3 -m pip install -r requirement-uv.txt --break-system-packages
- uv sync --frozen --no-install-project
- source .venv/bin/activate
- export PYTHONPATH=$(pwd)
- uv run scripts/common/py/run_tests.py
- uv run scripts/multi_build.py
- python3 scripts/common/py/run_tests.py
- python3 scripts/multi_build.py
- deactivate
artifacts:
paths:
- "multi-build/zero-*.zip"
Expand All @@ -36,6 +38,7 @@ job-tag:
- git config --global user.email "${EMAIL}"
- git remote remove origin
- git remote add origin https://${USERNAME}:${PASSWORD}@gitlab.com/${CI_PROJECT_PATH}
- git fetch --force --tags
- if [ $(git tag | grep "$TAGNAME") ]; then echo "[ * ] Tag already exists, skipping.."; else git tag $TAGNAME && git push origin $TAGNAME; fi
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
Expand Down
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ An advanced Android kernel builder with assets collection and Kali NetHunter sup

- [zero\_kernel](#zero_kernel)
- [Contents](#contents)
- [**Important to Read**](#important-to-read)
- [**Important**](#important)
- [Description](#description)
- [Kernel Features](#kernel-features)
- [Supported Devices \& ROMs](#supported-devices--roms)
Expand All @@ -19,7 +19,7 @@ An advanced Android kernel builder with assets collection and Kali NetHunter sup
- [See also](#see-also)
- [Credits](#credits)

## **Important to Read**
## **Important**

> [!IMPORTANT]
> **\- DISCLAIMER \-**
Expand Down Expand Up @@ -92,7 +92,7 @@ Commands:
- `bundle`.

```help
$ uv run builder --help
$ python3 builder --help
usage: builder [-h] [--clean] {kernel,assets,bundle} ...
A custom builder for the zero_kernel.
Expand All @@ -117,26 +117,28 @@ optional arguments:
To run this tool in a `local` environment, you will need:

- a Debian-based Linux distribution (other types of distros are untested);
- a few [packages](Dockerfile#L15) installed in your system.

You will also need to install [uv](https://github.com/astral-sh/uv). Please refer to [installation guide](https://docs.astral.sh/uv/getting-started/installation/) to choose an option fit for you. It is recommended to use `pip` to install the version of uv used in the project.
- a Debian-based Linux distribution (other distribution families are untested);
- a few [packages](Dockerfile#L15) installed in your system;
- a configured Python environment with Python 3.12+.

```sh
# making "builder" executable from source
export PYTHONPATH=$(pwd)
# installing uv via pip
# install uv version from project file
python3 -m pip install -r requirement-uv.txt
# collecting project dependencies into a local .venv
# make builder/ internal imports visible to itself
export PYTHONPATH=$(pwd)
# prepare and activate dev environment
uv sync --frozen --no-install-project
source .venv/bin/activate
```

Once you are finished working with the project, don't forget to disable the virtual environment (venv) via simple `deactivate`.

### Kernel

Kernel build process can be launched using the `kernel` subcommand.

```help
$ uv run builder kernel --help
$ python3 builder kernel --help
usage: builder kernel [-h] --build-env {local,docker,podman} --base {los,pa,x,aosp}
--codename CODENAME --lkv LKV [-c] [--clean-image] [--ksu]
Expand All @@ -159,7 +161,7 @@ options:
As mentioned, there is also an asset downloader, which can collect latest versions of ROM, TWRP, Magisk and it's modules, Kali Chroot etc.

```help
$ uv run builder assets --help
$ python3 builder assets --help
usage: builder assets [-h] --build-env {local,docker,podman} --base {los,pa,x,aosp}
--codename CODENAME --chroot {full,minimal} [--rom-only]
[--clean-image] [--clean] [--ksu]
Expand Down Expand Up @@ -201,7 +203,7 @@ Options `full` and `conan` collect all of the assets required to successfuly fla
Option named `slim` is a much lighter version of `full` packaging, as only the ROM is collected from the asset list. This is done to reduce package sizes while ensuring the kernel+ROM compatibility.

```help
$ uv run builder bundle --help
$ python3 builder bundle --help
usage: builder bundle [-h] --build-env {local,docker,podman} --base {los,pa,x,aosp}
--codename CODENAME --lkv LKV --package-type
{conan,slim,full} [--conan-upload] [--clean-image] [--ksu]
Expand Down Expand Up @@ -229,19 +231,19 @@ Here are some examples of commands:
**(Recommended)** Build kernel and collect ROM via Docker:

```sh
uv run builder bundle --build-env=docker --base=los --codename=dumpling --lkv=4.4 --package-type=slim
python3 builder bundle --build-env=docker --base=los --codename=dumpling --lkv=4.4 --package-type=slim
```

Build kernel locally:

```sh
uv run builder kernel --build-env=local --base=los --codename=dumpling --lkv=4.4
python3 builder kernel --build-env=local --base=los --codename=dumpling --lkv=4.4
```

Collect all of the assets locally:

```sh
uv run builder assets --build-env=local --base=los --codename=dumpling --package-type=full
python3 builder assets --build-env=local --base=los --codename=dumpling --package-type=full
```

## See also
Expand Down
26 changes: 19 additions & 7 deletions builder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import argparse
from pathlib import Path
from importlib.metadata import version

from builder.tools import cleaning as cm, commands as ccmd
from builder.configs import ArgumentConfig, DirectoryConfig as dcfg
Expand All @@ -15,29 +16,34 @@ def parse_args() -> argparse.Namespace:
"""Parse the script arguments."""
# show the 'help' message if no arguments supplied
args = None if sys.argv[1:] else ["-h"]

# parser and subparsers
parser_parent = argparse.ArgumentParser(description="A custom builder for the zero kernel.")
parser_parent = argparse.ArgumentParser(description="Advanced Android kernel builder with Kali NetHunter support.")
subparsers = parser_parent.add_subparsers(dest="command")
parser_kernel = subparsers.add_parser("kernel", help="build the kernel")
parser_assets = subparsers.add_parser("assets", help="collect assets")
parser_bundle = subparsers.add_parser("bundle", help="build the kernel + collect assets")
# add a single argument for the main parser

# main parser arguments
parser_parent.add_argument(
"--clean",
dest="clean_root",
action="store_true",
help="clean the root directory"
)
parser_parent.add_argument("-v", "--version", action="version", version=version("zero-kernel"))

# common argument attributes for subparsers
help_base = "select a kernel base for the build"
help_codename = "select device codename"
help_benv = "select build environment"
help_clean = "remove Docker/Podman image from the host machine after build"
choices_benv = ("local", "docker", "podman")
choices_base = ("los", "pa", "x", "aosp")
choices_benv = {"local", "docker", "podman"}
choices_base = {"los", "pa", "x", "aosp"}
help_defconfig = "specify path to custom defconfig"
help_ksu = "add KernelSU support"
help_lkv = "select Linux Kernel Version"

# kernel
parser_kernel.add_argument(
"--build-env",
Expand Down Expand Up @@ -90,6 +96,7 @@ def parse_args() -> argparse.Namespace:
dest="defconfig",
help=help_defconfig
)

# assets
parser_assets.add_argument(
"--build-env",
Expand Down Expand Up @@ -143,6 +150,7 @@ def parse_args() -> argparse.Namespace:
dest="ksu",
help=help_ksu
)

# bundle
parser_bundle.add_argument(
"--build-env",
Expand Down Expand Up @@ -176,7 +184,7 @@ def parse_args() -> argparse.Namespace:
type=str,
required=True,
dest="package_type",
choices=("conan", "slim", "full"),
choices={"conan", "slim", "full"},
help="select package type of the bundle"
)
parser_bundle.add_argument(
Expand Down Expand Up @@ -212,20 +220,24 @@ def main(args: argparse.Namespace) -> None:
if args.clean_root:
cm.root()
sys.exit(0)

# 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]
os.environ["KVERSION"] = f.read().split('version = "')[1].split('"')[0]

# create a config for checking and storing arguments
if args.command != "assets" and args.defconfig:
args.defconfig = args.defconfig if args.defconfig.is_absolute() else Path.cwd() / args.defconfig
arguments = vars(args)
acfg = ArgumentConfig(**arguments)
acfg.check_settings()
# determine the build

# determine the build variation
match args.benv:
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":
Expand Down
16 changes: 14 additions & 2 deletions builder/clients/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def direct_url(self) -> str:
def run(self) -> str | None:
"""Get the latest version of an artifact from GitHub project."""
response = requests.get(self.endpoint).json()

# this will check whether the GitHub API usage is exceeded
try:
data = response["message"]
Expand All @@ -40,14 +41,17 @@ def run(self) -> str | None:
)
except Exception:
pass

try:
# get direct download URL and optionally filter it with the given parameter
data = response["assets"]
browser_download_urls = []

for elem in data:
url_dto = elem["browser_download_url"]
if url_dto and self.file_filter in url_dto:
browser_download_urls.append(url_dto)

# if there is more than one fitting response -- throw an error
if len(browser_download_urls) > 1:
msg.error(
Expand All @@ -56,16 +60,24 @@ def run(self) -> str | None:
)
else:
data = "".join(browser_download_urls)

except Exception:
# if not available via API -- use regular "git clone"
rdir = Path(dcfg.assets, self.direct_url.rsplit("/", 1)[1])
msg.note(f"Non-API GitHub resolution for {self.project}")

rdir = Path(dcfg.assets, self.direct_url.rsplit("/", 1)[1])

cm.remove(rdir)
ccmd.launch(f"git clone --depth 1 --remote-submodules --recurse-submodules --shallow-submodules {self.direct_url} {rdir}")
ccmd.launch(
"git clone --depth 1 --remote-submodules --recurse-submodules --shallow-submodules {} {}"
.format(self.direct_url, rdir)
)
os.chdir(rdir)
cm.remove(".git*")
os.chdir(dcfg.assets)
shutil.make_archive(str(rdir), "zip", rdir)
cm.remove(rdir)

return None

return data
2 changes: 2 additions & 0 deletions builder/clients/rom_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def map_codename(self) -> str:

def run(self) -> str:
data = requests.get(self.endpoint)

try:
data = data.json()[self.json_key][0]["url"]
except Exception:
Expand All @@ -38,4 +39,5 @@ def run(self) -> str:
f"Could not connect to {self.rom_name} API, HTTP status code: {data.status_code}",
dont_exit=exit_flag
)

return str(data)
Loading

0 comments on commit 9e87ad1

Please sign in to comment.