From 434cf7122174e15b166449ec531d4c18d064368c Mon Sep 17 00:00:00 2001 From: demostanis Date: Fri, 23 Oct 2020 15:44:02 +0200 Subject: Add support for remote profiles --- archinstall/__main__.py | 58 ++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 22 deletions(-) (limited to 'archinstall') diff --git a/archinstall/__main__.py b/archinstall/__main__.py index fe4a3732..744d2a5d 100644 --- a/archinstall/__main__.py +++ b/archinstall/__main__.py @@ -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') - - 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 `.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 + """ + 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) + + 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() -- cgit v1.2.3-54-g00ecf From d018ae98a1178ca3105e68cf7a25df32ece12398 Mon Sep 17 00:00:00 2001 From: demostanis Date: Fri, 23 Oct 2020 15:49:03 +0200 Subject: Remove useless log --- archinstall/__main__.py | 1 - 1 file changed, 1 deletion(-) (limited to 'archinstall') diff --git a/archinstall/__main__.py b/archinstall/__main__.py index 744d2a5d..d2ef22cb 100644 --- a/archinstall/__main__.py +++ b/archinstall/__main__.py @@ -45,7 +45,6 @@ def run_as_a_module(): try: profile = find(sys.argv[1]) - print(profile) except ProfileNotFound as err: print(f"Couldn't find file: {err}") sys.exit(1) -- cgit v1.2.3-54-g00ecf From 5d2b11e60fa244c1f8af78d4238848f45a4c0d2c Mon Sep 17 00:00:00 2001 From: demostanis Date: Fri, 23 Oct 2020 15:53:31 +0200 Subject: Ran autopep8 --- archinstall/__main__.py | 73 +++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 32 deletions(-) (limited to 'archinstall') diff --git a/archinstall/__main__.py b/archinstall/__main__.py index d2ef22cb..de7cf994 100644 --- a/archinstall/__main__.py +++ b/archinstall/__main__.py @@ -1,59 +1,68 @@ from urllib.parse import urlparse -import archinstall, sys, os, glob +import archinstall +import sys +import os +import glob import urllib.request # TODO: Learn the dark arts of argparse... # (I summon thee dark spawn of cPython) + class ProfileNotFound(BaseException): - pass + pass + def find_examples(): - """ - Used to locate the examples, bundled with the module or executable. + """ + Used to locate the examples, bundled with the module or executable. - :return: {'guided.py' : './examples/guided.py', '' : ''} - :rtype: dict - """ - cwd = os.path.abspath(f'{os.path.dirname(__file__)}') - examples = f"{cwd}/examples" + :return: {'guided.py' : './examples/guided.py', '' : ''} + :rtype: dict + """ + cwd = os.path.abspath(f'{os.path.dirname(__file__)}') + examples = f"{cwd}/examples" + + return {os.path.basename(path): path for path in glob.glob(f'{examples}/*.py')} - 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() + return open(examples[f"{url}.py"]).read() try: - return open(url, 'r').read() + return open(url, 'r').read() except FileNotFoundError: - return ProfileNotFound(f"File {url} does not exist") + 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') - - try: - profile = find(sys.argv[1]) - except ProfileNotFound as err: - print(f"Couldn't find file: {err}") - sys.exit(1) - - 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 + """ + 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]) + except ProfileNotFound as err: + print(f"Couldn't find file: {err}") + sys.exit(1) + + 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() + run_as_a_module() -- cgit v1.2.3-54-g00ecf From 57eef46f953574799e01b389e9f665f058bbd42d Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 1 Nov 2020 23:01:41 +0000 Subject: Fix for issue #59 Relative paths work great for running as a script. But break when running as a module since there is no `src/` folder to mention. This should clear that up as the `src/` now lives under wherever the python package was installed. --- archinstall/__main__.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'archinstall') diff --git a/archinstall/__main__.py b/archinstall/__main__.py index fe4a3732..48d8a5ce 100644 --- a/archinstall/__main__.py +++ b/archinstall/__main__.py @@ -33,6 +33,10 @@ def run_as_a_module(): if f'{profile}.py' not in library: raise ProfileNotFound(f'Could not locate {profile}.py among the example files.') + # Swap the working dir, otherwise certain relative lookups won't work within archinstall. + # Mainly to avoid https://github.com/Torxed/archinstall/issues/59 + os.chdir(os.path.abspath(os.path.dirname(__file__))) + # Import and execute the chosen `.py`: spec = importlib.util.spec_from_file_location( library[f"{profile}.py"], -- cgit v1.2.3-54-g00ecf From 8f7cc1280eed461e28d9a3273da103142c59c18c Mon Sep 17 00:00:00 2001 From: demostanis Date: Mon, 2 Nov 2020 18:30:43 +0100 Subject: Replaced spaces with tabs --- archinstall/__main__.py | 82 ++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) (limited to 'archinstall') diff --git a/archinstall/__main__.py b/archinstall/__main__.py index de7cf994..be3d4e66 100644 --- a/archinstall/__main__.py +++ b/archinstall/__main__.py @@ -6,63 +6,63 @@ import glob import urllib.request # TODO: Learn the dark arts of argparse... -# (I summon thee dark spawn of cPython) +# (I summon thee dark spawn of cPython) class ProfileNotFound(BaseException): - pass + pass def find_examples(): - """ - Used to locate the examples, bundled with the module or executable. + """ + Used to locate the examples, bundled with the module or executable. - :return: {'guided.py' : './examples/guided.py', '' : ''} - :rtype: dict - """ - cwd = os.path.abspath(f'{os.path.dirname(__file__)}') - examples = f"{cwd}/examples" + :return: {'guided.py' : './examples/guided.py', '' : ''} + :rtype: dict + """ + cwd = os.path.abspath(f'{os.path.dirname(__file__)}') + examples = f"{cwd}/examples" - return {os.path.basename(path): path for path in glob.glob(f'{examples}/*.py')} + 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}") + 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]) - except ProfileNotFound as err: - print(f"Couldn't find file: {err}") - sys.exit(1) + try: + profile = find(sys.argv[1]) + except ProfileNotFound as err: + print(f"Couldn't find file: {err}") + sys.exit(1) - 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 + 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() + run_as_a_module() -- cgit v1.2.3-54-g00ecf