-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsystem.py
123 lines (96 loc) · 3.79 KB
/
system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import shared
import whiptail
import json
class SystemData:
def __init__(self):
self.config = None
self.registries = {}
self.mounts = None
shared.logger.step("System Registry")
self.build = {
"number": open("/buildct").read().replace("\n", ""),
"time": open("/buildtm").read().replace("\n", "")}
self.reload(quiet=False, initial=True)
shared.logger.substep("All Done")
def reload(self, quiet=False, initial=False):
if initial:
shared.logger.substep("Read registry mounts configuration")
self.mounts = shared.jsonLoad("/data/.config/registry.json", silent=quiet)["mounts"]
if initial:
shared.logger.substep("Mount system registries")
for mount in self.mounts:
if initial:
shared.logger.substep(
f"Mount system registry {mount['path']} at /{mount['point']}")
self.registries[mount["point"]] = shared.jsonLoad(mount["path"], silent=quiet)
self.registries["build"]= {
"number": open("/buildct").read().replace("\n", ""),
"time": open("/buildtm").read().replace("\n", "")}
def init(self):
shared.logger.warn(
"SystemData.init is depricated! Please use SystemData.reload instead!")
def set(self, location, value, save=True):
try:
location_parts = [i.replace("/", "") for i in location.split("/") if i.strip() != ""]
current_location = self.registries
for part in location_parts[:-1]:
try:
part = int(part)
except ValueError:
pass
if part not in current_location:
current_location[part] = {}
current_location = current_location[part]
final_part = location_parts[-1]
try:
final_part = int(final_part)
except ValueError:
pass
current_location[final_part] = value
except Exception as e:
shared.logger.error("Cannot set system key <{0}>".format(location))
if save:
self.save()
def save(self):
for mount in self.mounts:
shared.jsonSave(mount["path"], self.registries[mount["point"]])
def get(self, location):
try:
location_parts = [i.replace("/", "") for i in location.split("/") if i.strip() != ""]
current_location = ""
try:
current_location = self.registries[location_parts[0]]
except NameError:
shared.logger.error("No base location {0}" % location_parts[0])
return None
del location_parts[0]
for location in location_parts:
try:
location = int(location)
except ValueError:
pass # ok, can't do that!
current_location = current_location[location]
return current_location
except Exception as e:
shared.logger.error("Cannot get system key <{0}>".format(location))
return
class SystemHandler:
def __init__(self):
pass
def system(self, ctx, args):
del args[0]
if len(args) == 0:
print("Usage: system <command> <subcommand>")
print("| Command | Subcommands")
print("+-----------+------------")
print("| passwd | change,forcereset")
return 0
args[0] = args[0].lower()
if args[0] == "passwd":
if len(args) == 2:
args[1] = args[1].lower()
if args[1] == "forcereset":
pass
data = SystemData()
data.init()
handler = SystemHandler()