Feature config dir (#299)

* Define a separate config directory

* Create config dir when installing

* Fix typo

* Introduce delete_file method for deleting non-image files

* Add docstring
This commit is contained in:
Daniel Markstedt 2021-10-05 18:58:49 -07:00 committed by GitHub
parent 8f09f97d30
commit 7717890b5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 20 deletions

View File

@ -47,6 +47,7 @@ echo -e $logo
}
VIRTUAL_DRIVER_PATH=/home/pi/images
CFG_PATH="$HOME/.config/rascsi"
HFS_FORMAT=/usr/bin/hformat
HFDISK_BIN=/usr/bin/hfdisk
LIDO_DRIVER=~/RASCSI/lido-driver.img
@ -141,6 +142,14 @@ function createImagesDir() {
mkdir -p $VIRTUAL_DRIVER_PATH
chmod -R 775 $VIRTUAL_DRIVER_PATH
fi
if [ -d $CFG_PATH ]; then
echo "The $CFG_PATH directory already exists."
else
echo "The $CFG_PATH directory does not exist; creating..."
mkdir -p $CFG_PATH
chmod -R 775 $CFG_PATH
fi
}
function stopOldWebInterface() {

View File

@ -11,14 +11,14 @@ from settings import *
import rascsi_interface_pb2 as proto
def list_files(file_types):
def list_files(file_types, dir_path):
"""
Takes a list or tuple of str file_types - e.g. ('hda', 'hds')
Returns list of lists files_list:
index 0 is str file name and index 1 is int size in bytes
"""
files_list = []
for path, dirs, files in os.walk(base_dir):
for path, dirs, files in os.walk(dir_path):
# Only list selected file types
files = [f for f in files if f.lower().endswith(file_types)]
files_list.extend(
@ -35,11 +35,11 @@ def list_files(file_types):
def list_config_files():
"""
Returns a list of RaSCSI config files in base_dir:
Returns a list of RaSCSI config files in cfg_dir:
list of str files_list
"""
files_list = []
for root, dirs, files in os.walk(base_dir):
for root, dirs, files in os.walk(cfg_dir):
for file in files:
if file.endswith(".json"):
files_list.append(file)
@ -59,9 +59,9 @@ def list_images():
result = proto.PbResult()
result.ParseFromString(data)
# Get a list of all *.properties files in base_dir
# Get a list of all *.properties files in cfg_dir
from pathlib import PurePath
prop_data = list_files(PROPERTIES_SUFFIX)
prop_data = list_files(PROPERTIES_SUFFIX, cfg_dir)
prop_files = [PurePath(x[0]).stem for x in prop_data]
files = []
@ -97,7 +97,7 @@ def create_new_image(file_name, file_type, size):
return {"status": result.status, "msg": result.msg}
def delete_file(file_name):
def delete_image(file_name):
"""
Takes str file_name
Sends a DELETE_IMAGE command to the server
@ -114,6 +114,18 @@ def delete_file(file_name):
return {"status": result.status, "msg": result.msg}
def delete_file(file_path):
"""
Takes str file_path with the full path to the file to delete
Returns dict with boolean status and str msg
"""
if os.path.exists(file_path):
os.remove(file_path)
return {"status": True, "msg": "File deleted"}
else:
return {"status": False, "msg": "Could not delete file"}
def unzip_file(file_name):
"""
Takes str file_name
@ -194,7 +206,7 @@ def write_config(file_name):
Returns dict with boolean status and str msg
"""
from json import dump
file_name = base_dir + file_name
file_name = cfg_dir + file_name
try:
with open(file_name, "w") as json_file:
devices = list_devices()["device_list"]
@ -229,7 +241,7 @@ def read_config(file_name):
Returns dict with boolean status and str msg
"""
from json import load
file_name = base_dir + file_name
file_name = cfg_dir + file_name
try:
with open(file_name) as json_file:
detach_all()
@ -263,7 +275,7 @@ def write_drive_properties(file_name, conf):
"""
from json import dump
try:
with open(base_dir + file_name, "w") as json_file:
with open(cfg_dir + file_name, "w") as json_file:
dump(conf, json_file, indent=4)
return {"status": True, "msg": f"Successfully wrote to file: {file_name}"}
except (IOError, ValueError, EOFError, TypeError) as e:

View File

@ -1,6 +1,7 @@
from os import getenv, getcwd
base_dir = getenv("BASE_DIR", "/home/pi/images/")
cfg_dir = getenv("HOME", "/home/pi/") + ".config/rascsi/"
home_dir = getcwd()
DEFAULT_CONFIG = "default.json"

View File

@ -5,7 +5,7 @@
<summary>Current RaSCSI Configuration</summary>
<ul>
<li>Displays the currently attached devices for each available SCSI ID.</li>
<li>Save and load device configurations into <tt>{{base_dir}}</tt></li>
<li>Save and load device configurations into <tt>{{cfg_dir}}</tt></li>
<li>The <em>default</em> configuration will be loaded when the Web UI starts up, if available.</li>
</ul>
</details>

View File

@ -16,6 +16,7 @@ from file_cmds import (
list_images,
create_new_image,
download_file_to_iso,
delete_image,
delete_file,
unzip_file,
download_image,
@ -84,6 +85,7 @@ def index():
files=sorted_image_files,
config_files=sorted_config_files,
base_dir=base_dir,
cfg_dir=cfg_dir,
scsi_ids=scsi_ids,
reserved_scsi_ids=reserved_scsi_ids,
max_file_size=int(MAX_FILE_SIZE / 1024 / 1024),
@ -250,10 +252,9 @@ def config_load():
flash(process['msg'], "error")
return redirect(url_for("index"))
elif "delete" in request.form:
process = delete_file(file_name)
process = delete_file(cfg_dir + file_name)
if process["status"] == True:
flash(f"Deleted config {file_name}!")
flash(process["msg"])
return redirect(url_for("index"))
else:
flash(f"Failed to delete {file_name}!", "error")
@ -346,7 +347,7 @@ def attach():
# same base path but PROPERTIES_SUFFIX instead of the original suffix.
from pathlib import Path
file_name_base = str(Path(file_name).stem)
drive_properties = Path(base_dir + file_name_base + "." + PROPERTIES_SUFFIX)
drive_properties = Path(cfg_dir + file_name_base + "." + PROPERTIES_SUFFIX)
if drive_properties.is_file():
process = read_drive_properties(str(drive_properties))
if process["status"] == False:
@ -568,7 +569,7 @@ def download():
def delete():
file_name = request.form.get("image")
process = delete_file(file_name)
process = delete_image(file_name)
if process["status"] == True:
flash(f"File {file_name} deleted!")
flash(process["msg"])
@ -580,12 +581,11 @@ def delete():
# Delete the drive properties file, if it exists
from pathlib import Path
file_name = str(Path(file_name).stem) + "." + PROPERTIES_SUFFIX
file_path = Path(base_dir + file_name)
file_path = Path(cfg_dir + file_name)
if file_path.is_file():
process = delete_file(file_name)
process = delete_file(cfg_dir + file_name)
if process["status"] == True:
flash(f"File {file_name} deleted!")
flash(process["msg"])
return redirect(url_for("index"))
else:
flash(f"Failed to delete file {file_name}!", "error")
@ -601,7 +601,7 @@ def show_properties():
from pathlib import PurePath
file_name = str(PurePath(file_name).stem) + "." + PROPERTIES_SUFFIX
process = read_drive_properties(base_dir + file_name)
process = read_drive_properties(cfg_dir + file_name)
prop = process["conf"]
if process["status"]:
@ -628,7 +628,7 @@ if __name__ == "__main__":
# Load the default configuration file, if found
from pathlib import Path
default_config_path = Path(base_dir + DEFAULT_CONFIG)
default_config_path = Path(cfg_dir + DEFAULT_CONFIG)
if default_config_path.is_file():
read_config(DEFAULT_CONFIG)