staleness_test_lib.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2009-2021, Google LLC
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of Google LLC nor the
  14. # names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. """Shared code for validating generated_file_staleness_test() rules.
  28. This code is used by test scripts generated from
  29. generated_file_staleness_test() rules.
  30. """
  31. from __future__ import absolute_import
  32. from __future__ import print_function
  33. import sys
  34. import os
  35. from shutil import copyfile
  36. class _FilePair(object):
  37. """Represents a single (target, generated) file pair."""
  38. def __init__(self, target, generated):
  39. self.target = target
  40. self.generated = generated
  41. class Config(object):
  42. """Represents the configuration for a single staleness test target."""
  43. def __init__(self, file_list):
  44. # Duplicate to avoid modifying our arguments.
  45. file_list = list(file_list)
  46. # The file list contains a few other bits of information at the end.
  47. # This is packed by the code in build_defs.bzl.
  48. self.target_name = file_list.pop()
  49. self.package_name = file_list.pop()
  50. self.pattern = file_list.pop()
  51. self.file_list = file_list
  52. def _GetFilePairs(config):
  53. """Generates the list of file pairs.
  54. Args:
  55. config: a Config object representing this target's config.
  56. Returns:
  57. A list of _FilePair objects.
  58. """
  59. ret = []
  60. has_bazel_genfiles = os.path.exists("bazel-bin")
  61. for filename in config.file_list:
  62. target = os.path.join(config.package_name, filename)
  63. generated = os.path.join(config.package_name, config.pattern % filename)
  64. if has_bazel_genfiles:
  65. generated = os.path.join("bazel-bin", generated)
  66. # Generated files should always exist. Blaze should guarantee this before
  67. # we are run.
  68. if not os.path.isfile(generated):
  69. print("Generated file '%s' does not exist." % generated)
  70. print("Please run this command to generate it:")
  71. print(" bazel build %s:%s" % (config.package_name, config.target_name))
  72. sys.exit(1)
  73. ret.append(_FilePair(target, generated))
  74. return ret
  75. def _GetMissingAndStaleFiles(file_pairs):
  76. """Generates lists of missing and stale files.
  77. Args:
  78. file_pairs: a list of _FilePair objects.
  79. Returns:
  80. missing_files: a list of _FilePair objects representing missing files.
  81. These target files do not exist at all.
  82. stale_files: a list of _FilePair objects representing stale files.
  83. These target files exist but have stale contents.
  84. """
  85. missing_files = []
  86. stale_files = []
  87. for pair in file_pairs:
  88. if not os.path.isfile(pair.target):
  89. missing_files.append(pair)
  90. continue
  91. with open(pair.generated) as g, open(pair.target) as t:
  92. if g.read() != t.read():
  93. stale_files.append(pair)
  94. return missing_files, stale_files
  95. def _CopyFiles(file_pairs):
  96. """Copies all generated files to the corresponding target file.
  97. The target files must be writable already.
  98. Args:
  99. file_pairs: a list of _FilePair objects that we want to copy.
  100. """
  101. for pair in file_pairs:
  102. target_dir = os.path.dirname(pair.target)
  103. if not os.path.isdir(target_dir):
  104. os.makedirs(target_dir)
  105. copyfile(pair.generated, pair.target)
  106. def FixFiles(config):
  107. """Implements the --fix option: overwrites missing or out-of-date files.
  108. Args:
  109. config: the Config object for this test.
  110. """
  111. file_pairs = _GetFilePairs(config)
  112. missing_files, stale_files = _GetMissingAndStaleFiles(file_pairs)
  113. _CopyFiles(stale_files + missing_files)
  114. def CheckFilesMatch(config):
  115. """Checks whether each target file matches the corresponding generated file.
  116. Args:
  117. config: the Config object for this test.
  118. Returns:
  119. None if everything matches, otherwise a string error message.
  120. """
  121. diff_errors = []
  122. file_pairs = _GetFilePairs(config)
  123. missing_files, stale_files = _GetMissingAndStaleFiles(file_pairs)
  124. for pair in missing_files:
  125. diff_errors.append("File %s does not exist" % pair.target)
  126. continue
  127. for pair in stale_files:
  128. diff_errors.append("File %s is out of date" % pair.target)
  129. if diff_errors:
  130. error_msg = "Files out of date!\n\n"
  131. error_msg += "To fix run THIS command:\n"
  132. error_msg += " bazel-bin/%s/%s --fix\n\n" % (config.package_name,
  133. config.target_name)
  134. error_msg += "Errors:\n"
  135. error_msg += " " + "\n ".join(diff_errors)
  136. return error_msg
  137. else:
  138. return None