-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunctions.py
68 lines (59 loc) · 2.17 KB
/
functions.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
56
57
58
59
60
61
62
63
64
65
66
67
68
# copyright 2011-2012 Stefano Karapetsas <[email protected]>
# This file is part of AutoMate.
#
# AutoMate is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AutoMate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AutoMate. If not, see <http://www.gnu.org/licenses/>.
import hashlib
import json
import os
import pwd
import subprocess
import sys
def debug_message(debug_active, debug_message):
if debug_active:
print debug_message
# check if user is root, exit if normal user
def check_root():
usrinfo = pwd.getpwuid(os.getuid())
if not usrinfo.pw_name == "root":
print "E: root privileges required!"
sys.exit(1)
def command_result(command_string, output=True):
command = command_string.split(" ")
if output:
p = subprocess.Popen(command, shell=False)
else:
DEVNULL = open('/dev/null', 'w')
p = subprocess.Popen(command, shell=False, stdout=DEVNULL, stderr=DEVNULL)
while p.returncode is None:
p.poll()
return p.returncode
def json_load(json_file):
return json.load(open(json_file, "r"))
def json_save(json_object, json_file):
json_fd = open(json_file, "w")
json_fd.write(json.dumps(json_object, indent=4))
json_fd.close()
def distro_name(distro_codename):
if distro_codename in ["jessie", "wheezy"]:
return "debian"
elif distro_codename in ["trusty", "saucy", "raring", "quantal", "precise", "oneiric", "natty", "maverick", "lucid", "karmic", "jaunty", "hardy"]:
return "ubuntu"
else:
print "E: unknown distro name (%s)!" % distro_codename
sys.exit(1)
def sha1file(filepath):
if os.path.exists(filepath):
return hashlib.sha1(open(filepath, "rb").read()).hexdigest()
else:
return ""