forked from bryant1410/dotbot-apt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt.py
34 lines (26 loc) · 1.16 KB
/
apt.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
from subprocess import CalledProcessError, check_call, DEVNULL
from typing import Any, List, Sequence
import dotbot
class Apt(dotbot.Plugin):
def can_handle(self, directive: str) -> bool:
return directive == "apt"
def handle(self, directive: str, packages: List[str]) -> bool:
success = self._run(["apt", "update"], "Updating APT") \
and self._run(["apt", "install", "-y"] + packages,
"Installing the APT packages: {}".format(", ".join(packages)))
if success:
self._log.info("APT packages installed successfully")
return success
def _run(self, command: Sequence[Any], low_info: str) -> bool:
self._log.lowinfo(low_info)
try:
try:
# Try running the command without sudo first
check_call(command, stdout=DEVNULL, stderr=DEVNULL)
except CalledProcessError as e:
sudo_command = ["sudo"] + command
check_call(sudo_command, stdout=DEVNULL, stderr=DEVNULL)
return True
except CalledProcessError as e:
self._log.error(e)
return False