Skip to content

Commit

Permalink
feat(all): Many improvements and changes
Browse files Browse the repository at this point in the history
1)Moving load_toml to main file, as it is common for all files.
2)Adding context to forward instance and settings to subcommands
3)Modifying at least "commit" subcommand to support latest Pipeline API
4)Add automatic repo info retrieval for "commit" with option to override
by cli arguments.

Signed-off-by: Denys Fedoryshchenko <[email protected]>
  • Loading branch information
nuclearcat committed Sep 10, 2024
1 parent 6682357 commit 16921cd
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 58 deletions.
46 changes: 42 additions & 4 deletions kci-dev/kci-dev.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import toml
import click
import sys
from subcommands import commit
from subcommands import patch


def load_toml(settings):
with open(settings) as fp:
config = toml.load(fp)
return config


@click.group(
help="Stand alone tool for Linux Kernel developers and maintainers that can test local Linux Kernel changes on a enabled KernelCI server"
help="Stand alone tool for Linux Kernel developers and maintainers"
" that assists in the development and testing of patches and commits"
)
@click.version_option("0.0.1", prog_name="kci-dev")
def cli():
@click.option(
"--instance",
help="KernelCI instance",
default="local",
show_default=True,
)
@click.option("--settings", default=".kci-dev.toml",
help="path of toml setting file", show_default=True)
@click.pass_context
def cli(ctx, settings, instance):
ctx.ensure_object(dict)
ctx.obj["CONFIG"] = load_toml(settings)
ctx.obj["INSTANCE"] = instance
# verify if instance is in the toml file
if instance not in ctx.obj["CONFIG"]:
click.secho("Instance not found in the toml file", fg="red")
sys.exit(1)
# instance must have a url and token
if "host" not in ctx.obj["CONFIG"][instance]:
click.secho("Instance must have a host(url)", fg="red")
sys.exit(1)
if "token" not in ctx.obj["CONFIG"][instance]:
click.secho("Instance must have a token", fg="red")
sys.exit(1)
pass


def run():
def load_toml(settings):
with open(settings) as fp:
config = toml.load(fp)
return config


def run(settings=None, instance=None):
cli.add_command(commit.commit)
cli.add_command(patch.patch)
cli()
Expand Down
108 changes: 66 additions & 42 deletions kci-dev/subcommands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import toml
import requests
import json
import sys
import os
from git import Repo


Expand All @@ -13,63 +15,85 @@ def api_connection(host):
return host


def find_diff(path, branch, repository):
repo = Repo(path)
assert not repo.bare
hcommit = repo.iter_commits("origin/master.." + branch)
commits = []
for i in hcommit:
commits.append(repo.git.show(i))
return commits


def send_build(url, patch, branch, treeurl, token):
def send_checkout(apiurl, repourl, repobranch, repocommit, jobfilter, token):
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Bearer {}".format(token),
}
values = {
"treeurl": treeurl,
"branch": branch,
"commit": "example",
"kbuildname": "example",
"testname": "example",
"url": repourl,
"branch": repobranch,
"commit": repocommit,
"jobfilter": jobfilter,
}
response = requests.post(url, headers=headers, files={"patch": patch}, data=values)
print(values)
try:
response = requests.post(apiurl, headers=headers, data=values)
except Exception as e:
click.secho(f"API connection error: {e}", fg="red")
sys.exit(1)
click.secho(response.status_code, fg="green")
click.secho(response.json(), fg="green")


def load_toml(settings):
with open(settings) as fp:
config = toml.load(fp)
return config
def retrieve_repo_data(path):
try:
repo = Repo(path)
except Exception as e:
click.secho(f"Git repo retrieval error: {e}", fg="red")
sys.exit(1)
giturl = repo.remotes.origin.url
branch = repo.active_branch.name
commit = repo.head.commit.hexsha
return giturl, branch, commit


@click.command(help="Test commits from a local Kernel repository")
@click.option(
"--repository",
default="mainline",
help="define the kernel upstream repository where to test local changes",
)
@click.option("--branch", default="master", help="define the repository branch")
@click.option(
"--private",
default=False,
is_flag=True,
help="define if the test results will be published",
)
@click.command(help="Test current commit from a local Kernel repository")
@click.option(
"--path",
default=".",
help="define the directory of the local tree with local changes",
help="Path to the local kernel repository",
)
@click.option("--settings", default=".kci-dev.toml", help="path of toml setting file")
def commit(repository, branch, private, path, settings):
config = load_toml(settings)
url = api_connection(config["connection"]["host"])
diff = find_diff(path, branch, repository)
send_build(url, diff, branch, repository, config["connection"]["token"])
@click.option("--commit", help="Commit hash to test")
@click.option("--branch", help="Branch to test")
@click.option("--jobfilter", help="Job filter")
@click.option("--repository", help="Repository url")
@click.pass_context
def commit(ctx, **kwargs):
cfg = ctx.obj.get("CONFIG")
instance = ctx.obj.get("INSTANCE")
url = api_connection(cfg[instance]["host"])
repourl = None
branch = None
commit = None

# is repo path provided and is it a valid git repository
if 'path' in kwargs and kwargs["path"]:
if not os.path.isdir(kwargs["path"]):
click.secho("Invalid git repository path", fg="red")
sys.exit(1)

# if commit or branch or repository is not provided
# fetch data from local git repository
if not kwargs["commit"] or not kwargs["branch"]\
or not kwargs["repository"] and os.path.isdir(kwargs["path"]):
repourl, branch, commit = retrieve_repo_data(kwargs["path"])

# If any option specified - override the data from local git repository
if kwargs["commit"]:
commit = kwargs["commit"]
if kwargs["branch"]:
branch = kwargs["branch"]
if kwargs["repository"]:
repourl = kwargs["repository"]

# if commit or branch or repository is not available, exit
if not repourl or not branch or not commit:
click.secho("Repository url, branch and commit hash must be provided "
"or retrieved from local repo", fg="red")
sys.exit(1)

send_checkout(url, repourl, branch, commit,
kwargs["jobfilter"], cfg[instance]["token"])


if __name__ == "__main__":
Expand Down
36 changes: 24 additions & 12 deletions kci-dev/subcommands/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ def send_build(url, patch, branch, treeurl, token):
click.secho(response.json(), fg="green")


def load_toml(settings):
with open(settings) as fp:
config = toml.load(fp)
return config


@click.command(help="Test a patch or a mbox file")
@click.option(
"--repository",
Expand All @@ -49,13 +43,31 @@ def load_toml(settings):
is_flag=True,
help="define if the test results will be published",
)

@click.option("--patch", required=True, help="mbox or patch file path")
@click.option("--settings", default=".kci-dev.toml", help="path of toml setting file")
def patch(repository, branch, private, patch, settings):
config = load_toml(settings)
url = api_connection(config["connection"]["host"])
patch = open(patch, "rb")
send_build(url, patch, branch, repository, config["connection"]["token"])

@click.pass_context
def patch(ctx, **kwargs):
cfg = ctx.obj.get('CONFIG')
instance = ctx.obj.get('INSTANCE')
url = api_connection(cfg[instance]["host"])
patchfile = kwargs["patch"]
patchdata = open(patchfile, "rb")
branch = kwargs["branch"]
repository = kwargs["repository"]
private = kwargs["private"]
send_build(url, patchdata, branch, repository, cfg[instance]["token"])


params = {
"repository": None,
"branch": None,
"private": None,
"settings": None,
"patch": None,
"instance": None,
}



if __name__ == "__main__":
Expand Down

0 comments on commit 16921cd

Please sign in to comment.