Enabling load_instructions() to set a temporary namespace, and then reverting it after the instructions are loaded. This is to temporarly override the namespace during import - enabling avoidance of triggering __name__ checks, and at the same time reverting back the namespace automatically to enable .execute() on the script (reusability of classes)

This commit is contained in:
Anton Hvornum 2021-03-21 15:04:23 +01:00
parent 27bde44b8d
commit 9ee6479701
No known key found for this signature in database
GPG Key ID: F1234C5BA67C59DF
2 changed files with 8 additions and 3 deletions

View File

@ -125,7 +125,9 @@ class Script():
else: else:
raise ProfileNotFound(f"Cannot handle scheme {parsed_url.scheme}") raise ProfileNotFound(f"Cannot handle scheme {parsed_url.scheme}")
def load_instructions(self, namespace=None): def load_instructions(self, namespace=None, reset_namespace=False):
original_namespace = self.namespace
if namespace: if namespace:
self.namespace = namespace self.namespace = namespace
@ -135,6 +137,9 @@ class Script():
print(f"Imported {self} into sys.modules with namespace {self.namespace}.") print(f"Imported {self} into sys.modules with namespace {self.namespace}.")
if reset_namespace:
self.namespace = original_namespace
return self return self
def execute(self): def execute(self):
@ -173,7 +178,7 @@ class Profile(Script):
# if __name__ == 'moduleName' # if __name__ == 'moduleName'
if '__name__' in source_data and '_prep_function' in source_data: if '__name__' in source_data and '_prep_function' in source_data:
print(f"Checking if {self} has _prep_function by importing with namespace {self.namespace}.py") print(f"Checking if {self} has _prep_function by importing with namespace {self.namespace}.py")
with self.load_instructions(namespace=f"{self.namespace}.py") as imported: with self.load_instructions(namespace=f"{self.namespace}.py", reset_namespace=True) as imported:
if hasattr(imported, '_prep_function'): if hasattr(imported, '_prep_function'):
return True return True
return False return False

View File

@ -170,7 +170,7 @@ def ask_user_questions():
# Check the potentially selected profiles preperations to get early checks if some additional questions are needed. # Check the potentially selected profiles preperations to get early checks if some additional questions are needed.
if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_prep_function(): if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_prep_function():
print(f"{archinstall.arguments['profile']} has prep-function, loading with namespace {archinstall.arguments['profile'].namespace}.py") print(f"{archinstall.arguments['profile']} has prep-function, loading with namespace {archinstall.arguments['profile'].namespace}.py")
with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported: with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py", reset_namespace=True) as imported:
if not imported._prep_function(): if not imported._prep_function():
archinstall.log( archinstall.log(
' * Profile\'s preparation requirements was not fulfilled.', ' * Profile\'s preparation requirements was not fulfilled.',