Skip to content

Commit 2421e53

Browse files
Move Pacman out of init (archlinux#4150)
* Move pacman out of init * Update archinstall/lib/pacman/pacman.py Co-authored-by: codefiles <11915375+codefiles@users.noreply.github.com> * Update archinstall/lib/pacman/pacman.py Co-authored-by: codefiles <11915375+codefiles@users.noreply.github.com> --------- Co-authored-by: codefiles <11915375+codefiles@users.noreply.github.com>
1 parent 28f7aec commit 2421e53

File tree

6 files changed

+87
-94
lines changed

6 files changed

+87
-94
lines changed

archinstall/lib/installer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
from .models.network import Nic
5050
from .models.users import User
5151
from .output import debug, error, info, log, logger, warn
52-
from .pacman import Pacman
5352
from .pacman.config import PacmanConfig
53+
from .pacman.pacman import Pacman
5454
from .plugins import plugins
5555

5656
# Any package that the Installer() is responsible for (optional and the default ones)

archinstall/lib/networking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from .exceptions import DownloadTimeout, SysCallError
1616
from .output import debug, error, info
17-
from .pacman import Pacman
17+
from .pacman.pacman import Pacman
1818

1919

2020
class DownloadTimer:

archinstall/lib/packages/packages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ..exceptions import SysCallError
44
from ..models.packages import AvailablePackage, LocalPackage, Repository
55
from ..output import debug
6-
from ..pacman import Pacman
6+
from ..pacman.pacman import Pacman
77

88

99
# TODO: This shouldn't be living in here but there are too many

archinstall/lib/pacman/__init__.py

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +0,0 @@
1-
import sys
2-
import time
3-
from collections.abc import Callable
4-
from pathlib import Path
5-
6-
from archinstall.lib.translationhandler import tr
7-
8-
from ..exceptions import RequirementError
9-
from ..general import SysCommand
10-
from ..output import error, info, warn
11-
from ..plugins import plugins
12-
from .config import PacmanConfig
13-
14-
15-
class Pacman:
16-
def __init__(self, target: Path, silent: bool = False):
17-
self.synced = False
18-
self.silent = silent
19-
self.target = target
20-
21-
@staticmethod
22-
def run(args: str, default_cmd: str = 'pacman') -> SysCommand:
23-
"""
24-
A centralized function to call `pacman` from.
25-
It also protects us from colliding with other running pacman sessions (if used locally).
26-
The grace period is set to 10 minutes before exiting hard if another pacman instance is running.
27-
"""
28-
pacman_db_lock = Path('/var/lib/pacman/db.lck')
29-
30-
if pacman_db_lock.exists():
31-
warn(tr('Pacman is already running, waiting maximum 10 minutes for it to terminate.'))
32-
33-
started = time.time()
34-
while pacman_db_lock.exists():
35-
time.sleep(0.25)
36-
37-
if time.time() - started > (60 * 10):
38-
error(tr('Pre-existing pacman lock never exited. Please clean up any existing pacman sessions before using archinstall.'))
39-
sys.exit(1)
40-
41-
return SysCommand(f'{default_cmd} {args}')
42-
43-
def ask(self, error_message: str, bail_message: str, func: Callable, *args, **kwargs) -> None: # type: ignore[no-untyped-def, type-arg]
44-
while True:
45-
try:
46-
func(*args, **kwargs)
47-
break
48-
except Exception as err:
49-
error(f'{error_message}: {err}')
50-
if not self.silent and input('Would you like to re-try this download? (Y/n): ').lower().strip() in 'y':
51-
continue
52-
raise RequirementError(f'{bail_message}: {err}')
53-
54-
def sync(self) -> None:
55-
if self.synced:
56-
return
57-
self.ask(
58-
'Could not sync a new package database',
59-
'Could not sync mirrors',
60-
self.run,
61-
'-Syy',
62-
default_cmd='pacman',
63-
)
64-
self.synced = True
65-
66-
def strap(self, packages: str | list[str]) -> None:
67-
self.sync()
68-
if isinstance(packages, str):
69-
packages = [packages]
70-
71-
for plugin in plugins.values():
72-
if hasattr(plugin, 'on_pacstrap'):
73-
if result := plugin.on_pacstrap(packages):
74-
packages = result
75-
76-
info(f'Installing packages: {packages}')
77-
78-
self.ask(
79-
'Could not strap in packages',
80-
'Pacstrap failed. See /var/log/archinstall/install.log or above message for error details',
81-
SysCommand,
82-
f'pacstrap -C /etc/pacman.conf -K {self.target} {" ".join(packages)} --noconfirm --needed',
83-
peek_output=True,
84-
)
85-
86-
87-
__all__ = [
88-
'Pacman',
89-
'PacmanConfig',
90-
]

archinstall/lib/pacman/pacman.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import sys
2+
import time
3+
from collections.abc import Callable
4+
from pathlib import Path
5+
6+
from archinstall.lib.translationhandler import tr
7+
8+
from ..exceptions import RequirementError
9+
from ..general import SysCommand
10+
from ..output import error, info, warn
11+
from ..plugins import plugins
12+
13+
14+
class Pacman:
15+
def __init__(self, target: Path, silent: bool = False):
16+
self.synced = False
17+
self.silent = silent
18+
self.target = target
19+
20+
@staticmethod
21+
def run(args: str, default_cmd: str = 'pacman') -> SysCommand:
22+
"""
23+
A centralized function to call `pacman` from.
24+
It also protects us from colliding with other running pacman sessions (if used locally).
25+
The grace period is set to 10 minutes before exiting hard if another pacman instance is running.
26+
"""
27+
pacman_db_lock = Path('/var/lib/pacman/db.lck')
28+
29+
if pacman_db_lock.exists():
30+
warn(tr('Pacman is already running, waiting maximum 10 minutes for it to terminate.'))
31+
32+
started = time.time()
33+
while pacman_db_lock.exists():
34+
time.sleep(0.25)
35+
36+
if time.time() - started > (60 * 10):
37+
error(tr('Pre-existing pacman lock never exited. Please clean up any existing pacman sessions before using archinstall.'))
38+
sys.exit(1)
39+
40+
return SysCommand(f'{default_cmd} {args}')
41+
42+
def ask(self, error_message: str, bail_message: str, func: Callable, *args, **kwargs) -> None: # type: ignore[no-untyped-def, type-arg]
43+
while True:
44+
try:
45+
func(*args, **kwargs)
46+
break
47+
except Exception as err:
48+
error(f'{error_message}: {err}')
49+
if not self.silent and input('Would you like to re-try this download? (Y/n): ').lower().strip() in 'y':
50+
continue
51+
raise RequirementError(f'{bail_message}: {err}')
52+
53+
def sync(self) -> None:
54+
if self.synced:
55+
return
56+
self.ask(
57+
'Could not sync a new package database',
58+
'Could not sync mirrors',
59+
self.run,
60+
'-Syy',
61+
default_cmd='pacman',
62+
)
63+
self.synced = True
64+
65+
def strap(self, packages: str | list[str]) -> None:
66+
self.sync()
67+
if isinstance(packages, str):
68+
packages = [packages]
69+
70+
for plugin in plugins.values():
71+
if hasattr(plugin, 'on_pacstrap'):
72+
if result := plugin.on_pacstrap(packages):
73+
packages = result
74+
75+
info(f'Installing packages: {packages}')
76+
77+
self.ask(
78+
'Could not strap in packages',
79+
'Pacstrap failed. See /var/log/archinstall/install.log or above message for error details',
80+
SysCommand,
81+
f'pacstrap -C /etc/pacman.conf -K {self.target} {" ".join(packages)} --noconfirm --needed',
82+
peek_output=True,
83+
)

archinstall/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from .lib.hardware import SysInfo
1818
from .lib.output import debug, error, info, warn
19-
from .lib.pacman import Pacman
19+
from .lib.pacman.pacman import Pacman
2020
from .lib.translationhandler import tr
2121

2222

0 commit comments

Comments
 (0)