Skip to content

Commit 1657fc2

Browse files
committed
Check platform
1 parent 3c62e51 commit 1657fc2

File tree

5 files changed

+92
-64
lines changed

5 files changed

+92
-64
lines changed

picsi/commands/build.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
__all__ = ["build"]
22

3-
from halo import Halo
3+
44
from pathlib import Path
5+
from sys import exit as sys_exit
6+
7+
from halo import Halo
58

69
from picsi.vendored.mkmanifest import mkmanifest
710
from picsi.vendored.run_commands import run_commands
811
from picsi.vendored.get_output import get_output
912
from picsi.vendored.get_uname import get_uname
1013
from picsi.vendored.ci_resolve_proxy import ci_resolve_proxy
14+
from picsi.vendored.check_platform import check_platform
1115

1216

1317
def build(
@@ -20,6 +24,11 @@ def build(
2024
Build Nexmon_CSI from source
2125
"""
2226

27+
if not check_platform():
28+
confirmation = input("Unsupported platform. Do you want to continue? [Y/n]: ")
29+
if confirmation.lower() in ["n", "no"]:
30+
sys_exit(1)
31+
2332
Path("/home/pi/.picsi/").mkdir(exist_ok=True)
2433

2534
path_nexmon = Path("/home/pi/.picsi/nexmon")

picsi/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pathlib import Path
2+
from tomli import load
3+
4+
path_config = Path(__file__).parent / "config.toml"
5+
6+
with open(path_config, "rb") as config_file:
7+
config = load(config_file)

picsi/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
supported_kernels = ["4.19", "5.4", "5.10"]
2+
supported_archs = ["armv7l"]
3+

picsi/picsi.py

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,26 @@
11
#!/usr/bin/env python3
2+
from os import getuid
3+
from sys import argv
4+
from sys import exit as sys_exit
5+
from subprocess import run
6+
from importlib import import_module
27

3-
import os
4-
import sys
5-
6-
# Elevate picsi to root if not running as root
7-
if os.getuid() != 0:
8-
import subprocess
9-
from picsi.vendored.get_output import get_output
10-
11-
executable = get_output(["which", "picsi"])
12-
arguments = sys.argv[1:]
13-
14-
p = subprocess.run(["sudo", "-E", executable] + sys.argv[1:])
15-
16-
sys.exit(p.returncode)
17-
else:
18-
from picsi.vendored.get_output import get_output
19-
20-
get_output(["chown", "-R", "pi:pi", "/home/pi/.picsi"])
21-
22-
# ---------------------------------------------------------------
23-
24-
import typer
8+
from typer import Typer
259
from click import Group
2610

27-
from picsi.commands.install import install
28-
from picsi.commands.uninstall import uninstall
29-
from picsi.commands.enable import enable
30-
from picsi.commands.disable import disable
31-
from picsi.commands.build import build
32-
from picsi.commands.up import up
33-
from picsi.commands.down import down
34-
from picsi.commands.status import status
35-
from picsi.vendored.get_uname import get_uname
11+
from picsi.vendored.get_output import get_output
3612

37-
# Check if picsi can run on this kernel
38-
supported_kernel_versions = ["4.19", "5.4", "5.10"]
39-
current_kernel_version = ".".join(get_uname("-r").split(".")[:2])
4013

41-
if current_kernel_version not in supported_kernel_versions:
42-
print(
43-
"Warning: Nexmon_csi is only available for kernel versions: "
44-
+ ", ".join(supported_kernel_versions)
45-
+ ".\n"
46-
+ "You are running Kernel version "
47-
+ current_kernel_version
48-
+ "."
49-
)
14+
if getuid() != 0:
15+
# If not running as root
16+
executable = get_output(["which", "picsi"])
5017

51-
supported_cpu_architectures = ["armv7l", "armv7l+"]
52-
current_cpu_architecture = get_uname("-m")
18+
# Start picsi as root
19+
p = run(["sudo", "-E", executable] + argv[1:])
5320

54-
if current_cpu_architecture not in supported_cpu_architectures:
55-
print(
56-
"Warning: Nexmon_csi is only available for architectures: "
57-
+ ", ".join(supported_cpu_architectures)
58-
+ ".\n"
59-
+ "You are running Kernel version "
60-
+ current_kernel_version
61-
+ "."
62-
)
21+
sys_exit(p.returncode)
22+
# else:
23+
# get_output(["chown", "-R", "pi:pi", "/home/pi/.picsi"])
6324

6425

6526
# Typer prints commands in the help menu
@@ -70,16 +31,22 @@ def list_commands(self, ctx):
7031
return self.commands.keys()
7132

7233

73-
app = typer.Typer(cls=NaturalOrderGroup)
34+
commands = [
35+
"install",
36+
"uninstall",
37+
"enable",
38+
"disable",
39+
"up",
40+
"down",
41+
"status",
42+
"build",
43+
]
44+
45+
app = Typer(cls=NaturalOrderGroup)
46+
for command in commands:
47+
module = import_module(f"picsi.commands.{command}")
7448

75-
app.command()(install)
76-
app.command()(uninstall)
77-
app.command()(enable)
78-
app.command()(disable)
79-
app.command()(up)
80-
app.command()(down)
81-
app.command()(status)
82-
app.command()(build)
49+
app.command()(getattr(module, command))
8350

8451
if __name__ == "__main__":
8552
app()

picsi/vendored/check_platform.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from picsi.vendored.get_uname import get_uname
2+
from picsi.config import config
3+
4+
5+
def check_kernel_version():
6+
current_kernel_version = ".".join(get_uname("-r").split(".")[:2])
7+
8+
if current_kernel_version in config.supported_kernels:
9+
return True
10+
11+
print(
12+
"Warning: Nexmon_csi is only available for kernel versions: "
13+
+ ", ".join(config.supported_kernels)
14+
+ ".\n"
15+
+ "You are running Kernel version "
16+
+ current_kernel_version
17+
+ "."
18+
)
19+
20+
return False
21+
22+
23+
def check_cpu_arch():
24+
current_cpu_architecture = get_uname("-m")
25+
26+
if current_cpu_architecture in config.supported_archs:
27+
return True
28+
29+
print(
30+
"Warning: Nexmon_csi is only available for architectures: "
31+
+ ", ".join(config.supported_archs)
32+
+ ".\n"
33+
+ "Your current architecture is "
34+
+ current_cpu_architecture
35+
+ "."
36+
)
37+
38+
return False
39+
40+
41+
def check_platform():
42+
return check_cpu_arch() and check_kernel_version()

0 commit comments

Comments
 (0)