|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0(the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http: // www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""Check no absl reference are added to Firestore/core/src/api header files. |
| 17 | +
|
| 18 | +Absl references in core/src/api public interface will cause link error for |
| 19 | +the Unity SDK, when it is built from google3. |
| 20 | +""" |
| 21 | + |
| 22 | +# TODO(b/192129206) : Remove this check once Unity SDK is built from Github. |
| 23 | +import argparse |
| 24 | +import logging |
| 25 | +import six |
| 26 | +import subprocess |
| 27 | + |
| 28 | +from lib import command_trace |
| 29 | + |
| 30 | +_logger = logging.getLogger('absl_check') |
| 31 | + |
| 32 | + |
| 33 | +def diff_with_absl(revision, patterns): |
| 34 | + """Finds diffs containing 'absl' since a revision from specified path |
| 35 | + pattern. |
| 36 | + """ |
| 37 | + # git command to print all diffs that has 'absl' in it. |
| 38 | + command = ['git', 'diff', '-G', 'absl', revision, '--'] |
| 39 | + command.extend(patterns) |
| 40 | + _logger.debug(command) |
| 41 | + return six.ensure_text(subprocess.check_output(command)) |
| 42 | + |
| 43 | + |
| 44 | +def main(): |
| 45 | + patterns = ['Firestore/core/src/api/*.h'] |
| 46 | + parser = argparse.ArgumentParser( |
| 47 | + description='Check Absl usage in %s' % patterns) |
| 48 | + parser.add_argument( |
| 49 | + '--dry-run', |
| 50 | + '-n', |
| 51 | + action='store_true', |
| 52 | + help='Show what the linter would do without doing it') |
| 53 | + parser.add_argument( |
| 54 | + 'rev', |
| 55 | + nargs='?', |
| 56 | + help='A single revision that specifies a point in time ' |
| 57 | + 'from which to look for changes. Defaults to ' |
| 58 | + 'origin/master.') |
| 59 | + args = command_trace.parse_args(parser) |
| 60 | + |
| 61 | + dry_run = False |
| 62 | + if args.dry_run: |
| 63 | + dry_run = True |
| 64 | + _logger.setLevel(logging.DEBUG) |
| 65 | + |
| 66 | + revision = 'origin/master' if not args.rev else args.rev |
| 67 | + _logger.debug('Checking %s absl usage against %s' % |
| 68 | + (patterns, revision)) |
| 69 | + diff = diff_with_absl(revision, patterns) |
| 70 | + |
| 71 | + # Check for changes adding new absl references only. |
| 72 | + lines = [line for line in diff.splitlines() |
| 73 | + if line.startswith('+') and 'absl::' in line] |
| 74 | + if lines: |
| 75 | + _logger.error( |
| 76 | + 'Found a change introducing reference to absl under %s' |
| 77 | + % patterns) |
| 78 | + for line in lines: |
| 79 | + _logger.error(' %s' % line) |
| 80 | + if not dry_run: |
| 81 | + exit(-1) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + main() |
0 commit comments