Skip to content

Commit 7eb73d3

Browse files
committed
kt: Add setup command
Usage: kt setup --help It will clone all the repos a developer needs for kernel development. It clones the common_repos from kernels.yaml file in the config.base_path directory. If config.base_path = ~/ciq, these will be created: ~/ciq/kernel-src-tree ~/ciq/dist-git-tree-fips ~/ciq/dist-git-tree-cbr ~/ciq/dit-git-tree-lts ~/ciq/kernel-src-tree-tools ~/ciq/kernel-tools Signed-off-by: Roxana Nicolescu <rnicolescu@ciq.com>
1 parent a2874d5 commit 7eb73d3

7 files changed

Lines changed: 123 additions & 12 deletions

File tree

bin/kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import logging
55
import click
66

77
from kt.commands.list_kernels.command import list_kernels
8+
from kt.commands.setup.command import setup
89

910
epilog = """
1011
Base of all tooling used for kernel development.
@@ -22,6 +23,7 @@ def main():
2223
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
2324

2425
cli.add_command(list_kernels)
26+
cli.add_command(setup)
2527
cli()
2628

2729

kt/KT.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,30 @@ lts-8.8
145145
lts-9.2
146146
lts-9.4
147147
```
148+
149+
### kt setup
150+
151+
```
152+
$ kt setup --help
153+
```
154+
155+
It prepares the working directory for later commands:
156+
157+
It clones the common_repos from kernels.yaml file in the config.base_path
158+
directory.
159+
If config.base_path = ~/ciq, these will be created:
160+
161+
~/ciq/kernel-src-tree
162+
163+
~/ciq/dist-git-tree-fips
164+
165+
~/ciq/dist-git-tree-cbr
166+
167+
~/ciq/dit-git-tree-lts
168+
169+
~/ciq/kernel-src-tree-tools
170+
171+
~/ciq/kernel-tools
172+
173+
If there's a repo that needs to be cloned relevant for any future command,
174+
this is when it should be cloned.

kt/commands/setup/command.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import click
2+
3+
from kt.commands.setup.impl import main
4+
5+
epilog = """
6+
Prepares the working directory for later commands:
7+
8+
It clones the common_repos from kernels.yaml file in the config.base_path
9+
directory.
10+
If the repos are cloned already, they will be updated.
11+
12+
If config.base_path = ~/ciq, these will be created:
13+
14+
~/ciq/kernel-src-tree
15+
16+
~/ciq/dist-git-tree-fips
17+
18+
~/ciq/dist-git-tree-cbr
19+
20+
~/ciq/dit-git-tree-lts
21+
22+
~/ciq/kernel-src-tree-tools
23+
24+
~/ciq/kernel-tools
25+
26+
Examples:
27+
28+
\b
29+
$ kt setup
30+
"""
31+
32+
33+
@click.command(epilog=epilog)
34+
def setup():
35+
main()

kt/commands/setup/impl.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from kt.ktlib.config import Config
2+
from kt.ktlib.kernels import KernelsInfo
3+
4+
5+
def main():
6+
config = Config.load()
7+
8+
# create working dir if it does not exist
9+
config.base_path.mkdir(parents=True, exist_ok=True)
10+
11+
repos = KernelsInfo.from_yaml(config=config).repos
12+
for repo in repos.values():
13+
repo.setup_repo()

kt/ktlib/kernels.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@
55
from pathlib3x import Path
66

77
from kt.ktlib.config import Config
8+
from kt.ktlib.repo import RepoInfo
89
from kt.ktlib.util import Constants
910

1011
# TODO move this to a separate repo
1112
KERNEL_INFO_YAML_PATH = Path(__file__).parent.parent.joinpath("data/kernels.yaml")
1213

1314

14-
@dataclass
15-
class RepoInfo:
16-
"""
17-
Dataclass that represents a local clone of a git repository.
18-
folder: absolute path to the local clone
19-
url: remote origin
20-
"""
21-
22-
folder: Path
23-
url: str
24-
25-
2615
@dataclass
2716
class KernelInfo:
2817
"""

kt/ktlib/repo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
from dataclasses import dataclass
3+
4+
import git
5+
from git import Repo
6+
from pathlib3x import Path
7+
8+
9+
@dataclass
10+
class RepoInfo:
11+
"""
12+
Dataclass that represents a local clone of a git repository.
13+
folder: absolute path to the local clone
14+
url: remote origin
15+
"""
16+
17+
folder: Path
18+
url: str
19+
20+
def _clone_repo(self):
21+
"""
22+
It clones the repo into destination folder
23+
"""
24+
25+
# TODO show progress
26+
logging.info(f"Cloning {self.url} to {self.folder}")
27+
git.Repo.clone_from(self.url, self.folder)
28+
29+
def _update(self):
30+
repo = Repo(self.folder)
31+
repo.remotes.origin.pull(rebase=True)
32+
33+
def setup_repo(self):
34+
"""
35+
Set up a git repository at the destination.
36+
If destination already exists and override == True,
37+
nothing is done
38+
"""
39+
if not self.folder.exists():
40+
self._clone_repo()
41+
return
42+
43+
logging.info(f"{self.folder} already exists, updating it")
44+
self._update()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ readme = "README.md"
77
version = "0.0.1"
88
dependencies = [
99
"click",
10+
"gitpython",
1011
"pathlib3x",
1112
]
1213

0 commit comments

Comments
 (0)