mirror of
https://github.com/akuker/RASCSI.git
synced 2024-06-07 13:53:33 +00:00
Display hardware info in footer; close open file handlers; better error handling (#989)
* Expose hardware info in env string * Improve error handling for get_properties_by_drive_name * Verbose uname parameters. * Close open file in introspect method * Add error handling for when no drive name was matched with props in the database
This commit is contained in:
parent
3627b39af4
commit
4645e8c975
|
@ -7,6 +7,7 @@ from subprocess import run
|
||||||
from shutil import disk_usage
|
from shutil import disk_usage
|
||||||
from re import findall, match
|
from re import findall, match
|
||||||
from socket import socket, gethostname, AF_INET, SOCK_DGRAM
|
from socket import socket, gethostname, AF_INET, SOCK_DGRAM
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from rascsi.common_settings import SHELL_ERROR
|
from rascsi.common_settings import SHELL_ERROR
|
||||||
|
|
||||||
|
@ -37,9 +38,9 @@ class SysCmds:
|
||||||
ra_git_version = ""
|
ra_git_version = ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pi_version = (
|
os_version = (
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["uname", "-a"],
|
["uname", "--kernel-name", "--kernel-release", "--machine"],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
check=True,
|
check=True,
|
||||||
)
|
)
|
||||||
|
@ -48,9 +49,35 @@ class SysCmds:
|
||||||
)
|
)
|
||||||
except subprocess.CalledProcessError as error:
|
except subprocess.CalledProcessError as error:
|
||||||
logging.warning(SHELL_ERROR, " ".join(error.cmd), error.stderr.decode("utf-8"))
|
logging.warning(SHELL_ERROR, " ".join(error.cmd), error.stderr.decode("utf-8"))
|
||||||
pi_version = "?"
|
os_version = "Unknown OS"
|
||||||
|
|
||||||
return {"git": ra_git_version, "env": pi_version}
|
PROC_MODEL_PATH = "/proc/device-tree/model"
|
||||||
|
SYS_VENDOR_PATH = "/sys/devices/virtual/dmi/id/sys_vendor"
|
||||||
|
SYS_PROD_PATH = "/sys/devices/virtual/dmi/id/product_name"
|
||||||
|
# First we try to get the Pi model
|
||||||
|
if Path(PROC_MODEL_PATH).is_file():
|
||||||
|
try:
|
||||||
|
with open(PROC_MODEL_PATH, "r") as open_file:
|
||||||
|
hardware = open_file.read().rstrip()
|
||||||
|
except (IOError, ValueError, EOFError, TypeError) as error:
|
||||||
|
logging.error(str(error))
|
||||||
|
# As a fallback, look for PC vendor information
|
||||||
|
elif Path(SYS_VENDOR_PATH).is_file() and Path(SYS_PROD_PATH).is_file():
|
||||||
|
hardware = ""
|
||||||
|
try:
|
||||||
|
with open(SYS_VENDOR_PATH, "r") as open_file:
|
||||||
|
hardware = open_file.read().rstrip() + " "
|
||||||
|
except (IOError, ValueError, EOFError, TypeError) as error:
|
||||||
|
logging.error(str(error))
|
||||||
|
try:
|
||||||
|
with open(SYS_PROD_PATH, "r") as open_file:
|
||||||
|
hardware = hardware + open_file.read().rstrip()
|
||||||
|
except (IOError, ValueError, EOFError, TypeError) as error:
|
||||||
|
logging.error(str(error))
|
||||||
|
else:
|
||||||
|
hardware = "Unknown Device"
|
||||||
|
|
||||||
|
return {"git": ra_git_version, "env": f"{hardware}, {os_version}" }
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def running_proc(daemon):
|
def running_proc(daemon):
|
||||||
|
@ -114,15 +141,18 @@ class SysCmds:
|
||||||
Will introspect file_path for the existance of re_term
|
Will introspect file_path for the existance of re_term
|
||||||
and return True if found, False if not found
|
and return True if found, False if not found
|
||||||
"""
|
"""
|
||||||
|
result = False
|
||||||
try:
|
try:
|
||||||
ifile = open(file_path, "r", encoding="ISO-8859-1")
|
ifile = open(file_path, "r", encoding="ISO-8859-1")
|
||||||
|
for line in ifile:
|
||||||
|
if match(re_term, line):
|
||||||
|
result = True
|
||||||
|
break
|
||||||
except (IOError, ValueError, EOFError, TypeError) as error:
|
except (IOError, ValueError, EOFError, TypeError) as error:
|
||||||
logging.error(str(error))
|
logging.error(str(error))
|
||||||
return False
|
finally:
|
||||||
for line in ifile:
|
ifile.close()
|
||||||
if match(re_term, line):
|
return result
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
# pylint: disable=broad-except
|
# pylint: disable=broad-except
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -129,7 +129,7 @@
|
||||||
{{ _("RaSCSI Reloaded version: ") }}<b>{{ env["version"] }} <a href="https://github.com/akuker/RASCSI/commit/{{ env["running_env"]["git"] }}" target="_blank">{{ env["running_env"]["git"][:7] }}</a></b>
|
{{ _("RaSCSI Reloaded version: ") }}<b>{{ env["version"] }} <a href="https://github.com/akuker/RASCSI/commit/{{ env["running_env"]["git"] }}" target="_blank">{{ env["running_env"]["git"][:7] }}</a></b>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
{{ _("Pi environment: ") }}{{ env["running_env"]["env"] }}
|
{{ _("Hardware and OS: ") }}{{ env["running_env"]["env"] }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -361,6 +361,12 @@ def drive_create():
|
||||||
drive_name
|
drive_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not properties:
|
||||||
|
return response(
|
||||||
|
error=True,
|
||||||
|
message=_("No properties data for drive %(drive_name)s", drive_name=drive_name),
|
||||||
|
)
|
||||||
|
|
||||||
# Creating the image file
|
# Creating the image file
|
||||||
process = file_cmd.create_new_image(
|
process = file_cmd.create_new_image(
|
||||||
file_name,
|
file_name,
|
||||||
|
@ -398,6 +404,13 @@ def drive_cdrom():
|
||||||
APP.config["RASCSI_DRIVE_PROPERTIES"],
|
APP.config["RASCSI_DRIVE_PROPERTIES"],
|
||||||
drive_name
|
drive_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not properties:
|
||||||
|
return response(
|
||||||
|
error=True,
|
||||||
|
message=_("No properties data for drive %(drive_name)s", drive_name=drive_name),
|
||||||
|
)
|
||||||
|
|
||||||
process = file_cmd.write_drive_properties(file_name, properties)
|
process = file_cmd.write_drive_properties(file_name, properties)
|
||||||
process = ReturnCodeMapper.add_msg(process)
|
process = ReturnCodeMapper.add_msg(process)
|
||||||
if process["status"]:
|
if process["status"]:
|
||||||
|
@ -1007,21 +1020,22 @@ def create_file():
|
||||||
APP.config["RASCSI_DRIVE_PROPERTIES"],
|
APP.config["RASCSI_DRIVE_PROPERTIES"],
|
||||||
drive_name
|
drive_name
|
||||||
)
|
)
|
||||||
prop_file_name = f"{full_file_name}.{PROPERTIES_SUFFIX}"
|
if properties:
|
||||||
process = file_cmd.write_drive_properties(prop_file_name, properties)
|
prop_file_name = f"{full_file_name}.{PROPERTIES_SUFFIX}"
|
||||||
process = ReturnCodeMapper.add_msg(process)
|
process = file_cmd.write_drive_properties(prop_file_name, properties)
|
||||||
if not process["status"]:
|
process = ReturnCodeMapper.add_msg(process)
|
||||||
return response(error=True, message=process["msg"])
|
if not process["status"]:
|
||||||
|
return response(error=True, message=process["msg"])
|
||||||
|
|
||||||
return response(
|
return response(
|
||||||
status_code=201,
|
status_code=201,
|
||||||
message=_(
|
message=_(
|
||||||
"Image file with properties created: %(file_name)s%(drive_format)s",
|
"Image file with properties created: %(file_name)s%(drive_format)s",
|
||||||
file_name=full_file_name,
|
file_name=full_file_name,
|
||||||
drive_format=message_postfix,
|
drive_format=message_postfix,
|
||||||
),
|
),
|
||||||
image=full_file_name,
|
image=full_file_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
return response(
|
return response(
|
||||||
status_code=201,
|
status_code=201,
|
||||||
|
|
|
@ -189,19 +189,19 @@ def get_properties_by_drive_name(drives, drive_name):
|
||||||
"""
|
"""
|
||||||
drives.sort(key=lambda item: item.get("name"))
|
drives.sort(key=lambda item: item.get("name"))
|
||||||
|
|
||||||
drive_props = None
|
|
||||||
for drive in drives:
|
for drive in drives:
|
||||||
if drive["name"] == drive_name:
|
if drive["name"] == drive_name:
|
||||||
drive_props = drive
|
return {
|
||||||
|
"file_type": drive["file_type"],
|
||||||
|
"vendor": drive["vendor"],
|
||||||
|
"product": drive["product"],
|
||||||
|
"revision": drive["revision"],
|
||||||
|
"block_size": drive["block_size"],
|
||||||
|
"size": drive["size"],
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
logging.error("Properties for drive '%s' does not exist in database", drive_name)
|
||||||
"file_type": drive_props["file_type"],
|
return False
|
||||||
"vendor": drive_props["vendor"],
|
|
||||||
"product": drive_props["product"],
|
|
||||||
"revision": drive_props["revision"],
|
|
||||||
"block_size": drive_props["block_size"],
|
|
||||||
"size": drive_props["size"],
|
|
||||||
}
|
|
||||||
|
|
||||||
def auth_active(group):
|
def auth_active(group):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user