graph-tool 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python3
  2. # Simple graph query utility
  3. # useful for getting answers from .dot files produced by bitbake -g
  4. #
  5. # Written by: Paul Eggleton <paul.eggleton@linux.intel.com>
  6. #
  7. # Copyright 2013 Intel Corporation
  8. #
  9. # SPDX-License-Identifier: GPL-2.0-only
  10. #
  11. import sys
  12. import os
  13. import argparse
  14. scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib'))
  15. sys.path.insert(0, scripts_lib_path)
  16. import argparse_oe
  17. def get_path_networkx(dotfile, fromnode, tonode):
  18. try:
  19. import networkx
  20. except ImportError:
  21. print('ERROR: Please install the networkx python module')
  22. sys.exit(1)
  23. graph = networkx.DiGraph(networkx.nx_pydot.read_dot(dotfile))
  24. def node_missing(node):
  25. import difflib
  26. close_matches = difflib.get_close_matches(node, graph.nodes(), cutoff=0.7)
  27. if close_matches:
  28. print('ERROR: no node "%s" in graph. Close matches:\n %s' % (node, '\n '.join(close_matches)))
  29. sys.exit(1)
  30. if not fromnode in graph:
  31. node_missing(fromnode)
  32. if not tonode in graph:
  33. node_missing(tonode)
  34. return networkx.all_simple_paths(graph, source=fromnode, target=tonode)
  35. def find_paths(args):
  36. path = None
  37. for path in get_path_networkx(args.dotfile, args.fromnode, args.tonode):
  38. print(" -> ".join(map(str, path)))
  39. if not path:
  40. print("ERROR: no path from %s to %s in graph" % (args.fromnode, args.tonode))
  41. return 1
  42. def filter_graph(args):
  43. import fnmatch
  44. exclude_tasks = []
  45. if args.exclude_tasks:
  46. for task in args.exclude_tasks.split(','):
  47. if not task.startswith('do_'):
  48. task = 'do_%s' % task
  49. exclude_tasks.append(task)
  50. def checkref(strval):
  51. strval = strval.strip().strip('"')
  52. target, taskname = strval.rsplit('.', 1)
  53. if exclude_tasks:
  54. for extask in exclude_tasks:
  55. if fnmatch.fnmatch(taskname, extask):
  56. return False
  57. if strval in args.ref or target in args.ref:
  58. return True
  59. return False
  60. with open(args.infile, 'r') as f:
  61. for line in f:
  62. line = line.rstrip()
  63. if line.startswith(('digraph', '}')):
  64. print(line)
  65. elif '->' in line:
  66. linesplit = line.split('->')
  67. if checkref(linesplit[0]) and checkref(linesplit[1]):
  68. print(line)
  69. elif (not args.no_nodes) and checkref(line.split()[0]):
  70. print(line)
  71. def main():
  72. parser = argparse_oe.ArgumentParser(description='Small utility for working with .dot graph files')
  73. subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
  74. subparsers.required = True
  75. parser_find_paths = subparsers.add_parser('find-paths',
  76. help='Find all of the paths between two nodes in a dot graph',
  77. description='Finds all of the paths between two nodes in a dot graph')
  78. parser_find_paths.add_argument('dotfile', help='.dot graph to search in')
  79. parser_find_paths.add_argument('fromnode', help='starting node name')
  80. parser_find_paths.add_argument('tonode', help='ending node name')
  81. parser_find_paths.set_defaults(func=find_paths)
  82. parser_filter = subparsers.add_parser('filter',
  83. help='Pare down a task graph to contain only the specified references',
  84. description='Pares down a task-depends.dot graph produced by bitbake -g to contain only the specified references')
  85. parser_filter.add_argument('infile', help='Input file')
  86. parser_filter.add_argument('ref', nargs='+', help='Reference to include (either recipe/target name or full target.taskname specification)')
  87. parser_filter.add_argument('-n', '--no-nodes', action='store_true', help='Skip node formatting lines')
  88. parser_filter.add_argument('-x', '--exclude-tasks', help='Comma-separated list of tasks to exclude (do_ prefix optional, wildcards allowed)')
  89. parser_filter.set_defaults(func=filter_graph)
  90. args = parser.parse_args()
  91. ret = args.func(args)
  92. return ret
  93. if __name__ == "__main__":
  94. ret = main()
  95. sys.exit(ret)