oe-depends-dot 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2018 Wind River Systems, Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. # See the GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. import os
  18. import sys
  19. import argparse
  20. import logging
  21. import re
  22. class Dot(object):
  23. def __init__(self):
  24. parser = argparse.ArgumentParser(
  25. description="Analyse recipe-depends.dot generated by bitbake -g",
  26. epilog="Use %(prog)s --help to get help")
  27. parser.add_argument("dotfile",
  28. help = "Specify the dotfile", nargs = 1, action='store', default='')
  29. parser.add_argument("-k", "--key",
  30. help = "Specify the key, e.g., recipe name",
  31. action="store", default='')
  32. parser.add_argument("-d", "--depends",
  33. help = "Print the key's dependencies",
  34. action="store_true", default=False)
  35. parser.add_argument("-w", "--why",
  36. help = "Print why the key is built",
  37. action="store_true", default=False)
  38. parser.add_argument("-r", "--remove",
  39. help = "Remove duplicated dependencies to reduce the size of the dot files."
  40. " For example, A->B, B->C, A->C, then A->C can be removed.",
  41. action="store_true", default=False)
  42. self.args = parser.parse_args()
  43. if len(sys.argv) != 3 and len(sys.argv) < 5:
  44. print('ERROR: Not enough args, see --help for usage')
  45. def main(self):
  46. #print(self.args.dotfile[0])
  47. # The format is {key: depends}
  48. depends = {}
  49. with open(self.args.dotfile[0], 'r') as f:
  50. for line in f.readlines():
  51. if ' -> ' not in line:
  52. continue
  53. line_no_quotes = line.replace('"', '')
  54. m = re.match("(.*) -> (.*)", line_no_quotes)
  55. if not m:
  56. print('WARNING: Found unexpected line: %s' % line)
  57. continue
  58. key = m.group(1)
  59. if key == "meta-world-pkgdata":
  60. continue
  61. dep = m.group(2)
  62. if key in depends:
  63. if not key in depends[key]:
  64. depends[key].add(dep)
  65. else:
  66. print('WARNING: Fonud duplicated line: %s' % line)
  67. else:
  68. depends[key] = set()
  69. depends[key].add(dep)
  70. if self.args.remove:
  71. reduced_depends = {}
  72. for k, deps in depends.items():
  73. child_deps = set()
  74. added = set()
  75. # Both direct and indirect depends are already in the dict, so
  76. # we don't have to do this recursively.
  77. for dep in deps:
  78. if dep in depends:
  79. child_deps |= depends[dep]
  80. reduced_depends[k] = deps - child_deps
  81. outfile= '%s-reduced%s' % (self.args.dotfile[0][:-4], self.args.dotfile[0][-4:])
  82. with open(outfile, 'w') as f:
  83. print('Saving reduced dot file to %s' % outfile)
  84. f.write('digraph depends {\n')
  85. for k, v in reduced_depends.items():
  86. for dep in v:
  87. f.write('"%s" -> "%s"\n' % (k, dep))
  88. f.write('}\n')
  89. sys.exit(0)
  90. if self.args.key not in depends:
  91. print("ERROR: Can't find key %s in %s" % (self.args.key, self.args.dotfile[0]))
  92. sys.exit(1)
  93. if self.args.depends:
  94. if self.args.key in depends:
  95. print('Depends: %s' % ' '.join(depends[self.args.key]))
  96. reverse_deps = []
  97. if self.args.why:
  98. for k, v in depends.items():
  99. if self.args.key in v and not k in reverse_deps:
  100. reverse_deps.append(k)
  101. print('Because: %s' % ' '.join(reverse_deps))
  102. if __name__ == "__main__":
  103. try:
  104. dot = Dot()
  105. ret = dot.main()
  106. except Exception as esc:
  107. ret = 1
  108. import traceback
  109. traceback.print_exc()
  110. sys.exit(ret)