Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # simple parser for systemd dependeny dot data
- # parameter: -c for colored output
- # Example usage:
- # systemctl dot | python systemd-depends.py -c | less -R
- '''
- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- Version 2, December 2004
- Copyright (C) 2012 ProgAndy
- Everyone is permitted to copy and distribute verbatim or modified
- copies of this license document, and changing it is allowed as long
- as the name is changed.
- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- 0. You just DO WHAT THE FUCK YOU WANT TO.
- '''
- import sys
- depends = dict()
- class shellcolors:
- UNIT = '\033[95m'
- BLUE = '\033[94m'
- GREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- DEFAULT = '\033[0m'
- def disable(self):
- self.UNIT = ''
- self.BLUE = ''
- self.GREEN = ''
- self.WARNING = ''
- self.FAIL = ''
- self.DEFAULT = ''
- shellcolor = shellcolors()
- def dep2shellcol(c):
- if (c == "Conflicts"):
- return shellcolor.FAIL
- elif (c == "Requires"):
- return shellcolor.BLUE
- elif (c == "Wants"):
- return shellcolor.GREEN
- elif (c == "Requisite"):
- return shellcolor.WARNING
- else:
- return ""
- def col2dep(c):
- if (c.startswith("red")):
- return "Conflicts"
- elif (c.startswith("green")):
- return "After"
- elif (c.startswith("blue")):
- return "Requisite"
- elif (c.startswith("grey")):
- return "Wants"
- elif (c.startswith("black")):
- return "Requires"
- else:
- return "unknown"
- def add_dependency(unit, dtype, dep):
- if unit in depends:
- depends[unit].append([dtype, dep])
- else:
- depends[unit] = [[dtype, dep]]
- # enable colors if first argument is -c
- if (len(sys.argv) < 2 or not sys.argv[1].startswith("-c")):
- shellcolor.disable()
- # parse systemctl dot data from stdinput
- for line in sys.stdin.readlines():
- if line.startswith('\t"'):
- firstend = line.index('"->"')
- secondend = line.index('" [color="', firstend+4)
- add_dependency(line[2:firstend], col2dep(line[secondend+10:]), line[firstend+4:secondend])
- # print dependencies for each unit
- for unit in depends:
- print (shellcolor.UNIT + unit + ":")
- for dep in depends[unit]:
- print ("\t" + dep2shellcol(dep[0]) + dep[0].ljust(10) + shellcolor.DEFAULT + "\t" + dep[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement