Skip to content

Commit 78969c5

Browse files
authored
Move script entry point (archlinux#4151)
1 parent 7cf1566 commit 78969c5

7 files changed

Lines changed: 142 additions & 154 deletions

File tree

archinstall/__init__.py

Lines changed: 2 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,3 @@
1-
"""Arch Linux installer - guided, templates etc."""
1+
from .lib.plugins import plugin
22

3-
import importlib
4-
import os
5-
import sys
6-
import time
7-
import traceback
8-
9-
from archinstall.lib.args import arch_config_handler
10-
from archinstall.lib.disk.utils import disk_layouts
11-
from archinstall.lib.general import running_from_host
12-
from archinstall.lib.network.wifi_handler import wifi_handler
13-
from archinstall.lib.networking import ping
14-
from archinstall.lib.packages.packages import check_version_upgrade
15-
16-
from .lib.hardware import SysInfo
17-
from .lib.output import FormattedOutput, debug, error, info, log, warn
18-
from .lib.pacman import Pacman
19-
from .lib.plugins import load_plugin, plugins
20-
from .lib.translationhandler import Language, tr, translation_handler
21-
22-
23-
# @archinstall.plugin decorator hook to programmatically add
24-
# plugins in runtime. Useful in profiles_bck and other things.
25-
def plugin(f, *args, **kwargs) -> None: # type: ignore[no-untyped-def]
26-
plugins[f.__name__] = f
27-
28-
29-
def _log_sys_info() -> None:
30-
# Log various information about hardware before starting the installation. This might assist in troubleshooting
31-
debug(f'Hardware model detected: {SysInfo.sys_vendor()} {SysInfo.product_name()}; UEFI mode: {SysInfo.has_uefi()}')
32-
debug(f'Processor model detected: {SysInfo.cpu_model()}')
33-
debug(f'Memory statistics: {SysInfo.mem_available()} available out of {SysInfo.mem_total()} total installed')
34-
debug(f'Virtualization detected: {SysInfo.virtualization()}; is VM: {SysInfo.is_vm()}')
35-
debug(f'Graphics devices detected: {SysInfo._graphics_devices().keys()}')
36-
37-
# For support reasons, we'll log the disk layout pre installation to match against post-installation layout
38-
debug(f'Disk states before installing:\n{disk_layouts()}')
39-
40-
41-
def _check_online() -> None:
42-
try:
43-
ping('1.1.1.1')
44-
except OSError as ex:
45-
if 'Network is unreachable' in str(ex):
46-
if not arch_config_handler.args.skip_wifi_check:
47-
success = not wifi_handler.setup()
48-
if not success:
49-
sys.exit(0)
50-
51-
52-
def _fetch_arch_db() -> None:
53-
info('Fetching Arch Linux package database...')
54-
try:
55-
Pacman.run('-Sy')
56-
except Exception as e:
57-
error('Failed to sync Arch Linux package database.')
58-
if 'could not resolve host' in str(e).lower():
59-
error('Most likely due to a missing network connection or DNS issue.')
60-
61-
error('Run archinstall --debug and check /var/log/archinstall/install.log for details.')
62-
63-
debug(f'Failed to sync Arch Linux package database: {e}')
64-
sys.exit(1)
65-
66-
67-
def main() -> int:
68-
"""
69-
This can either be run as the compiled and installed application: python setup.py install
70-
OR straight as a module: python -m archinstall
71-
In any case we will be attempting to load the provided script to be run from the scripts/ folder
72-
"""
73-
if '--help' in sys.argv or '-h' in sys.argv:
74-
arch_config_handler.print_help()
75-
return 0
76-
77-
if os.getuid() != 0:
78-
print(tr('Archinstall requires root privileges to run. See --help for more.'))
79-
return 1
80-
81-
_log_sys_info()
82-
83-
if not arch_config_handler.args.offline:
84-
_check_online()
85-
_fetch_arch_db()
86-
87-
if not arch_config_handler.args.skip_version_check:
88-
upgrade = check_version_upgrade()
89-
90-
if upgrade:
91-
text = tr('New version available') + f': {upgrade}'
92-
info(text)
93-
time.sleep(3)
94-
95-
if running_from_host():
96-
# log which mode we are using
97-
debug('Running from Host (H2T Mode)...')
98-
else:
99-
debug('Running from ISO (Live Mode)...')
100-
101-
script = arch_config_handler.get_script()
102-
103-
mod_name = f'archinstall.scripts.{script}'
104-
# by loading the module we'll automatically run the script
105-
importlib.import_module(mod_name)
106-
107-
return 0
108-
109-
110-
def run_as_a_module() -> None:
111-
rc = 0
112-
exc = None
113-
114-
try:
115-
rc = main()
116-
except Exception as e:
117-
exc = e
118-
finally:
119-
if exc:
120-
err = ''.join(traceback.format_exception(exc))
121-
error(err)
122-
123-
text = (
124-
'Archinstall experienced the above error. If you think this is a bug, please report it to\n'
125-
'https://github.com/archlinux/archinstall and include the log file "/var/log/archinstall/install.log".\n\n'
126-
"Hint: To extract the log from a live ISO \ncurl -F 'file=@/var/log/archinstall/install.log' https://0x0.st\n"
127-
)
128-
129-
warn(text)
130-
rc = 1
131-
132-
sys.exit(rc)
133-
134-
135-
__all__ = [
136-
'FormattedOutput',
137-
'Language',
138-
'Pacman',
139-
'SysInfo',
140-
'arch_config_handler',
141-
'debug',
142-
'disk_layouts',
143-
'error',
144-
'info',
145-
'load_plugin',
146-
'log',
147-
'plugin',
148-
'translation_handler',
149-
'warn',
150-
]
3+
__all__ = ['plugin']

archinstall/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import archinstall
1+
from archinstall.main import main
22

33
if __name__ == '__main__':
4-
archinstall.run_as_a_module()
4+
main()

archinstall/lib/plugins.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
)
2828

2929

30+
# @archinstall.plugin decorator hook to programmatically add
31+
# plugins in runtime. Useful in profiles_bck and other things.
32+
def plugin(f, *args, **kwargs) -> None: # type: ignore[no-untyped-def]
33+
plugins[f.__name__] = f
34+
35+
3036
def _localize_path(path: Path) -> Path:
3137
"""
3238
Support structures for load_plugin()

archinstall/main.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""Arch Linux installer - guided, templates etc."""
2+
3+
import importlib
4+
import os
5+
import sys
6+
import time
7+
import traceback
8+
9+
from archinstall.lib.args import arch_config_handler
10+
from archinstall.lib.disk.utils import disk_layouts
11+
from archinstall.lib.general import running_from_host
12+
from archinstall.lib.network.wifi_handler import wifi_handler
13+
from archinstall.lib.networking import ping
14+
from archinstall.lib.packages.packages import check_version_upgrade
15+
16+
from .lib.hardware import SysInfo
17+
from .lib.output import debug, error, info, warn
18+
from .lib.pacman import Pacman
19+
from .lib.translationhandler import tr
20+
21+
22+
def _log_sys_info() -> None:
23+
# Log various information about hardware before starting the installation. This might assist in troubleshooting
24+
debug(f'Hardware model detected: {SysInfo.sys_vendor()} {SysInfo.product_name()}; UEFI mode: {SysInfo.has_uefi()}')
25+
debug(f'Processor model detected: {SysInfo.cpu_model()}')
26+
debug(f'Memory statistics: {SysInfo.mem_available()} available out of {SysInfo.mem_total()} total installed')
27+
debug(f'Virtualization detected: {SysInfo.virtualization()}; is VM: {SysInfo.is_vm()}')
28+
debug(f'Graphics devices detected: {SysInfo._graphics_devices().keys()}')
29+
30+
# For support reasons, we'll log the disk layout pre installation to match against post-installation layout
31+
debug(f'Disk states before installing:\n{disk_layouts()}')
32+
33+
34+
def _check_online() -> None:
35+
try:
36+
ping('1.1.1.1')
37+
except OSError as ex:
38+
if 'Network is unreachable' in str(ex):
39+
if not arch_config_handler.args.skip_wifi_check:
40+
success = not wifi_handler.setup()
41+
if not success:
42+
sys.exit(0)
43+
44+
45+
def _fetch_arch_db() -> None:
46+
info('Fetching Arch Linux package database...')
47+
try:
48+
Pacman.run('-Sy')
49+
except Exception as e:
50+
error('Failed to sync Arch Linux package database.')
51+
if 'could not resolve host' in str(e).lower():
52+
error('Most likely due to a missing network connection or DNS issue.')
53+
54+
error('Run archinstall --debug and check /var/log/archinstall/install.log for details.')
55+
56+
debug(f'Failed to sync Arch Linux package database: {e}')
57+
sys.exit(1)
58+
59+
60+
def run() -> int:
61+
"""
62+
This can either be run as the compiled and installed application: python setup.py install
63+
OR straight as a module: python -m archinstall
64+
In any case we will be attempting to load the provided script to be run from the scripts/ folder
65+
"""
66+
if '--help' in sys.argv or '-h' in sys.argv:
67+
arch_config_handler.print_help()
68+
return 0
69+
70+
if os.getuid() != 0:
71+
print(tr('Archinstall requires root privileges to run. See --help for more.'))
72+
return 1
73+
74+
_log_sys_info()
75+
76+
if not arch_config_handler.args.offline:
77+
_check_online()
78+
_fetch_arch_db()
79+
80+
if not arch_config_handler.args.skip_version_check:
81+
upgrade = check_version_upgrade()
82+
83+
if upgrade:
84+
text = tr('New version available') + f': {upgrade}'
85+
info(text)
86+
time.sleep(3)
87+
88+
if running_from_host():
89+
# log which mode we are using
90+
debug('Running from Host (H2T Mode)...')
91+
else:
92+
debug('Running from ISO (Live Mode)...')
93+
94+
script = arch_config_handler.get_script()
95+
96+
mod_name = f'archinstall.scripts.{script}'
97+
# by loading the module we'll automatically run the script
98+
importlib.import_module(mod_name)
99+
100+
return 0
101+
102+
103+
def main() -> int:
104+
rc = 0
105+
exc = None
106+
107+
try:
108+
rc = run()
109+
except Exception as e:
110+
exc = e
111+
finally:
112+
if exc:
113+
err = ''.join(traceback.format_exception(exc))
114+
error(err)
115+
116+
text = (
117+
'Archinstall experienced the above error. If you think this is a bug, please report it to\n'
118+
'https://github.com/archlinux/archinstall and include the log file "/var/log/archinstall/install.log".\n\n'
119+
"Hint: To extract the log from a live ISO \ncurl -F 'file=@/var/log/archinstall/install.log' https://0x0.st\n"
120+
)
121+
122+
warn(text)
123+
rc = 1
124+
125+
sys.exit(rc)
126+
127+
128+
if __name__ == '__main__':
129+
main()

archinstall/scripts/guided.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import time
44
from pathlib import Path
55

6-
from archinstall import SysInfo
76
from archinstall.lib.applications.application_handler import application_handler
87
from archinstall.lib.args import arch_config_handler
98
from archinstall.lib.authentication.authentication_handler import auth_handler
109
from archinstall.lib.configuration import ConfigurationOutput
1110
from archinstall.lib.disk.filesystem import FilesystemHandler
1211
from archinstall.lib.disk.utils import disk_layouts
1312
from archinstall.lib.global_menu import GlobalMenu
13+
from archinstall.lib.hardware import SysInfo
1414
from archinstall.lib.installer import Installer, accessibility_tools_in_use, run_custom_user_commands
1515
from archinstall.lib.interactions.general_conf import PostInstallationAction, ask_post_installation
1616
from archinstall.lib.models import Bootloader

archinstall/scripts/only_hd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import sys
22
from pathlib import Path
33

4-
from archinstall import debug, error
54
from archinstall.lib.args import arch_config_handler
65
from archinstall.lib.configuration import ConfigurationOutput
76
from archinstall.lib.disk.filesystem import FilesystemHandler
87
from archinstall.lib.disk.utils import disk_layouts
98
from archinstall.lib.global_menu import GlobalMenu
109
from archinstall.lib.installer import Installer
10+
from archinstall.lib.output import debug, error
1111

1212

1313
def ask_user_questions() -> None:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dev = [
4242
doc = ["sphinx"]
4343

4444
[project.scripts]
45-
archinstall = "archinstall:run_as_a_module"
45+
archinstall = "archinstall.main:main"
4646

4747
[tool.setuptools.dynamic]
4848
readme = {file = ["README.rst", "USAGE.rst"]}

0 commit comments

Comments
 (0)