pretty_sln.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 Google Inc. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Prints the information in a sln file in a diffable way.
  6. It first outputs each projects in alphabetical order with their
  7. dependencies.
  8. Then it outputs a possible build order.
  9. """
  10. __author__ = 'nsylvain (Nicolas Sylvain)'
  11. import os
  12. import re
  13. import sys
  14. import pretty_vcproj
  15. def BuildProject(project, built, projects, deps):
  16. # if all dependencies are done, we can build it, otherwise we try to build the
  17. # dependency.
  18. # This is not infinite-recursion proof.
  19. for dep in deps[project]:
  20. if dep not in built:
  21. BuildProject(dep, built, projects, deps)
  22. print project
  23. built.append(project)
  24. def ParseSolution(solution_file):
  25. # All projects, their clsid and paths.
  26. projects = dict()
  27. # A list of dependencies associated with a project.
  28. dependencies = dict()
  29. # Regular expressions that matches the SLN format.
  30. # The first line of a project definition.
  31. begin_project = re.compile(r'^Project\("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942'
  32. r'}"\) = "(.*)", "(.*)", "(.*)"$')
  33. # The last line of a project definition.
  34. end_project = re.compile('^EndProject$')
  35. # The first line of a dependency list.
  36. begin_dep = re.compile(
  37. r'ProjectSection\(ProjectDependencies\) = postProject$')
  38. # The last line of a dependency list.
  39. end_dep = re.compile('EndProjectSection$')
  40. # A line describing a dependency.
  41. dep_line = re.compile(' *({.*}) = ({.*})$')
  42. in_deps = False
  43. solution = open(solution_file)
  44. for line in solution:
  45. results = begin_project.search(line)
  46. if results:
  47. # Hack to remove icu because the diff is too different.
  48. if results.group(1).find('icu') != -1:
  49. continue
  50. # We remove "_gyp" from the names because it helps to diff them.
  51. current_project = results.group(1).replace('_gyp', '')
  52. projects[current_project] = [results.group(2).replace('_gyp', ''),
  53. results.group(3),
  54. results.group(2)]
  55. dependencies[current_project] = []
  56. continue
  57. results = end_project.search(line)
  58. if results:
  59. current_project = None
  60. continue
  61. results = begin_dep.search(line)
  62. if results:
  63. in_deps = True
  64. continue
  65. results = end_dep.search(line)
  66. if results:
  67. in_deps = False
  68. continue
  69. results = dep_line.search(line)
  70. if results and in_deps and current_project:
  71. dependencies[current_project].append(results.group(1))
  72. continue
  73. # Change all dependencies clsid to name instead.
  74. for project in dependencies:
  75. # For each dependencies in this project
  76. new_dep_array = []
  77. for dep in dependencies[project]:
  78. # Look for the project name matching this cldis
  79. for project_info in projects:
  80. if projects[project_info][1] == dep:
  81. new_dep_array.append(project_info)
  82. dependencies[project] = sorted(new_dep_array)
  83. return (projects, dependencies)
  84. def PrintDependencies(projects, deps):
  85. print "---------------------------------------"
  86. print "Dependencies for all projects"
  87. print "---------------------------------------"
  88. print "-- --"
  89. for (project, dep_list) in sorted(deps.items()):
  90. print "Project : %s" % project
  91. print "Path : %s" % projects[project][0]
  92. if dep_list:
  93. for dep in dep_list:
  94. print " - %s" % dep
  95. print ""
  96. print "-- --"
  97. def PrintBuildOrder(projects, deps):
  98. print "---------------------------------------"
  99. print "Build order "
  100. print "---------------------------------------"
  101. print "-- --"
  102. built = []
  103. for (project, _) in sorted(deps.items()):
  104. if project not in built:
  105. BuildProject(project, built, projects, deps)
  106. print "-- --"
  107. def PrintVCProj(projects):
  108. for project in projects:
  109. print "-------------------------------------"
  110. print "-------------------------------------"
  111. print project
  112. print project
  113. print project
  114. print "-------------------------------------"
  115. print "-------------------------------------"
  116. project_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[1]),
  117. projects[project][2]))
  118. pretty = pretty_vcproj
  119. argv = [ '',
  120. project_path,
  121. '$(SolutionDir)=%s\\' % os.path.dirname(sys.argv[1]),
  122. ]
  123. argv.extend(sys.argv[3:])
  124. pretty.main(argv)
  125. def main():
  126. # check if we have exactly 1 parameter.
  127. if len(sys.argv) < 2:
  128. print 'Usage: %s "c:\\path\\to\\project.sln"' % sys.argv[0]
  129. return 1
  130. (projects, deps) = ParseSolution(sys.argv[1])
  131. PrintDependencies(projects, deps)
  132. PrintBuildOrder(projects, deps)
  133. if '--recursive' in sys.argv:
  134. PrintVCProj(projects)
  135. return 0
  136. if __name__ == '__main__':
  137. sys.exit(main())