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:
authorAnton Hvornum <anton@hvornum.se>2022-01-06 22:01:15 +0100
committerGitHub <noreply@github.com>2022-01-06 22:01:15 +0100
commite32cf71ae7dacbf9674262705cb2e8e1a5a2d206 (patch)
treed33e6202d10813221a9935dcdc49145f6ffca83e /archinstall/lib/general.py
parent015cd2a59fbdf3316ddfb7546b884157ad00c7fe (diff)
Added type annotations to all functions (#845)
* Added type annotations for 1/5 of the files. There's bound to be some issues with type miss-match, will sort that out later. * Added type hints for 4/5 of the code * Added type hints for 4.7/5 of the code * Added type hints for 5/5 of the code base * Split the linters into individual files This should help with more clearly show which runner is breaking since they don't share a single common name any longer. Also moved mypy settings into pyproject.toml * Fixed some of the last flake8 issues * Missing parameter * Fixed invalid lookahead types * __future__ had to be at the top * Fixed last flake8 issues
Diffstat (limited to 'archinstall/lib/general.py')
-rw-r--r--archinstall/lib/general.py45
1 files changed, 28 insertions, 17 deletions
diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py
index cc50e80a..96c9d50c 100644
--- a/archinstall/lib/general.py
+++ b/archinstall/lib/general.py
@@ -1,3 +1,4 @@
+from __future__ import annotations
import hashlib
import json
import logging
@@ -9,7 +10,10 @@ import string
import sys
import time
from datetime import datetime, date
-from typing import Callable, Optional, Dict, Any, List, Union, Iterator
+from typing import Callable, Optional, Dict, Any, List, Union, Iterator, TYPE_CHECKING
+# https://stackoverflow.com/a/39757388/929999
+if TYPE_CHECKING:
+ from .installer import Installer
if sys.platform == 'linux':
from select import epoll, EPOLLIN, EPOLLHUP
@@ -46,14 +50,14 @@ from .exceptions import RequirementError, SysCallError
from .output import log
from .storage import storage
-def gen_uid(entropy_length=256):
+def gen_uid(entropy_length :int = 256) -> str:
return hashlib.sha512(os.urandom(entropy_length)).hexdigest()
-def generate_password(length=64):
+def generate_password(length :int = 64) -> str:
haystack = string.printable # digits, ascii_letters, punctiation (!"#$[] etc) and whitespace
return ''.join(secrets.choice(haystack) for i in range(length))
-def multisplit(s, splitters):
+def multisplit(s :str, splitters :List[str]) -> str:
s = [s, ]
for key in splitters:
ns = []
@@ -77,12 +81,12 @@ def locate_binary(name :str) -> str:
raise RequirementError(f"Binary {name} does not exist.")
-def json_dumps(*args, **kwargs):
+def json_dumps(*args :str, **kwargs :str) -> str:
return json.dumps(*args, **{**kwargs, 'cls': JSON})
class JsonEncoder:
@staticmethod
- def _encode(obj):
+ def _encode(obj :Any) -> Any:
"""
This JSON encoder function will try it's best to convert
any archinstall data structures, instances or variables into
@@ -119,7 +123,7 @@ class JsonEncoder:
return obj
@staticmethod
- def _unsafe_encode(obj):
+ def _unsafe_encode(obj :Any) -> Any:
"""
Same as _encode() but it keeps dictionary keys starting with !
"""
@@ -141,20 +145,20 @@ class JSON(json.JSONEncoder, json.JSONDecoder):
"""
A safe JSON encoder that will omit private information in dicts (starting with !)
"""
- def _encode(self, obj):
+ def _encode(self, obj :Any) -> Any:
return JsonEncoder._encode(obj)
- def encode(self, obj):
+ def encode(self, obj :Any) -> Any:
return super(JSON, self).encode(self._encode(obj))
class UNSAFE_JSON(json.JSONEncoder, json.JSONDecoder):
"""
UNSAFE_JSON will call/encode and keep private information in dicts (starting with !)
"""
- def _encode(self, obj):
+ def _encode(self, obj :Any) -> Any:
return JsonEncoder._unsafe_encode(obj)
- def encode(self, obj):
+ def encode(self, obj :Any) -> Any:
return super(UNSAFE_JSON, self).encode(self._encode(obj))
class SysCommandWorker:
@@ -455,12 +459,16 @@ class SysCommand:
return None
-def prerequisite_check():
- if not os.path.isdir("/sys/firmware/efi"):
- raise RequirementError("Archinstall only supports machines in UEFI mode.")
+def prerequisite_check() -> bool:
+ """
+ This function is used as a safety check before
+ continuing with an installation.
- return True
+ Could be anything from checking that /boot is big enough
+ to check if nvidia hardware exists when nvidia driver was chosen.
+ """
+ return True
def reboot():
SysCommand("/usr/bin/reboot")
@@ -473,12 +481,15 @@ def pid_exists(pid: int) -> bool:
return False
-def run_custom_user_commands(commands, installation):
+def run_custom_user_commands(commands :List[str], installation :Installer) -> None:
for index, command in enumerate(commands):
- log(f'Executing custom command "{command}" ...', fg='yellow')
+ log(f'Executing custom command "{command}" ...', level=logging.INFO)
+
with open(f"{installation.target}/var/tmp/user-command.{index}.sh", "w") as temp_script:
temp_script.write(command)
+
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")