Skip to content

Commit 34121b6

Browse files
committed
black format, update docstrings and return types
Signed-off-by: Zen <[email protected]>
1 parent 16aabf7 commit 34121b6

File tree

10 files changed

+356
-347
lines changed

10 files changed

+356
-347
lines changed

src/ugrd/base/banner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
__author__ = 'desultory'
2-
__version__ = '0.1.0'
1+
__author__ = "desultory"
2+
__version__ = "0.1.0"
33

44

55
def print_banner(self) -> list[str]:
6-
""" Prints the banner. Prints the kernel version if set """
6+
"""Prints the banner. Prints the kernel version if set"""
77
banner = [self.banner]
8-
if kver := self.get('kernel_version'):
8+
if kver := self.get("kernel_version"):
99
banner.append(f"einfo 'Kernel version: {kver}'")
1010
return banner

src/ugrd/base/base.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,15 @@ def setvar(self) -> str:
153153

154154

155155
def readvar(self) -> str:
156-
"""
157-
Returns a bash function that reads a variable from /run/vars/{name}.
156+
"""Returns a bash function that reads a variable from /run/vars/{name}.
158157
The second arg can be a default value.
159158
If no default is supplied, and the variable is not found, it returns an empty string.
160159
"""
161160
return 'cat "/run/vars/${1}" 2>/dev/null || echo "${2}"'
162161

163162

164-
def check_var(self) -> str:
165-
"""
166-
Returns a bash function that checks the value of a variable.
163+
def check_var(self) -> list[str]:
164+
"""Returns a bash function that checks the value of a variable.
167165
if it's not set, tries to read the cmdline.
168166
"""
169167
return [
@@ -181,9 +179,8 @@ def check_var(self) -> str:
181179
]
182180

183181

184-
def prompt_user(self) -> str:
185-
"""
186-
Returns a bash function that pauses until the user presses enter.
182+
def prompt_user(self) -> list[str]:
183+
"""Returns a bash function that pauses until the user presses enter.
187184
The first argument is the prompt message.
188185
The second argument is the timeout in seconds.
189186
@@ -210,9 +207,8 @@ def prompt_user(self) -> str:
210207
return output
211208

212209

213-
def retry(self) -> str:
214-
"""
215-
Returns a bash function that retries a command some number of times.
210+
def retry(self) -> list[str]:
211+
"""Returns a bash function that retries a command some number of times.
216212
The first argument is the number of retries. if 0, it retries 100 times.
217213
The second argument is the timeout in seconds.
218214
The remaining arguments represent the command to run.
@@ -241,7 +237,7 @@ def retry(self) -> str:
241237

242238

243239
# To feel more at home
244-
def edebug(self) -> str:
240+
def edebug(self) -> list[str]:
245241
"""Returns a bash function like edebug."""
246242
return [
247243
"if check_var quiet; then",
@@ -254,12 +250,12 @@ def edebug(self) -> str:
254250
]
255251

256252

257-
def einfo(self) -> str:
253+
def einfo(self) -> list[str]:
258254
"""Returns a bash function like einfo."""
259255
return ["if check_var quiet; then", " return", "fi", r'echo -e "\e[1;32m *\e[0m ${*}"']
260256

261257

262-
def ewarn(self) -> str:
258+
def ewarn(self) -> list[str]:
263259
"""Returns a bash function like ewarn.
264260
If plymouth is running, it displays a message instead of echoing.
265261
"""
@@ -290,7 +286,7 @@ def eerror(self) -> str:
290286
' plymouth display-message --text="Error: ${*}"',
291287
" return",
292288
"fi",
293-
r'echo -e "\e[1;31m *\e[0m ${*}"'
289+
r'echo -e "\e[1;31m *\e[0m ${*}"',
294290
]
295291
else:
296292
return [r'echo -e "\e[1;31m *\e[0m ${*}"']

src/ugrd/base/checks.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
__version__ = '0.2.1'
1+
__version__ = "0.2.1"
22

33
from zenlib.util import contains
44

55

6-
@contains('check_included_funcs', 'Skipping included funcs check', log_level=30)
6+
@contains("check_included_funcs", "Skipping included funcs check", log_level=30)
77
def check_included_funcs(self):
8-
""" Ensures required functions are included in the build dir. """
9-
bash_func_names = [func + '() {\n' for func in self.included_functions]
10-
_check_in_file(self, '/etc/profile', bash_func_names)
8+
"""Ensures required functions are included in the build dir."""
9+
bash_func_names = [func + "() {\n" for func in self.included_functions]
10+
_check_in_file(self, "/etc/profile", bash_func_names)
1111
return "All functions found in the build dir."
1212

1313

14-
@contains('check_in_file', 'Skipping in file check')
14+
@contains("check_in_file", "Skipping in file check")
1515
def check_in_file(self):
16-
""" Runs all 'check_in_file' checks. """
17-
for file, lines in self['check_in_file'].items():
16+
"""Runs all 'check_in_file' checks."""
17+
for file, lines in self["check_in_file"].items():
1818
_check_in_file(self, file, lines)
1919
return "All 'check_in_file' checks passed"
2020

2121

2222
def _check_in_file(self, file, lines):
23-
""" Checks that all lines are in the file. """
23+
"""Checks that all lines are in the file."""
2424
file = self._get_build_path(file)
2525
if not file.exists():
2626
raise ValueError("File '%s' does not exist" % file)
2727

28-
with open(file, 'r') as f:
28+
with open(file, "r") as f:
2929
file_lines = f.readlines()
3030

3131
for check_line in lines:
3232
if check_line not in file_lines:
3333
raise ValueError("Failed to find line '%s' in file '%s'" % (check_line, file))
34-

src/ugrd/base/cmdline.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,71 @@
1-
__author__ = 'desultory'
2-
__version__ = '2.4.0'
1+
__author__ = "desultory"
2+
__version__ = "2.5.0"
33

44

5-
def parse_cmdline_bool(self) -> str:
6-
"""
7-
Returns a bash script to parse a boolean value from /proc/cmdline
5+
def parse_cmdline_bool(self) -> list[str]:
6+
"""Returns a bash script to parse a boolean value from /proc/cmdline
87
The only argument is the name of the variable to be read/set
98
"""
10-
return ['edebug "Parsing cmdline bool: $1"',
11-
r'setvar "$1" "$(grep -qE "(^|\s)$1(\s|$)" /proc/cmdline && echo 1 || echo 0)"']
9+
return [
10+
'edebug "Parsing cmdline bool: $1"',
11+
r'setvar "$1" "$(grep -qE "(^|\s)$1(\s|$)" /proc/cmdline && echo 1 || echo 0)"',
12+
]
1213

1314

14-
def parse_cmdline_str(self) -> str:
15-
"""
16-
Returns a bash script to parse a string value from /proc/cmdline
15+
def parse_cmdline_str(self) -> list[str]:
16+
"""Returns a bash script to parse a string value from /proc/cmdline
1717
The only argument is the name of the variable to be read/set
1818
"""
19-
return ['edebug "Parsing cmdline string: $1"',
20-
r'val=$(grep -oP "(?<=$1=)[^\s]+" /proc/cmdline)',
21-
'if [ -n "$val" ]; then',
22-
' edebug "Parsed $1: $val"',
23-
' setvar "$1" "$val"',
24-
'fi']
19+
return [
20+
'edebug "Parsing cmdline string: $1"',
21+
r'val=$(grep -oP "(?<=$1=)[^\s]+" /proc/cmdline)',
22+
'if [ -n "$val" ]; then',
23+
' edebug "Parsed $1: $val"',
24+
' setvar "$1" "$val"',
25+
"fi",
26+
]
27+
2528

29+
def parse_cmdline(self) -> list[str]:
30+
"""Returns bash script to parse /proc/cmdline"""
31+
return [
32+
r"""cmdline=$(awk -F '--' '{print $1}' /proc/cmdline)""", # Get everything before '--'
33+
r'''setvar INIT_ARGS "$(awk -F '--' '{print $2}' /proc/cmdline)"''', # Get everything after '--'
34+
f"""for bool in {" ".join([f'"{bool}"' for bool in self['cmdline_bools']])}; do""",
35+
' parse_cmdline_bool "$bool"',
36+
"done",
37+
f"""for string in {" ".join([f'"{string}"' for string in self['cmdline_strings']])}; do""",
38+
' parse_cmdline_str "$string"',
39+
"done",
40+
'einfo "Parsed cmdline: $cmdline"',
41+
]
2642

27-
def parse_cmdline(self) -> str:
28-
""" Returns bash script to parse /proc/cmdline """
29-
return [r'''cmdline=$(awk -F '--' '{print $1}' /proc/cmdline)''', # Get everything before '--'
30-
r'''setvar INIT_ARGS "$(awk -F '--' '{print $2}' /proc/cmdline)"''', # Get everything after '--'
31-
f'''for bool in {" ".join([f'"{bool}"' for bool in self['cmdline_bools']])}; do''',
32-
' parse_cmdline_bool "$bool"',
33-
'done',
34-
f'''for string in {" ".join([f'"{string}"' for string in self['cmdline_strings']])}; do''',
35-
' parse_cmdline_str "$string"',
36-
'done',
37-
'einfo "Parsed cmdline: $cmdline"']
3843

44+
def mount_cmdline_root(self) -> list[str]:
45+
"""Returns bash script to mount root partition based on /proc/cmdline"""
46+
return [
47+
"root=$(readvar root)",
48+
'if [ -z "$root" ]; then',
49+
' edebug "No root partition specified in /proc/cmdline, falling back to mount_root"',
50+
" mount_root",
51+
" return",
52+
"fi",
53+
'roottype="$(readvar roottype auto)"',
54+
'''rootflags="$(readvar rootflags 'defaults,ro')"''',
55+
'einfo "Mounting root partition based on /proc/cmdline: $root -t $roottype -o $rootflags"',
56+
'if ! mount "$root" "$(readvar MOUNTS_ROOT_TARGET)" -t "$roottype" -o "$rootflags"; then',
57+
' eerror "Failed to mount the root partition using /proc/cmdline: $root -t $roottype -o $rootflags"',
58+
" mount_root",
59+
"fi",
60+
]
3961

40-
def mount_cmdline_root(self) -> str:
41-
""" Returns bash script to mount root partition based on /proc/cmdline """
42-
return ['root=$(readvar root)',
43-
'if [ -z "$root" ]; then',
44-
' edebug "No root partition specified in /proc/cmdline, falling back to mount_root"',
45-
' mount_root',
46-
' return',
47-
'fi',
48-
'roottype="$(readvar roottype auto)"',
49-
'''rootflags="$(readvar rootflags 'defaults,ro')"''',
50-
'einfo "Mounting root partition based on /proc/cmdline: $root -t $roottype -o $rootflags"',
51-
'if ! mount "$root" "$(readvar MOUNTS_ROOT_TARGET)" -t "$roottype" -o "$rootflags"; then',
52-
' eerror "Failed to mount the root partition using /proc/cmdline: $root -t $roottype -o $rootflags"',
53-
' mount_root',
54-
'fi']
5562

63+
def export_exports(self) -> list[str]:
64+
"""Returns a bash script exporting all exports defined in the exports key."""
65+
from importlib.metadata import PackageNotFoundError, version
5666

57-
def export_exports(self) -> list:
58-
""" Returns a bash script exporting all exports defined in the exports key. """
59-
from importlib.metadata import version, PackageNotFoundError
6067
try:
61-
self['exports']['VERSION'] = version(__package__.split('.')[0])
68+
self["exports"]["VERSION"] = version(__package__.split(".")[0])
6269
except PackageNotFoundError:
63-
self['exports']['VERSION'] = 9999
64-
return [f'setvar {key} "{value}"' for key, value in self['exports'].items()]
70+
self["exports"]["VERSION"] = 9999
71+
return [f'setvar {key} "{value}"' for key, value in self["exports"].items()]

src/ugrd/base/console.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
__author__ = "desultory"
2-
__version__ = "1.2.0"
2+
__version__ = "1.3.0"
33

44

5-
def custom_init(self) -> str:
6-
"""
7-
init override for the console module.
5+
def custom_init(self) -> list[str]:
6+
""" init override for the console module.
7+
Adds the shebang to the top of the file, runs the banner, followed by
8+
most of the main init runlevels
89
Write the main init runlevels to self._custom_init_file.
910
Returns the output of console_init which is the command to start agetty.
1011
"""
11-
custom_init_contents = [self['shebang'],
12-
f'einfo "Starting console module v{__version__}"',
13-
'print_banner',
14-
*self.generate_init_main()]
12+
custom_init_contents = [
13+
self["shebang"],
14+
f'einfo "Starting console module v{__version__}"',
15+
"print_banner",
16+
*self.generate_init_main(),
17+
]
1518

1619
return console_init(self), custom_init_contents
1720

1821

19-
def console_init(self) -> str:
20-
"""
21-
Start agetty on the primary console.
22-
Tell it to execute the _custom_init_file
22+
def console_init(self) -> list[str]:
23+
""" Returns the command to start agetty on the primary console.
2324
If the console is a serial port, set the baud rate.
2425
"""
25-
name = self['primary_console']
26-
console = self['console'][name]
26+
name = self["primary_console"]
27+
console = self["console"][name]
2728

2829
out_str = f"agetty --autologin root --login-program {self['_custom_init_file']}"
2930

30-
console_type = console.get('type', 'tty')
31-
if console_type != 'tty':
31+
console_type = console.get("type", "tty")
32+
if console_type != "tty":
3233
# This differs from usage in the man page but seems to work?
3334
out_str += f" --local-line {console['baud']}"
3435

3536
out_str += f" {name} {console_type} || rd_restart"
3637

3738
return out_str
38-

0 commit comments

Comments
 (0)