verify-publishing-bot.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # Copyright 2019 The Kubernetes 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. from __future__ import print_function
  16. import fnmatch
  17. import os
  18. import sys
  19. import json
  20. def get_gomod_dependencies(rootdir, components):
  21. all_dependencies = {}
  22. for component in components:
  23. with open(os.path.join(rootdir, component, "go.mod")) as f:
  24. print(component + " dependencies")
  25. all_dependencies[component] = []
  26. lines = list(set(f))
  27. lines.sort()
  28. for line in lines:
  29. for dep in components:
  30. if dep == component:
  31. continue
  32. if ("k8s.io/" + dep + " v0.0.0") not in line:
  33. continue
  34. print("\t"+dep)
  35. if dep not in all_dependencies[component]:
  36. all_dependencies[component].append(dep)
  37. return all_dependencies
  38. def get_rules_dependencies(rules_file):
  39. import yaml
  40. with open(rules_file) as f:
  41. data = yaml.load(f)
  42. return data
  43. def main():
  44. rootdir = os.path.dirname(__file__) + "/../"
  45. rootdir = os.path.abspath(rootdir)
  46. components = []
  47. for component in os.listdir(rootdir + '/staging/src/k8s.io/'):
  48. components.append(component)
  49. components.sort()
  50. rules_file = "/staging/publishing/rules.yaml"
  51. try:
  52. import yaml
  53. except ImportError:
  54. print("Please install missing pyyaml module and re-run %s" % sys.argv[0])
  55. sys.exit(1)
  56. rules_dependencies = get_rules_dependencies(rootdir + rules_file)
  57. gomod_dependencies = get_gomod_dependencies(rootdir + '/staging/src/k8s.io/', components)
  58. processed_repos = []
  59. for rule in rules_dependencies["rules"]:
  60. branch = rule["branches"][0]
  61. if branch["name"] != "master":
  62. raise Exception("cannot find master branch for destination %s" % rule["destination"])
  63. if branch["source"]["branch"] != "master":
  64. raise Exception("cannot find master source branch for destination %s" % rule["destination"])
  65. print("processing : %s" % rule["destination"])
  66. if rule["destination"] not in gomod_dependencies:
  67. raise Exception("missing go.mod for %s" % rule["destination"])
  68. processed_repos.append(rule["destination"])
  69. for dep in set(gomod_dependencies[rule["destination"]]):
  70. found = False
  71. if "dependencies" in branch:
  72. for dep2 in branch["dependencies"]:
  73. if dep2["branch"] != "master":
  74. raise Exception("Looking for master branch and found : %s for destination", dep2,
  75. rule["destination"])
  76. if dep2["repository"] == dep:
  77. found = True
  78. else:
  79. raise Exception(
  80. "Please add %s as dependencies under destination %s in %s" % (gomod_dependencies[rule["destination"]], rule["destination"], rules_file))
  81. if not found:
  82. raise Exception("Please add %s as a dependency under destination %s in %s" % (dep, rule["destination"], rules_file))
  83. else:
  84. print(" found dependency %s" % dep)
  85. items = set(gomod_dependencies.keys()) - set(processed_repos)
  86. if len(items) > 0:
  87. raise Exception("missing rules for %s" % ','.join(str(s) for s in items))
  88. print("Done.")
  89. if __name__ == "__main__":
  90. sys.exit(main())