From 60f581319f99afbd678b4f5f56193d588228cbe2 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Tue, 7 Jul 2020 23:56:17 +0000 Subject: Added pythons -m module support. __main__.py is the main module entry 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. --- README.md | 4 +-- archinstall/__init__.py | 5 +-- archinstall/__main__.py | 29 ++++++++++++++++ archinstall/lib/general.py | 1 + examples/guided.py | 73 +++++++++++++++++++++++++++++++++++++++++ examples/guided_installation.py | 73 ----------------------------------------- examples/main_example.py | 32 ------------------ examples/minimal.py | 32 ++++++++++++++++++ setup.py | 3 +- 9 files changed, 140 insertions(+), 112 deletions(-) create mode 100644 archinstall/__main__.py create mode 100644 examples/guided.py delete mode 100644 examples/guided_installation.py delete mode 100644 examples/main_example.py create mode 100644 examples/minimal.py diff --git a/README.md b/README.md index 48b85a63..a0d86988 100644 --- a/README.md +++ b/README.md @@ -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.
-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.
+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 diff --git a/archinstall/__init__.py b/archinstall/__init__.py index a9eeb6b5..9cf7faec 100644 --- a/archinstall/__init__.py +++ b/archinstall/__init__.py @@ -4,7 +4,4 @@ from .lib.user_interaction import * from .lib.exceptions import * from .lib.installer import * from .lib.profiles import * -from .lib.luks import * - -if __name__ == '__main__': - print('Launching as a module?') \ No newline at end of file +from .lib.luks import * \ No newline at end of file diff --git a/archinstall/__main__.py b/archinstall/__main__.py new file mode 100644 index 00000000..bd657291 --- /dev/null +++ b/archinstall/__main__.py @@ -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 \ No newline at end of file diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 31f81413..b0de39a7 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -2,6 +2,7 @@ import os, json, hashlib, shlex, sys 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]) diff --git a/examples/guided.py b/examples/guided.py new file mode 100644 index 00000000..c84b5e56 --- /dev/null +++ b/examples/guided.py @@ -0,0 +1,73 @@ +import archinstall, getpass + +# Unmount and close previous runs +archinstall.sys_command(f'umount -R /mnt', surpress_errors=True) +archinstall.sys_command(f'cryptsetup close /dev/mapper/luksloop', surpress_errors=True) + +# Select a harddrive and a disk password +harddrive = archinstall.select_disk(archinstall.all_disks()) +while (disk_password := getpass.getpass(prompt='Enter disk encryption password (leave blank for no encryption): ')): + disk_password_verification = getpass.getpass(prompt='And one more time for verification: ') + if disk_password != disk_password_verification: + archinstall.log(' * Passwords did not match * ', bg='black', fg='red') + continue + break + +def perform_installation(device, boot_partition): + hostname = input('Desired hostname for the installation: ') + with archinstall.Installer(device, hostname=hostname) as installation: + if installation.minimal_installation(): + installation.add_bootloader(boot_partition) + + packages = input('Additional packages aside from base (space separated): ').split(' ') + if len(packages) and packages[0] != '': + installation.add_additional_packages(packages) + + profile = input('Any particular profile you want to install: ') + if len(profile.strip()): + installation.install_profile(profile) + + while 1: + new_user = input('Any additional users to install (leave blank for no users): ') + if not len(new_user.strip()): break + new_user_passwd = getpass.getpass(prompt=f'Password for user {new_user}: ') + new_user_passwd_verify = getpass.getpass(prompt=f'Enter password again for verification: ') + if new_user_passwd != new_user_passwd_verify: + archinstall.log(' * Passwords did not match * ', bg='black', fg='red') + continue + + installation.user_create(new_user, new_user_passwd) + + while (root_pw := getpass.getpass(prompt='Enter root password (leave blank for no password): ')): + root_pw_verification = getpass.getpass(prompt='And one more time for verification: ') + if root_pw != root_pw_verification: + archinstall.log(' * Passwords did not match * ', bg='black', fg='red') + continue + installation.user_set_pw('root', root_pw) + break + + aur = input('Would you like AUR support? (leave blank for no): ') + if len(aur.strip()): + archinstall.log(' - AUR support provided by yay (https://aur.archlinux.org/packages/yay/)', bg='black', fg='white') + installation.add_AUR_support() + +with archinstall.Filesystem(harddrive, archinstall.GPT) as fs: + # Use partitioning helper to set up the disk partitions. + if disk_password: + fs.use_entire_disk('luks2') + else: + fs.use_entire_disk('ext4') + + if harddrive.partition[1].size == '512M': + raise OSError('Trying to encrypt the boot partition for petes sake..') + harddrive.partition[0].format('fat32') + + if disk_password: + # First encrypt and unlock, then format the desired partition inside the encrypted part. + with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: + unlocked_device.format('btrfs') + + perform_installation(unlocked_device, harddrive.partition[0]) + else: + harddrive.partition[1].format('ext4') + perform_installation(harddrive.partition[1], harddrive.partition[0]) \ No newline at end of file diff --git a/examples/guided_installation.py b/examples/guided_installation.py deleted file mode 100644 index c84b5e56..00000000 --- a/examples/guided_installation.py +++ /dev/null @@ -1,73 +0,0 @@ -import archinstall, getpass - -# Unmount and close previous runs -archinstall.sys_command(f'umount -R /mnt', surpress_errors=True) -archinstall.sys_command(f'cryptsetup close /dev/mapper/luksloop', surpress_errors=True) - -# Select a harddrive and a disk password -harddrive = archinstall.select_disk(archinstall.all_disks()) -while (disk_password := getpass.getpass(prompt='Enter disk encryption password (leave blank for no encryption): ')): - disk_password_verification = getpass.getpass(prompt='And one more time for verification: ') - if disk_password != disk_password_verification: - archinstall.log(' * Passwords did not match * ', bg='black', fg='red') - continue - break - -def perform_installation(device, boot_partition): - hostname = input('Desired hostname for the installation: ') - with archinstall.Installer(device, hostname=hostname) as installation: - if installation.minimal_installation(): - installation.add_bootloader(boot_partition) - - packages = input('Additional packages aside from base (space separated): ').split(' ') - if len(packages) and packages[0] != '': - installation.add_additional_packages(packages) - - profile = input('Any particular profile you want to install: ') - if len(profile.strip()): - installation.install_profile(profile) - - while 1: - new_user = input('Any additional users to install (leave blank for no users): ') - if not len(new_user.strip()): break - new_user_passwd = getpass.getpass(prompt=f'Password for user {new_user}: ') - new_user_passwd_verify = getpass.getpass(prompt=f'Enter password again for verification: ') - if new_user_passwd != new_user_passwd_verify: - archinstall.log(' * Passwords did not match * ', bg='black', fg='red') - continue - - installation.user_create(new_user, new_user_passwd) - - while (root_pw := getpass.getpass(prompt='Enter root password (leave blank for no password): ')): - root_pw_verification = getpass.getpass(prompt='And one more time for verification: ') - if root_pw != root_pw_verification: - archinstall.log(' * Passwords did not match * ', bg='black', fg='red') - continue - installation.user_set_pw('root', root_pw) - break - - aur = input('Would you like AUR support? (leave blank for no): ') - if len(aur.strip()): - archinstall.log(' - AUR support provided by yay (https://aur.archlinux.org/packages/yay/)', bg='black', fg='white') - installation.add_AUR_support() - -with archinstall.Filesystem(harddrive, archinstall.GPT) as fs: - # Use partitioning helper to set up the disk partitions. - if disk_password: - fs.use_entire_disk('luks2') - else: - fs.use_entire_disk('ext4') - - if harddrive.partition[1].size == '512M': - raise OSError('Trying to encrypt the boot partition for petes sake..') - harddrive.partition[0].format('fat32') - - if disk_password: - # First encrypt and unlock, then format the desired partition inside the encrypted part. - with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: - unlocked_device.format('btrfs') - - perform_installation(unlocked_device, harddrive.partition[0]) - else: - harddrive.partition[1].format('ext4') - perform_installation(harddrive.partition[1], harddrive.partition[0]) \ No newline at end of file diff --git a/examples/main_example.py b/examples/main_example.py deleted file mode 100644 index 195ee60c..00000000 --- a/examples/main_example.py +++ /dev/null @@ -1,32 +0,0 @@ -import archinstall, getpass - -# Unmount and close previous runs -archinstall.sys_command(f'umount -R /mnt', surpress_errors=True) -archinstall.sys_command(f'cryptsetup close /dev/mapper/luksloop', surpress_errors=True) - -# Select a harddrive and a disk password -harddrive = archinstall.select_disk(archinstall.all_disks()) -disk_password = getpass.getpass(prompt='Disk password (won\'t echo): ') - -with archinstall.Filesystem(harddrive, archinstall.GPT) as fs: - # Use the entire disk instead of setting up partitions on your own - fs.use_entire_disk('luks2') - - if harddrive.partition[1].size == '512M': - raise OSError('Trying to encrypt the boot partition for petes sake..') - harddrive.partition[0].format('fat32') - - with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: - unlocked_device.format('btrfs') - - with archinstall.Installer(unlocked_device, hostname='testmachine') as installation: - if installation.minimal_installation(): - installation.add_bootloader(harddrive.partition[0]) - - installation.add_additional_packages(['nano', 'wget', 'git']) - installation.install_profile('workstation') - - installation.user_create('anton', 'test') - installation.user_set_pw('root', 'toor') - - installation.add_AUR_support() \ No newline at end of file diff --git a/examples/minimal.py b/examples/minimal.py new file mode 100644 index 00000000..195ee60c --- /dev/null +++ b/examples/minimal.py @@ -0,0 +1,32 @@ +import archinstall, getpass + +# Unmount and close previous runs +archinstall.sys_command(f'umount -R /mnt', surpress_errors=True) +archinstall.sys_command(f'cryptsetup close /dev/mapper/luksloop', surpress_errors=True) + +# Select a harddrive and a disk password +harddrive = archinstall.select_disk(archinstall.all_disks()) +disk_password = getpass.getpass(prompt='Disk password (won\'t echo): ') + +with archinstall.Filesystem(harddrive, archinstall.GPT) as fs: + # Use the entire disk instead of setting up partitions on your own + fs.use_entire_disk('luks2') + + if harddrive.partition[1].size == '512M': + raise OSError('Trying to encrypt the boot partition for petes sake..') + harddrive.partition[0].format('fat32') + + with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: + unlocked_device.format('btrfs') + + with archinstall.Installer(unlocked_device, hostname='testmachine') as installation: + if installation.minimal_installation(): + installation.add_bootloader(harddrive.partition[0]) + + installation.add_additional_packages(['nano', 'wget', 'git']) + installation.install_profile('workstation') + + installation.user_create('anton', 'test') + installation.user_set_pw('root', 'toor') + + installation.add_AUR_support() \ No newline at end of file diff --git a/setup.py b/setup.py index 81666ffb..90d691ad 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -import setuptools +import setuptools, glob with open("README.md", "r") as fh: long_description = fh.read() @@ -19,4 +19,5 @@ setuptools.setup( "Operating System :: POSIX :: Linux", ], python_requires='>=3.8', + data_files=[('examples', glob.glob('examples/*.py'))], ) \ No newline at end of file -- cgit v1.2.3-70-g09d2