Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/general.py
diff options
context:
space:
mode:
authorWerner Llácer <wllacer@gmail.com>2022-01-02 12:28:49 +0100
committerGitHub <noreply@github.com>2022-01-02 12:28:49 +0100
commitb89408ab7ba89fe03b62c9ae7f3d32ce74ebc035 (patch)
tree6b539a4488b5fdd244ca21b6926de1c5f67e2f64 /archinstall/lib/general.py
parente388537bc36534fe782e1e68a70ddcdb2e13e99a (diff)
Improved command line argument parsing (#725)
* An update to PR 715, making the handling of the *--mount-point* parameter less error prone. I added a synomym (accepting the name both with underscore and dash) and ignoring when no value specified I added it explicitly to the list to accept both the --parm value and --parm=value syntax DOES NOT check the contents of the parameter * Explicitly set all the know parameters * Define explictly all parameters. Make all non explicitly defined parameters behave as standard parameters, with on exception, names are not changed Some cleanup of the code In guided.py the reference to the dry_run parameter is updated to the standard naming convention for parameters * Linter with flake8. corrections * Linter with flake8. corrections (II) * Linter with flake8. corrections (and III) * Added --disk_layout argument. Was missing I moved its loading from guided.py to __init__.py as it happens to the other json related arguments * Better handling of errors during processing of the --disk_layouts parameter. I define a routine to read an store a JSON file or stream. Tested on disk_layout * Expand the former commit to all JSON file arguments * Moved the function we created to read json files/streams to general.py. Add some comments * flake8. A reference now unneded * The merge process for the dry-run argument was causing the issue, not solving it The del is just a cleanup for version upgrade without consequence (I hope) * flake8 warning * Correcting the last correction . Worked for old config files, but only for them * New parameter parsing algorithm. More flexible and accepts multiple arguments (optionallY) plus some documentation effort * flake8 warning. For once is significant ( != None to not None)
Diffstat (limited to 'archinstall/lib/general.py')
-rw-r--r--archinstall/lib/general.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py
index 7c8f8ea3..cc50e80a 100644
--- a/archinstall/lib/general.py
+++ b/archinstall/lib/general.py
@@ -481,3 +481,31 @@ def run_custom_user_commands(commands, installation):
execution_output = SysCommand(f"arch-chroot {installation.target} bash /var/tmp/user-command.{index}.sh")
log(execution_output)
os.unlink(f"{installation.target}/var/tmp/user-command.{index}.sh")
+
+def json_stream_to_structure(id : str, stream :str, target :dict) -> bool :
+ """ Function to load a stream (file (as name) or valid JSON string into an existing dictionary
+ Returns true if it could be done
+ Return false if operation could not be executed
+ +id is just a parameter to get meaningful, but not so long messages
+ """
+ from pathlib import Path
+ if Path(stream).exists():
+ try:
+ with open(Path(stream)) as fh:
+ target.update(json.load(fh))
+ except Exception as e:
+ log(f"{id} = {stream} does not contain a valid JSON format: {e}",level=logging.ERROR)
+ return False
+ else:
+ log(f"{id} = {stream} does not exists in the filesystem. Trying as JSON stream",level=logging.DEBUG)
+ # NOTE: failure of this check doesn't make stream 'real' invalid JSON, just it first level entry is not an object (i.e. dict), so it is not a format we handle.
+ if stream.strip().startswith('{') and stream.strip().endswith('}'):
+ try:
+ target.update(json.loads(stream))
+ except Exception as e:
+ log(f" {id} Contains an invalid JSON format : {e}",level=logging.ERROR)
+ return False
+ else:
+ log(f" {id} is neither a file nor is a JSON string:",level=logging.ERROR)
+ return False
+ return True