Add support for remote profiles

This commit is contained in:
demostanis 2020-10-23 15:44:02 +02:00
parent 7f72c0fd93
commit 434cf71221
1 changed files with 35 additions and 21 deletions

View File

@ -1,5 +1,6 @@
from urllib.parse import urlparse
import archinstall, sys, os, glob
import importlib.util
import urllib.request
# TODO: Learn the dark arts of argparse...
# (I summon thee dark spawn of cPython)
@ -19,28 +20,41 @@ def find_examples():
return {os.path.basename(path): path for path in glob.glob(f'{examples}/*.py')}
def find(url):
parsed_url = urlparse(url)
if not parsed_url.scheme:
examples = find_examples()
if f"{url}.py" in examples:
return open(examples[f"{url}.py"]).read()
try:
return open(url, 'r').read()
except FileNotFoundError:
return ProfileNotFound(f"File {url} does not exist")
elif parsed_url.scheme in ('https', 'http'):
return urllib.request.urlopen(url).read().decode('utf-8')
else:
return ProfileNotFound(f"Cannot handle scheme {parsed_url.scheme}")
def run_as_a_module():
"""
Since we're running this as a 'python -m archinstall' module OR
a nuitka3 compiled version of the project.
This function and the file __main__ acts as a entry point.
"""
if len(sys.argv) == 1: sys.argv.append('guided')
"""
Since we're running this as a 'python -m archinstall' module OR
a nuitka3 compiled version of the project.
This function and the file __main__ acts as a entry point.
"""
if len(sys.argv) == 1: sys.argv.append('guided')
try:
profile = find(sys.argv[1])
print(profile)
except ProfileNotFound as err:
print(f"Couldn't find file: {err}")
sys.exit(1)
profile = sys.argv[1]
library = find_examples()
if f'{profile}.py' not in library:
raise ProfileNotFound(f'Could not locate {profile}.py among the example files.')
# Import and execute the chosen `<profile>.py`:
spec = importlib.util.spec_from_file_location(
library[f"{profile}.py"],
library[f"{profile}.py"]
)
imported_path = importlib.util.module_from_spec(spec)
spec.loader.exec_module(imported_path)
sys.modules[library[f'{profile}.py']] = imported_path
try:
exec(profile) # Is this is very safe?
except Exception as err:
print(f"Failed to run profile... {err}")
sys.exit(1) # Should prompt for another profile path instead
if __name__ == '__main__':
run_as_a_module()