#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import os
import sys


import common


def main_run(args):
  with common.temporary_file() as tempfile_path:
    rc = common.run_command([
        os.path.join(common.SRC_DIR, 'tools', 'checklicenses',
                     'checklicenses.py'),
        '--json', tempfile_path
    ])

    with open(tempfile_path) as f:
      checklicenses_results = json.load(f)

  result_set = set()
  for result in checklicenses_results:
    result_set.add((result['filename'], result['license']))

  json.dump({
      'valid': True,
      'failures': ['%s: %s' % (r[0], r[1]) for r in result_set],
  }, args.output)

  return rc


def main_compile_targets(args):
  json.dump([], args.output)


if __name__ == '__main__':
  funcs = {
    'run': main_run,
    'compile_targets': main_compile_targets,
  }
  sys.exit(common.run_script(sys.argv[1:], funcs))

[((29, 2, 32, 17), 'json.dump', 'json.dump', ({(29, 12, 32, 3): "{'valid': True, 'failures': [('%s: %s' % (r[0], r[1])) for r in result_set]}", (32, 5, 32, 16): 'args.output'}, {}), "({'valid': True, 'failures': [('%s: %s' % (r[0], r[1])) for r in\n    result_set]}, args.output)", False, 'import json\n'), ((38, 2, 38, 28), 'json.dump', 'json.dump', ({(38, 12, 38, 14): '[]', (38, 16, 38, 27): 'args.output'}, {}), '([], args.output)', False, 'import json\n'), ((15, 7, 15, 30), 'common.temporary_file', 'common.temporary_file', ({}, {}), '()', False, 'import common\n'), ((46, 11, 46, 49), 'common.run_script', 'common.run_script', ({(46, 29, 46, 41): 'sys.argv[1:]', (46, 43, 46, 48): 'funcs'}, {}), '(sys.argv[1:], funcs)', False, 'import common\n'), ((23, 30, 23, 42), 'json.load', 'json.load', ({(23, 40, 23, 41): 'f'}, {}), '(f)', False, 'import json\n'), ((17, 8, 18, 40), 'os.path.join', 'os.path.join', ({(17, 21, 17, 35): 'common.SRC_DIR', (17, 37, 17, 44): '"""tools"""', (17, 46, 17, 61): '"""checklicenses"""', (18, 21, 18, 39): '"""checklicenses.py"""'}, {}), "(common.SRC_DIR, 'tools', 'checklicenses', 'checklicenses.py')", False, 'import os\n')]