Skip to content

Commit

Permalink
Added pythons -m module support. __main__.py is the main module entry…
Browse files Browse the repository at this point in the history
… path, and setup.py now includes the examples (which as been renamed for more convenient module importing) which - enables __main__.py to locate the examples and import them via importlib and execute them.
  • Loading branch information
Torxed committed Jul 7, 2020
1 parent e17fac4 commit 60f5813
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ This installer will perform the following:
# Testing

To test this, the simplest approach is to use a local image and create a loop device.<br>
This can be done by installing `pacman -S arch-install-scripts util-linux` locally and do the following:
To test this without a live ISO, the simplest approach is to use a local image and create a loop device.<br>
This can be done by installing `pacman -S arch-install-scripts util-linux` locally and doing the following:

# dd if=/dev/zero of=./testimage.img bs=1G count=5
# losetup -fP ./testimage.img
Expand Down
5 changes: 1 addition & 4 deletions archinstall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
from .lib.exceptions import *
from .lib.installer import *
from .lib.profiles import *
from .lib.luks import *

if __name__ == '__main__':
print('Launching as a module?')
from .lib.luks import *
29 changes: 29 additions & 0 deletions archinstall/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import archinstall, sys, os, glob
import importlib.util

class ProfileNotFound(BaseException):
pass

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

def find_examples():
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')}


if __name__ == '__main__':
if len(sys.argv) == 1: sys.arv.append('guided')

profile = sys.argv[1]
library = find_examples()

if not f'{profile}.py' in library:
raise ProfileNotFound(f'Could not locate {profile}.py among the example files.')

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
1 change: 1 addition & 0 deletions archinstall/lib/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time, pty
from subprocess import Popen, STDOUT, PIPE, check_output
from select import epoll, EPOLLIN, EPOLLHUP
from .exceptions import *

def log(*args, **kwargs):
string = ' '.join([str(x) for x in args])
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import setuptools
import setuptools, glob

with open("README.md", "r") as fh:
long_description = fh.read()
Expand All @@ -19,4 +19,5 @@
"Operating System :: POSIX :: Linux",
],
python_requires='>=3.8',
data_files=[('examples', glob.glob('examples/*.py'))],
)

0 comments on commit 60f5813

Please sign in to comment.