# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import subprocess


def CheckChange(input, output):
    # There apparently is no way to wrap strings in blueprints, so ignore long
    # lines in them.
    long_line_sources = lambda x: input.FilterSourceFile(
            x, white_list=".*", black_list=['Android[.]bp'])
    results = []
    results += input.canned_checks.CheckDoNotSubmit(input, output)
    results += input.canned_checks.CheckChangeHasNoTabs(input, output)
    results += input.canned_checks.CheckLongLines(
            input, output, 80, source_file_filter=long_line_sources)
    results += input.canned_checks.CheckPatchFormatted(input, output)
    results += input.canned_checks.CheckGNFormatted(input, output)
    results += CheckIncludeGuards(input, output)
    results += CheckAndroidBlueprint(input, output)
    results += CheckMergedTraceConfigProto(input, output)
    return results


def CheckChangeOnUpload(input_api, output_api):
    return CheckChange(input_api, output_api)


def CheckChangeOnCommit(input_api, output_api):
    return CheckChange(input_api, output_api)


def CheckAndroidBlueprint(input_api, output_api):
    # If no GN files were modified, bail out.
    build_file_filter = lambda x: input_api.FilterSourceFile(
          x,
          white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
    if not input_api.AffectedSourceFiles(build_file_filter):
        return []

    with open('Android.bp') as f:
        current_blueprint = f.read()

    new_blueprint = subprocess.check_output(
        ['tools/gen_android_bp', '--output', '/dev/stdout'])

    if current_blueprint != new_blueprint:
        return [
            output_api.PresubmitError(
                'Android.bp is out of date. Please run tools/gen_android_bp '
                'to update it.')
        ]
    return []


def CheckIncludeGuards(input_api, output_api):
    tool = 'tools/fix_include_guards'
    file_filter = lambda x: input_api.FilterSourceFile(
          x,
          white_list=('.*[.]cc$', '.*[.]h$', tool))
    if not input_api.AffectedSourceFiles(file_filter):
        return []
    if subprocess.call([tool, '--check-only']):
        return [
            output_api.PresubmitError(
                'Please run ' + tool + ' to fix include guards.')
        ]
    return []


def CheckMergedTraceConfigProto(input_api, output_api):
    tool = 'tools/gen_merged_protos'
    build_file_filter = lambda x: input_api.FilterSourceFile(
          x,
          white_list=('protos/perfetto/.*[.]proto$', tool))
    if not input_api.AffectedSourceFiles(build_file_filter):
        return []
    if subprocess.call([tool, '--check-only']):
        return [
            output_api.PresubmitError(
                'perfetto_config.proto or perfetto_trace.proto is out of ' +
                'date. Please run ' + tool + ' to update it.')
        ]
    return []

@@ -594,16 +594,33 @@
 cense.%0A%0A
+import itertools%0A
 import s
@@ -630,17 +630,16 @@
 rocess%0A%0A
-%0A
 def Chec
@@ -1448,24 +1448,69 @@
 ut, output)%0A
+    results += CheckWhitelist(input, output)%0A
     return r
@@ -3483,28 +3483,609 @@
 .')%0A        %5D%0A    return %5B%5D%0A
+%0A%0A# Prevent removing or changing lines in event_whitelist.%0Adef CheckWhitelist(input_api, output_api):%0A  for f in input_api.AffectedFiles():%0A    if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist':%0A      continue%0A    if any(new_line != 'removed' and new_line != old_line for old_line, new_line%0A           in itertools.izip(f.OldContents(), f.NewContents())):%0A      return %5B%0A        output_api.PresubmitError(%0A            'event_whitelist only has two supported changes: '%0A            'appending a new line, and replacing a line with removed.'%0A        )%0A      %5D%0A  return %5B%5D%0A

Add PRESUBMIT to protect whitelist. am: 640b7aefc4 am: dd843304ef