Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/pacman/config.py
blob: 6686f4a9bd24ad9e17dec2a91a16a0f36e1e39c7 (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
import re
from pathlib import Path
from shutil import copy2
from typing import List

from .repo import Repo


class Config:
	def __init__(self, target: Path):
		self.path = Path("/etc") / "pacman.conf"
		self.chroot_path = target / "etc" / "pacman.conf"
		self.repos: List[Repo] = []

	def enable(self, repo: Repo):
		self.repos.append(repo)

	def apply(self):
		if not self.repos:
			return

		if Repo.Testing in self.repos:
			if Repo.Multilib in self.repos:
				repos_pattern = f'({Repo.Multilib.value}|.+-{Repo.Testing.value})'
			else:
				repos_pattern = f'(?!{Repo.Multilib.value}).+-{Repo.Testing.value}'
		else:
			repos_pattern = Repo.Multilib.value

		pattern = re.compile(rf"^#\s*\[{repos_pattern}\]$")

		lines = iter(self.path.read_text().splitlines(keepends=True))
		with open(self.path, 'w') as f:
			for line in lines:
				if pattern.match(line):
					# Uncomment this line and the next.
					f.write(line.lstrip('#'))
					f.write(next(lines).lstrip('#'))
				else:
					f.write(line)

	def persist(self):
		if self.repos:
			copy2(self.path, self.chroot_path)