-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom.py
More file actions
executable file
·97 lines (86 loc) · 3.33 KB
/
Copy pathcom.py
File metadata and controls
executable file
·97 lines (86 loc) · 3.33 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
## Hopefully we can avoid disaster if we dont import this in a main program
import os, shutil, subprocess
from sys import platform
# This class provides the functionality we want. You only need to look at
# this if you want to know how this works. It only needs to be defined
# once, no need to muck around with its internals.
# Located at http://code.activestate.com/recipes/410692/
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args:
self.fall = True
return True
else:
return False
## OS Specific Stuff
class _OS_(object):
"""Defines an OS environment to work with"""
def __init__(self):
_SystemOS_ = platform.strip()
if (_SystemOS_ == 'linux' or _SystemOS_ == 'linux2'):
# linux
with open('/etc/os-release') as file:
oper = file.readlines()
oper = oper[5].split('=')
self._type_ = oper[1].strip() # Grab OS release Name we want to know what flavor of lenny we use.
elif(_SystemOS_ == 'win32'):
self._type_ = _SystemOS_
def ProgExists(self,package):
"""Checks to see if a program is installed or not"""
# TODO move this over to aptitude so we can call a list of packages and see if ours is installed
# https://www.tecmint.com/difference-between-apt-and-aptitude/
status = subprocess.getstatusoutput("dpkg-query -W -f='${Status}' " + package)
if not status[0]:
return '{}Installed{}'.format(color.OKGREEN,color.END) # package is installed
else:
return '{}Not Installed{}'.format(color.FAIL,color.END)
def Clear(self):
"""Clear terminal screen"""
if(self._type_ == "win32"):
os.system("cls")
else: # well its not Windows we can just "clear"
os.system("clear")
def Shutdown(self):
"""Shut down system"""
if(self._type_ == 'win32'):
os.system('shutdown', '/s')
elif(self._type_ == 'debian'):
os.system('sudo shutdown -h')
def Reboot(self):
"""Reboot System"""
if(self._type_ == 'win32'):
os.system('shutdown', '/r')
elif(self._type_ == 'debian'):
os.system('sudo reboot')
def FormatName(self):
"""Return print ready string for OS name"""
formatstring = "{}{}{}"
if(self._type_ == "win32"):
return formatstring.format(color.FAIL,self._type_,color.END)
elif(self._type_ == "debian"):
return formatstring.format(color.OKGREEN,self._type_,color.END)
else:
return formatstring.format(color.WARNING,self._type_,color.END)
class color:
"""Text output color definitions"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
END = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def shellQoute(str):
"""Return a shell qouted string"""
return "'" + str.replace("'", "'\\''") + "'"