-
Notifications
You must be signed in to change notification settings - Fork 1
/
pacman_wrapper.py
47 lines (40 loc) · 1.81 KB
/
pacman_wrapper.py
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
45
46
47
"""This file wraps pacman functions to python functions"""
from subprocess import check_call as ccall
import time
import shutil
import logging
from tempfile import NamedTemporaryFile
PACCONF = "./pacman.conf"
logging.basicConfig(format='%(asctime)s %(message)s', level="DEBUG")
def backup_configuration_file():
shutil.copy2(PACCONF, "".join((PACCONF, str(int(time.time())),"~")))
logging.debug("Created backup file of pacman.conf")
def install(packagename):
ccall(["pacman", "-Ss", "--noconfirm", packagename])
logging.info("installed {}".format(packagename))
def pac_conf_edit(repo, enable_repo=True):
try:
edit_next=False # because alwasys two lines have to be edited
with NamedTemporaryFile(delete=False) as tmp_f, open(PACCONF) as f:
for line in f:
if edit_next:
if enable_repo:
tmp_f.write(line[1:])
else:
tmp_f.write("".join(("#", line)))
logging.info("changed {}".format(line))
edit_next = False
continue
if enable_repo and line.startswith("".join(["#[",repo])):
tmp_f.write(line[1:]) #remove the #, thus enabling the repo
logging.info("changed {}".format(line))
edit_next = True
elif line.startswith("".join(("[", repo))):
tmp_f.write("".join(("#", line) ))
logging.info("changed {}".format(line))
edit_next = True
else:
tmp_f.write(line)
shutil.move(tmp_f.name, f.name)
except EnvironmentError:
logging.execption("Something went terribly wrong. Do we have the right permissions?")