check_firestore_core_api_absl.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #! /usr/bin/env python
  2. # Copyright 2021 Google LLC
  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. """Check no absl reference are added to Firestore/core/src/api header files.
  16. Absl references in core/src/api public interface will cause link error for
  17. the Unity SDK, when it is built from google3.
  18. """
  19. # TODO(b/192129206) : Remove this check once Unity SDK is built from Github.
  20. import argparse
  21. import logging
  22. import six
  23. import subprocess
  24. from lib import command_trace
  25. _logger = logging.getLogger('absl_check')
  26. def diff_with_absl(revision, patterns):
  27. """Finds diffs containing 'absl' since a revision from specified path
  28. pattern.
  29. """
  30. # git command to print all diffs that has 'absl' in it.
  31. command = ['git', 'diff', '-G', 'absl', revision, '--']
  32. command.extend(patterns)
  33. _logger.debug(command)
  34. return six.ensure_text(subprocess.check_output(command))
  35. def main():
  36. patterns = ['Firestore/core/src/api/*.h']
  37. parser = argparse.ArgumentParser(
  38. description='Check Absl usage in %s' % patterns)
  39. parser.add_argument(
  40. '--dry-run',
  41. '-n',
  42. action='store_true',
  43. help='Show what the linter would do without doing it')
  44. parser.add_argument(
  45. 'rev',
  46. nargs='?',
  47. help='A single revision that specifies a point in time '
  48. 'from which to look for changes. Defaults to '
  49. 'origin/master.')
  50. args = command_trace.parse_args(parser)
  51. dry_run = False
  52. if args.dry_run:
  53. dry_run = True
  54. _logger.setLevel(logging.DEBUG)
  55. revision = 'origin/master' if not args.rev else args.rev
  56. _logger.debug('Checking %s absl usage against %s' %
  57. (patterns, revision))
  58. diff = diff_with_absl(revision, patterns)
  59. # Check for changes adding new absl references only.
  60. lines = [line for line in diff.splitlines()
  61. if line.startswith('+') and 'absl::' in line]
  62. if lines:
  63. _logger.error(
  64. 'Found a change introducing reference to absl under %s'
  65. % patterns)
  66. for line in lines:
  67. _logger.error(' %s' % line)
  68. if not dry_run:
  69. exit(-1)
  70. if __name__ == '__main__':
  71. main()