-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopmenu.py
More file actions
executable file
·277 lines (229 loc) · 7.36 KB
/
opmenu.py
File metadata and controls
executable file
·277 lines (229 loc) · 7.36 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/python
# -*- coding: UTF-8
# Author: David Acacio
# Email: dacacioa@gmail.com
## Contributions:
# Author: Jose Luis Bermudez
# Email: jl.bermudez@gmail.com
#
# * Check logfile exist and create it if not.
# * Translate all non english literals.
# * Adding and check actionlog.
#import python class library
import re,sys,os,subprocess,commands,logging,i18n,getpass
# INTERNACIONALIZACIÓN.
reload(sys)
#sys.setdefaultencoding("utf-8")
_ = i18n.language.ugettext #use ugettext instead of getttext to avoid unicode errors
INSTALL_PATH = os.getcwd()+'/' #by default the current working directory. Modify string value if you want to change it
USER = getpass.getuser()
## Check logfile, if don´t exist create it.
#LOGDIR='/logs/system/gomenu' ## not standard path.
LOGDIR='/var/log' ## dir location compatible all linux distributions.
LOGFILE=LOGDIR + '/opmenu.%s.log' % USER
bLog = True
try:
if not os.path.exists(LOGDIR):
os.makedirs(LOGDIR)
if os.path.isfile(LOGFILE):
print _("File exists")
else:
with open(LOGFILE,'a') as filelog:
filelog.close();
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
LOGDIR='/tmp' # Alternative to the log directory, users without permissions can not generate log in /var/log.
LOGFILE=LOGDIR + '/opmenu.%s.log' % USER
with open(LOGFILE,'a') as filelog:
filelog.close();
except:
print _("Unknown error")
bLog = False
## End check logfile.
if bLog:
logger = logging.getLogger('opmenu')
hdlr = logging.FileHandler(LOGFILE)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
#own classes
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
class navegacion:
menu = []
def addMenu(self,opcion):
#guarda opcion de menu en el array para poder guardar el menu de navegacion
self.menu.append(str(opcion))
def getMenu(self):
return str(self.menu[len(self.menu)-1])
def getMenuAnt(self):
try:
anterior= self.menu.pop()
return str(self.menu[len(self.menu)-1])
except IndexError:
return 1
def printruta(self):
ruta = ""
contador = 0
for i in self.menu:
if (ruta == ""):
ruta = str(self.menu[contador])
else:
ruta = ruta + " -> " + str(self.menu[contador])
contador = contador + 1
return str(ruta)
#Print columns
def generateColumns():
rows, columns = os.popen('stty size', 'r').read().split()
print bcolors.FAIL + "=" * int(columns) + bcolors.ENDC
#Bloque de programa
def cabecera():
server = str(os.popen("hostname").readlines())
server = server.replace("['","")
server = server.replace("\\n']","")
mapa = n.printruta().replace(".mnu","")
os.system("clear")
print "PyOpeMenu"
print _("Server") + " --> " +bcolors.WARNING + server + bcolors.ENDC
print _("Map") + " --> " + bcolors.OKGREEN + mapa + bcolors.ENDC
print ""
generateColumns()
def titulo(texto):
print
print bcolors.OKBLUE + "Menu: " + bcolors.HEADER + texto + bcolors.ENDC
print
def opcion(numero, texto):
print bcolors.FAIL + "\t" +numero + "\t"+bcolors.ENDC + texto
def executa(opciones):
seleccio = raw_input( _("Please select an option.") + " -> ")
runcomand = False
try:
if (seleccio == "q"):
readfile(n.getMenuAnt())
else:
if ( seleccio != "0"):
seleccio = int(seleccio) - 1
tipo = opciones[int(seleccio)].split(".")
if str(tipo[-1]).find("mnu") != -1:
n.addMenu(opciones[int(seleccio)])
#print tipo[-1]
#raw_input (n.getMenu())
#readfile(n.getMenu())
else:
comando = str((opciones[int(seleccio)]))
if ( comando.find("requestconfirm") != -1 ):
comando = comando.replace('requestconfirm','')
if ( query_yes_no( _("Are you sure?") , "no") ):
runcomand = True
else:
runcomand = True
if ( runcomand ):
if bLog:
logger.info (os.getenv('SSH_CLIENT','') + ' ' + os.getenv('USER','') + ' ' + comando)
os.system (comando)
readfile(n.getMenu())
except ValueError:
if bLog:
logger.warning (os.getenv('SSH_CLIENT','') + ' ' + os.getenv('USER','') + ' ' + _("Invalid value option. Enter to continue...") )
raw_input( _("Invalid value option. Enter to continue... ") )
readfile(n.getMenu())
except IndexError:
if bLog:
logger.warning (os.getenv('SSH_CLIENT','') + ' ' + os.getenv('USER','') + ' ' + _("Invalid index option. Enter to continue... ") )
raw_input( _("Invalid index option. Enter to continue... ") )
readfile(n.getMenu())
def readfile(fichero):
#lee fichero .mnu
try:
menufile = file(INSTALL_PATH + str(fichero))
printmenu(menufile)
except IOError:
#raw_input (str(fichero))
if (str(fichero) == "1"):
os.system("clear")
exit
else:
readfile(n.getMenuAnt())
def printmenu(menu):
#printa menu
orden = 0
opciones =[]
cabecera()
for line in menu:
linea = str(line)
linea = linea.replace( _("Press \"q\" to exit.\n") ,"")
linea = linea.replace(":", "")
linea = linea.replace("\n","")
if linea.find("MENU ") != -1:
#print ("Opcion de menu" + line)
palabras = linea.split(" ")
opciones.append(palabras[-1])
elif (linea.find("sudo") != -1) or (linea.find(".sh") != -1):
opciones.append(linea)
else:
if (orden==0):
#print ("Titulo " + linea)
titulo (linea)
orden = orden + 1
else:
#print ("Opcion " + str(orden) + " " + linea)
if len(linea) > 0:
opcion (str(orden),linea)
orden = orden + 1
print
print ( _("Press \"q\" to exit.\n") )
generateColumns()
print
executa(opciones)
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes" or "no".
"""
valid = {"yes":True, "y":True, "ye":True,
"no":False, "n":False}
if default == None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError( _("invalid default answer:") + " '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write( _("Please respond with 'yes' or 'no' ") + "(or 'y' or 'n').\n")
try:
if bLog:
logger.info(os.getenv('SSH_CLIENT','') + ' ' + os.getenv('USER','') + ' ' + _("Entering the opmenu") )
n = navegacion()
n.addMenu('main.mnu')
readfile(n.getMenu())
if bLog:
logger.info(os.getenv('SSH_CLIENT','') + ' ' + os.getenv('USER','') + ' ' + _("Leaving the opmenu") )
except KeyboardInterrupt:
if bLog:
logger.info(os.getenv('SSH_CLIENT','') + ' ' + os.getenv('USER','') + ' ' + _("Leaving the opmenu (Interrupt)") )
print _("Bye")