Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/__main__.py
blob: fe4a37321e8a211a3c7e6ee2646b376d0c00ea72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import archinstall, sys, os, glob
import importlib.util

# TODO: Learn the dark arts of argparse...
#       (I summon thee dark spawn of cPython)

class ProfileNotFound(BaseException):
	pass

def find_examples():
	"""
	Used to locate the examples, bundled with the module or executable.

	:return: {'guided.py' : './examples/guided.py', '<profile #2>' : '<path #2>'}
	: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')}

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 `<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

if __name__ == '__main__':
	run_as_a_module()