verify-publishing-bot.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 + " =>") 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 this no longer exists in master
  62. if rule["destination"] not in gomod_dependencies:
  63. # Make sure we don't include a rule to publish it from master
  64. for branch in rule["branches"]:
  65. if branch["name"] == "master":
  66. raise Exception("cannot find master branch for destination %s" % rule["destination"])
  67. # And skip validation of publishing rules for it
  68. continue
  69. if branch["name"] != "master":
  70. raise Exception("cannot find master branch for destination %s" % rule["destination"])
  71. if branch["source"]["branch"] != "master":
  72. raise Exception("cannot find master source branch for destination %s" % rule["destination"])
  73. print("processing : %s" % rule["destination"])
  74. if rule["destination"] not in gomod_dependencies:
  75. raise Exception("missing go.mod for %s" % rule["destination"])
  76. processed_repos.append(rule["destination"])
  77. processed_deps = []
  78. for dep in set(gomod_dependencies[rule["destination"]]):
  79. found = False
  80. if "dependencies" in branch:
  81. for dep2 in branch["dependencies"]:
  82. processed_deps.append(dep2["repository"])
  83. if dep2["branch"] != "master":
  84. raise Exception("Looking for master branch and found : %s for destination", dep2,
  85. rule["destination"])
  86. if dep2["repository"] == dep:
  87. found = True
  88. else:
  89. raise Exception(
  90. "Please add %s as dependencies under destination %s in %s" % (gomod_dependencies[rule["destination"]], rule["destination"], rules_file))
  91. if not found:
  92. raise Exception("Please add %s as a dependency under destination %s in %s" % (dep, rule["destination"], rules_file))
  93. else:
  94. print(" found dependency %s" % dep)
  95. extraDeps = set(processed_deps) - set(gomod_dependencies[rule["destination"]])
  96. if len(extraDeps) > 0:
  97. raise Exception("extra dependencies in rules for %s: %s" % (rule["destination"], ','.join(str(s) for s in extraDeps)))
  98. items = set(gomod_dependencies.keys()) - set(processed_repos)
  99. if len(items) > 0:
  100. raise Exception("missing rules for %s" % ','.join(str(s) for s in items))
  101. print("Done.")
  102. if __name__ == "__main__":
  103. sys.exit(main())