Skip to content

Commit 0b1a049

Browse files
committed
feat(all): Many improvements and changes
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. 5)Modify python to python3 in shebang, as many distros dont have python Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 6682357 commit 0b1a049

3 files changed

Lines changed: 132 additions & 58 deletions

File tree

kci-dev/kci-dev.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
import toml
45
import click
6+
import sys
57
from subcommands import commit
68
from subcommands import patch
79

810

11+
def load_toml(settings):
12+
with open(settings) as fp:
13+
config = toml.load(fp)
14+
return config
15+
16+
917
@click.group(
10-
help="Stand alone tool for Linux Kernel developers and maintainers that can test local Linux Kernel changes on a enabled KernelCI server"
18+
help="Stand alone tool for Linux Kernel developers and maintainers"
19+
" that assists in the development and testing of patches and commits"
1120
)
1221
@click.version_option("0.0.1", prog_name="kci-dev")
13-
def cli():
22+
@click.option(
23+
"--instance",
24+
help="KernelCI instance",
25+
default="local",
26+
show_default=True,
27+
)
28+
@click.option("--settings", default=".kci-dev.toml",
29+
help="path of toml setting file", show_default=True)
30+
@click.pass_context
31+
def cli(ctx, settings, instance):
32+
ctx.ensure_object(dict)
33+
ctx.obj["CONFIG"] = load_toml(settings)
34+
ctx.obj["INSTANCE"] = instance
35+
# verify if instance is in the toml file
36+
if instance not in ctx.obj["CONFIG"]:
37+
click.secho("Instance not found in the toml file", fg="red")
38+
sys.exit(1)
39+
# instance must have a url and token
40+
if "host" not in ctx.obj["CONFIG"][instance]:
41+
click.secho("Instance must have a host(url)", fg="red")
42+
sys.exit(1)
43+
if "token" not in ctx.obj["CONFIG"][instance]:
44+
click.secho("Instance must have a token", fg="red")
45+
sys.exit(1)
1446
pass
1547

1648

17-
def run():
49+
def load_toml(settings):
50+
with open(settings) as fp:
51+
config = toml.load(fp)
52+
return config
53+
54+
55+
def run(settings=None, instance=None):
1856
cli.add_command(commit.commit)
1957
cli.add_command(patch.patch)
2058
cli()

kci-dev/subcommands/commit.py

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import toml
66
import requests
77
import json
8+
import sys
9+
import os
810
from git import Repo
911

1012

@@ -13,63 +15,85 @@ def api_connection(host):
1315
return host
1416

1517

16-
def find_diff(path, branch, repository):
17-
repo = Repo(path)
18-
assert not repo.bare
19-
hcommit = repo.iter_commits("origin/master.." + branch)
20-
commits = []
21-
for i in hcommit:
22-
commits.append(repo.git.show(i))
23-
return commits
24-
25-
26-
def send_build(url, patch, branch, treeurl, token):
18+
def send_checkout(apiurl, repourl, repobranch, repocommit, jobfilter, token):
2719
headers = {
2820
"Content-Type": "application/json; charset=utf-8",
2921
"Authorization": "Bearer {}".format(token),
3022
}
3123
values = {
32-
"treeurl": treeurl,
33-
"branch": branch,
34-
"commit": "example",
35-
"kbuildname": "example",
36-
"testname": "example",
24+
"url": repourl,
25+
"branch": repobranch,
26+
"commit": repocommit,
27+
"jobfilter": jobfilter,
3728
}
38-
response = requests.post(url, headers=headers, files={"patch": patch}, data=values)
29+
print(values)
30+
try:
31+
response = requests.post(apiurl, headers=headers, data=values)
32+
except Exception as e:
33+
click.secho(f"API connection error: {e}", fg="red")
34+
sys.exit(1)
3935
click.secho(response.status_code, fg="green")
4036
click.secho(response.json(), fg="green")
4137

4238

43-
def load_toml(settings):
44-
with open(settings) as fp:
45-
config = toml.load(fp)
46-
return config
39+
def retrieve_repo_data(path):
40+
try:
41+
repo = Repo(path)
42+
except Exception as e:
43+
click.secho(f"Git repo retrieval error: {e}", fg="red")
44+
sys.exit(1)
45+
giturl = repo.remotes.origin.url
46+
branch = repo.active_branch.name
47+
commit = repo.head.commit.hexsha
48+
return giturl, branch, commit
4749

4850

49-
@click.command(help="Test commits from a local Kernel repository")
50-
@click.option(
51-
"--repository",
52-
default="mainline",
53-
help="define the kernel upstream repository where to test local changes",
54-
)
55-
@click.option("--branch", default="master", help="define the repository branch")
56-
@click.option(
57-
"--private",
58-
default=False,
59-
is_flag=True,
60-
help="define if the test results will be published",
61-
)
51+
@click.command(help="Test current commit from a local Kernel repository")
6252
@click.option(
6353
"--path",
64-
default=".",
65-
help="define the directory of the local tree with local changes",
54+
help="Path to the local kernel repository",
6655
)
67-
@click.option("--settings", default=".kci-dev.toml", help="path of toml setting file")
68-
def commit(repository, branch, private, path, settings):
69-
config = load_toml(settings)
70-
url = api_connection(config["connection"]["host"])
71-
diff = find_diff(path, branch, repository)
72-
send_build(url, diff, branch, repository, config["connection"]["token"])
56+
@click.option("--commit", help="Commit hash to test")
57+
@click.option("--branch", help="Branch to test")
58+
@click.option("--jobfilter", help="Job filter")
59+
@click.option("--repository", help="Repository url")
60+
@click.pass_context
61+
def commit(ctx, **kwargs):
62+
cfg = ctx.obj.get("CONFIG")
63+
instance = ctx.obj.get("INSTANCE")
64+
url = api_connection(cfg[instance]["host"])
65+
repourl = None
66+
branch = None
67+
commit = None
68+
69+
# is repo path provided and is it a valid git repository
70+
if 'path' in kwargs and kwargs["path"]:
71+
if not os.path.isdir(kwargs["path"]):
72+
click.secho("Invalid git repository path", fg="red")
73+
sys.exit(1)
74+
75+
# if commit or branch or repository is not provided
76+
# fetch data from local git repository
77+
if not kwargs["commit"] or not kwargs["branch"]\
78+
or not kwargs["repository"] and os.path.isdir(kwargs["path"]):
79+
repourl, branch, commit = retrieve_repo_data(kwargs["path"])
80+
81+
# If any option specified - override the data from local git repository
82+
if kwargs["commit"]:
83+
commit = kwargs["commit"]
84+
if kwargs["branch"]:
85+
branch = kwargs["branch"]
86+
if kwargs["repository"]:
87+
repourl = kwargs["repository"]
88+
89+
# if commit or branch or repository is not available, exit
90+
if not repourl or not branch or not commit:
91+
click.secho("Repository url, branch and commit hash must be provided "
92+
"or retrieved from local repo", fg="red")
93+
sys.exit(1)
94+
95+
send_checkout(url, repourl, branch, commit,
96+
kwargs["jobfilter"], cfg[instance]["token"])
7397

7498

7599
if __name__ == "__main__":

kci-dev/subcommands/patch.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ def send_build(url, patch, branch, treeurl, token):
3030
click.secho(response.json(), fg="green")
3131

3232

33-
def load_toml(settings):
34-
with open(settings) as fp:
35-
config = toml.load(fp)
36-
return config
37-
38-
3933
@click.command(help="Test a patch or a mbox file")
4034
@click.option(
4135
"--repository",
@@ -49,13 +43,31 @@ def load_toml(settings):
4943
is_flag=True,
5044
help="define if the test results will be published",
5145
)
46+
5247
@click.option("--patch", required=True, help="mbox or patch file path")
53-
@click.option("--settings", default=".kci-dev.toml", help="path of toml setting file")
54-
def patch(repository, branch, private, patch, settings):
55-
config = load_toml(settings)
56-
url = api_connection(config["connection"]["host"])
57-
patch = open(patch, "rb")
58-
send_build(url, patch, branch, repository, config["connection"]["token"])
48+
49+
@click.pass_context
50+
def patch(ctx, **kwargs):
51+
cfg = ctx.obj.get('CONFIG')
52+
instance = ctx.obj.get('INSTANCE')
53+
url = api_connection(cfg[instance]["host"])
54+
patchfile = kwargs["patch"]
55+
patchdata = open(patchfile, "rb")
56+
branch = kwargs["branch"]
57+
repository = kwargs["repository"]
58+
private = kwargs["private"]
59+
send_build(url, patchdata, branch, repository, cfg[instance]["token"])
60+
61+
62+
params = {
63+
"repository": None,
64+
"branch": None,
65+
"private": None,
66+
"settings": None,
67+
"patch": None,
68+
"instance": None,
69+
}
70+
5971

6072

6173
if __name__ == "__main__":

0 commit comments

Comments
 (0)