Guest User

Untitled

a guest
Aug 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import argparse
  4. import os.path
  5. from cStringIO import StringIO
  6. import subprocess
  7. import sys
  8.  
  9. parser = argparse.ArgumentParser(description="Create a graphviz file of a makefile")
  10. parser.add_argument("-C", "--directory", metavar="DIR", help="Change to directory DIR before reading the makefiles")
  11. parser.add_argument("-f", "--file", help="Use FILE as makefile")
  12. parser.add_argument("-o", "--output", default="makefile.gv", help="Output file")
  13. parser.add_argument("-l", "--limit", type=int, help="Limit number of underscores in name")
  14. parser.add_argument("target", nargs=argparse.REMAINDER, help="Make targets")
  15.  
  16. args = parser.parse_args()
  17.  
  18. makeargs = ["make", "-pn"]
  19. if args.directory is not None:
  20. makeargs += ["-C", args.directory]
  21. if args.file is not None:
  22. makeargs += ["-f", args.file]
  23. makeargs += args.target
  24.  
  25. makeout = subprocess.check_output(makeargs)
  26. makeoutfo = StringIO(makeout)
  27.  
  28. lastfiles = 0
  29.  
  30.  
  31. for line in makeoutfo:
  32. if line == "# Files\n":
  33. lastfiles = makeoutfo.tell()
  34.  
  35. makeoutfo.seek(lastfiles)
  36.  
  37. with open(args.output, "wb") as outfile:
  38. outfile.write("digraph{splines=true;\n")
  39. for line in makeoutfo:
  40. linestrip = line.strip()
  41. if linestrip == "" or linestrip[0] == '#' or line[0] == '\t' or ":" not in line:
  42. continue
  43. target, depends = line.split(":", 1)
  44. target = target.strip()
  45. if target == "" or target[0] == ".":
  46. continue
  47. if args.limit is not None and target.count("_") >= args.limit:
  48. continue
  49. outfile.write("\"%s\" [label=\"%s\"];\n" % (target, os.path.basename(target)))
  50. deplist = [x.strip() for x in depends.split(" ") if x.strip() != ""]
  51. for dep in deplist:
  52. if dep[0] == ".":
  53. continue
  54. if args.limit is not None and dep.count("_") >= args.limit:
  55. continue
  56. outfile.write("\"%s\" [label=\"%s\"];\n" % (dep, os.path.basename(dep)))
  57. outfile.write("\"%s\" -> \"%s\";\n" % (target, dep))
  58. outfile.write("}\n")
Advertisement
Add Comment
Please, Sign In to add comment