build_cleaner.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # produces cleaner build.yaml files
  16. import collections
  17. import os
  18. import sys
  19. import yaml
  20. TEST = (os.environ.get('TEST', 'false') == 'true')
  21. _TOP_LEVEL_KEYS = [
  22. 'settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages'
  23. ]
  24. _ELEM_KEYS = [
  25. 'name', 'gtest', 'cpu_cost', 'flaky', 'build', 'run', 'language',
  26. 'public_headers', 'headers', 'src', 'deps'
  27. ]
  28. def repr_ordered_dict(dumper, odict):
  29. return dumper.represent_mapping('tag:yaml.org,2002:map',
  30. list(odict.items()))
  31. yaml.add_representer(collections.OrderedDict, repr_ordered_dict)
  32. def _rebuild_as_ordered_dict(indict, special_keys):
  33. outdict = collections.OrderedDict()
  34. for key in sorted(indict.keys()):
  35. if '#' in key:
  36. outdict[key] = indict[key]
  37. for key in special_keys:
  38. if key in indict:
  39. outdict[key] = indict[key]
  40. for key in sorted(indict.keys()):
  41. if key in special_keys:
  42. continue
  43. if '#' in key:
  44. continue
  45. outdict[key] = indict[key]
  46. return outdict
  47. def _clean_elem(indict):
  48. for name in ['public_headers', 'headers', 'src']:
  49. if name not in indict:
  50. continue
  51. inlist = indict[name]
  52. protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto')
  53. others = set(x for x in inlist if x not in protos)
  54. indict[name] = protos + sorted(others)
  55. return _rebuild_as_ordered_dict(indict, _ELEM_KEYS)
  56. def cleaned_build_yaml_dict_as_string(indict):
  57. """Takes dictionary which represents yaml file and returns the cleaned-up yaml string"""
  58. js = _rebuild_as_ordered_dict(indict, _TOP_LEVEL_KEYS)
  59. for grp in ['filegroups', 'libs', 'targets']:
  60. if grp not in js:
  61. continue
  62. js[grp] = sorted([_clean_elem(x) for x in js[grp]],
  63. key=lambda x: (x.get('language', '_'), x['name']))
  64. output = yaml.dump(js, indent=2, width=80, default_flow_style=False)
  65. # massage out trailing whitespace
  66. lines = []
  67. for line in output.splitlines():
  68. lines.append(line.rstrip() + '\n')
  69. output = ''.join(lines)
  70. return output
  71. if __name__ == '__main__':
  72. for filename in sys.argv[1:]:
  73. with open(filename) as f:
  74. js = yaml.load(f, Loader=yaml.FullLoader)
  75. output = cleaned_build_yaml_dict_as_string(js)
  76. if TEST:
  77. with open(filename) as f:
  78. if not f.read() == output:
  79. raise Exception(
  80. 'Looks like build-cleaner.py has not been run for file "%s"?'
  81. % filename)
  82. else:
  83. with open(filename, 'w') as f:
  84. f.write(output)