blob: 60d202bca7f317f6afcd2cdf01ec1ca76bc3a99e (
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
|
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.patterns: List[re.Pattern] = []
def enable(self, repo: Repo):
self.patterns.append(re.compile(r"^#\s*\[{}\]$".format(repo.value)))
def apply(self):
if not self.patterns:
return
lines = iter(self.path.read_text().splitlines(keepends=True))
with open(self.path, 'w') as f:
for line in lines:
if any(pattern.match(line) for pattern in self.patterns):
# Uncomment this line and the next.
f.write(line.lstrip('#'))
f.write(next(lines).lstrip('#'))
else:
f.write(line)
def persist(self):
if self.patterns:
copy2(self.path, self.chroot_path)
|