forked from Metarget/metarget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.py
55 lines (45 loc) · 1.37 KB
/
system.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
48
49
50
51
52
53
54
55
"""
System Functions
"""
import subprocess
import os
from pathlib import Path
import utils.color_print as color_print
import utils.verbose as verbose_func
def reload_daemon_config(verbose=False):
color_print.debug('reloading daemon configurations')
stdout, stderr = verbose_func.verbose_output(verbose)
try:
subprocess.run(
'systemctl daemon-reload'.split(),
stdout=stdout,
stderr=stderr,
check=True)
return True
except subprocess.CalledProcessError:
color_print.error('failed to reload daemon configurations')
return False
def reboot_system(verbose=False):
stdout, stderr = verbose_func.verbose_output(verbose)
cmd_reboot = 'init 6'.split()
try:
subprocess.run(cmd_reboot, stdout=stdout, stderr=stderr, check=True)
except subprocess.CalledProcessError:
color_print.error('failed to reboot system')
return False
def mkdir_if_not_exist(dir_path):
try:
p = Path(dir_path)
p.mkdir(parents=True)
except FileExistsError:
pass
def create_file_if_not_exist(file_path):
try:
f = open(file_path, 'r')
f.close()
except FileNotFoundError:
dir_path = Path(os.path.split(file_path)[0])
if not dir_path.exists():
dir_path.mkdir(parents=True)
p = Path(file_path)
p.touch()