please feel free too try it
;) for a better world, just linux
#! /usr/bin/env python
__author__ = "Jonathan Colon"
__email__ = "rebe...@gmail.com"
__version__ = "0.4"
__productname__ = "rc-service"
__description__ = "Gentoo Service Manipulation Tool"
# ==================================
# ==> Python Modules Importations <==
# ==================================
import os,sys,copy,sets
from optparse import OptionParser, OptionGroup
from string import strip
from output import red,blue,green,bold,turquoise
# ==================================
# ==> User Verification <==
# ==================================
if os.geteuid() != 0:
print "Must be root to run this program"
sys.exit()
# =============================================================
# ==> Funtion to manipulate services (Ex: start,stop etc..) <==
# =============================================================
def deamon():
arguments = copy.copy(sys.argv)
arguments[0:2] = []
services = os.listdir("/etc/init.d/")
for options in arguments:
print blue(bold(options.upper()))
if options in services:
os.system('/etc/init.d/' + options + ' ' +
strip(sys.argv[1], '-'))
else:
msg = "Service " + options +" not found in the system"
print msg,
stty('!!',msg),
# ===========================================================
# ==> Funtion to calculate tty size for the status output <==
# ===========================================================
def stty(state, message):
get_stty = os.popen("stty size").read().split()
size_stty = int(get_stty[1]) - 7
wspace = ' '
if state == 'off' or state == '!!':
msg = blue("[ "+ red(state)+ blue(" ]"))
else:
msg = blue("[ "+ green(state)+ blue(" ]"))
print wspace * (size_stty - (len(state) + len(message))),msg
# =============================
# ==> Init var declarations <==
# =============================
started = os.listdir('/var/lib/init.d/started/')
service = os.listdir('/etc/init.d/')
boot = os.listdir('/etc/runlevels/boot/')
default = os.listdir('/etc/runlevels/default/')
nonetwork = os.listdir('/etc/runlevels/nonetwork/')
runlevels = boot + default + nonetwork
no_level = sets.Set(service).difference(runlevels)
unsigned = list(no_level)
all = [boot,default,nonetwork,unsigned]
args = ['unsigned','nonetwork','default','boot']
# =====================================================
# ==> Funtion to calculate runevels services status <==
# =====================================================
def statusd(level):
rnlv = level
rnlv.sort()
if sys.argv[2] in args:
print "Runlevel: " + turquoise(sys.argv[2])
for ser in rnlv:
if ser in started:
print " " + ser,
stty('on',ser)
else:
print " " + ser,
stty('off',ser)
else:
print "Runlevel: " + turquoise(args.pop())
for ser in rnlv:
if ser in started:
print " " + ser,
stty('on',ser)
else:
print " " + ser,
stty('off',ser)
# ======================================================
# ==> Principal Funtion. Exp: menu, correct arguments<==
# ======================================================
def use():
usage = "%prog [-h] [--argument] services"
opciones = OptionParser(usage=usage, version="%prog 0.4")
grupo = OptionGroup(opciones, "arguments")
grupo.add_option("--start", action="store_true", help="Start
service.")
grupo.add_option("--status", action="store_true", help="Status of
service.")
grupo.add_option("--restart", action="store_true", help="Restart
service.")
grupo.add_option("--stop", action="store_true", help="Stop
service.")
grupo.add_option("--zap", action="store_true", help="Slay
service.")
status = OptionGroup(opciones, "Status arguments")
status.add_option("--status all", action="store_true", help="Status
of all services.")
status.add_option("--status boot", action="store_true",
help="Status of boot services.")
status.add_option("--status default", action="store_true",
help="Status of default services.")
status.add_option("--status nonetwork", action="store_true",
help="Status of nonetwork services.")
status.add_option("--status unsigned", action="store_true",
help="Status of unsigned services.")
opciones.add_option_group(grupo)
opciones.add_option_group(status)
(options, args) = opciones.parse_args()
status_arg = ['all','boot','default','nonetwork','unsigned']
tags = ['--start','--stop','--zap','--restart','--status']
if len(sys.argv) <= 2:
print chr(7) + opciones.error("incorrect number of arguments")
elif sys.argv[1] not in tags:
print "Usage: rc-service [-h] [--argument] services"
print ""
print "rc-service: error: no such option:", sys.argv[1]
sys.exit()
elif sys.argv[2] in status_arg:
if sys.argv[2] == 'boot':
statusd(boot)
elif sys.argv[2] == 'default':
statusd(default)
elif sys.argv[2] == 'all':
map(statusd,all)
elif sys.argv[2] == 'unsigned':
statusd(unsigned)
elif sys.argv[2] == 'nonetwork':
statusd(nonetwork)
else:
deamon()
if __name__ == "__main__":
use()