Advertisement
Guest User

systemd-depends.py

a guest
Sep 1st, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. #!/usr/bin/python
  2. # simple parser for systemd dependeny dot data
  3. # parameter: -c for colored output
  4. # Example usage:
  5. # systemctl dot | python systemd-depends.py -c | less -R
  6. '''
  7.            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  8.                    Version 2, December 2004
  9.  
  10. Copyright (C) 2012 ProgAndy
  11.  
  12. Everyone is permitted to copy and distribute verbatim or modified
  13. copies of this license document, and changing it is allowed as long
  14. as the name is changed.
  15.  
  16.            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  17.   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  18.  
  19.  0. You just DO WHAT THE FUCK YOU WANT TO.
  20. '''
  21.  
  22. import sys
  23. depends = dict()
  24.  
  25. class shellcolors:
  26.     UNIT = '\033[95m'
  27.     BLUE = '\033[94m'
  28.     GREEN = '\033[92m'
  29.     WARNING = '\033[93m'
  30.     FAIL = '\033[91m'
  31.     DEFAULT = '\033[0m'
  32.  
  33.     def disable(self):
  34.         self.UNIT = ''
  35.         self.BLUE = ''
  36.         self.GREEN = ''
  37.         self.WARNING = ''
  38.         self.FAIL = ''
  39.         self.DEFAULT = ''
  40. shellcolor = shellcolors()
  41.  
  42.  
  43. def dep2shellcol(c):
  44.     if (c == "Conflicts"):
  45.         return shellcolor.FAIL
  46.     elif (c == "Requires"):
  47.         return shellcolor.BLUE
  48.     elif (c == "Wants"):
  49.         return shellcolor.GREEN
  50.     elif (c == "Requisite"):
  51.         return shellcolor.WARNING
  52.     else:
  53.         return ""
  54.  
  55.    
  56. def col2dep(c):
  57.     if (c.startswith("red")):
  58.         return "Conflicts"
  59.     elif (c.startswith("green")):
  60.         return "After"
  61.     elif (c.startswith("blue")):
  62.         return "Requisite"
  63.     elif (c.startswith("grey")):
  64.         return "Wants"
  65.     elif (c.startswith("black")):
  66.         return "Requires"
  67.     else:
  68.         return "unknown"
  69.  
  70. def add_dependency(unit, dtype, dep):
  71.     if unit in depends:
  72.         depends[unit].append([dtype, dep])
  73.     else:
  74.         depends[unit] = [[dtype, dep]]
  75.  
  76. # enable colors if first argument is -c
  77. if (len(sys.argv) < 2 or not sys.argv[1].startswith("-c")):
  78.     shellcolor.disable()
  79.  
  80. # parse systemctl dot data from stdinput
  81. for line in sys.stdin.readlines():
  82.     if line.startswith('\t"'):
  83.         firstend = line.index('"->"')
  84.         secondend = line.index('" [color="', firstend+4)
  85.         add_dependency(line[2:firstend], col2dep(line[secondend+10:]), line[firstend+4:secondend])
  86.  
  87. # print dependencies for each unit
  88. for unit in depends:
  89.     print (shellcolor.UNIT + unit + ":")
  90.     for dep in depends[unit]:
  91.         print ("\t" + dep2shellcol(dep[0]) + dep[0].ljust(10) + shellcolor.DEFAULT + "\t" + dep[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement