• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2017 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""//testing/scripts wrapper for the network traffic annotation auditor checks.
6This script is used to run traffic_annotation_auditor_tests.py on an FYI bot to
7check that traffic_annotation_auditor has the same results when heuristics that
8help it run fast and spam free on trybots are disabled."""
9
10import json
11import logging
12import os
13import re
14import sys
15import tempfile
16import traceback
17
18import common
19
20WINDOWS_SHEET_CONFIG = {
21    'spreadsheet_id': '1TmBr9jnf1-hrjntiVBzT9EtkINGrtoBYFMWad2MBeaY',
22    'annotations_sheet_name': 'Annotations',
23    'chrome_version_sheet_name': 'Chrome Version',
24    'silent_change_columns': [],
25    'last_update_column_name': 'Last Update',
26}
27
28CHROMEOS_SHEET_CONFIG = {
29    'spreadsheet_id': '1928goWKy6LVdF9Nl5nV1OD260YC10dHsdrnHEGdGsg8',
30    'annotations_sheet_name': 'Annotations',
31    'chrome_version_sheet_name': 'Chrome Version',
32    'silent_change_columns': [],
33    'last_update_column_name': 'Last Update',
34}
35
36
37def is_windows():
38  return os.name == 'nt'
39
40
41def is_chromeos(build_path):
42  current_platform = get_current_platform_from_gn_args(build_path)
43  return current_platform == 'chromeos'
44
45
46def get_sheet_config(build_path):
47  if is_windows():
48    return WINDOWS_SHEET_CONFIG
49  if is_chromeos(build_path):
50    return CHROMEOS_SHEET_CONFIG
51  return None
52
53
54def get_current_platform_from_gn_args(build_path):
55  if sys.platform.startswith('linux') and build_path is not None:
56    try:
57      with open(os.path.join(build_path, 'args.gn')) as f:
58        gn_args = f.read()
59      if not gn_args:
60        logging.info('Could not retrieve args.gn')
61
62      pattern = re.compile(r'^\s*target_os\s*=\s*"chromeos"\s*$', re.MULTILINE)
63      if pattern.search(gn_args):
64        return 'chromeos'
65
66    except (ValueError, OSError) as e:
67      logging.info(e)
68
69  return None
70
71
72def main_run(args):
73  annotations_file, annotations_filename = tempfile.mkstemp()
74  os.close(annotations_file)
75
76  errors_file, errors_filename = tempfile.mkstemp()
77  os.close(errors_file)
78
79  build_path = args.build_dir
80  command_line = [
81      sys.executable,
82      os.path.join(common.SRC_DIR, 'tools', 'traffic_annotation', 'scripts',
83                   'traffic_annotation_auditor_tests.py'),
84      '--build-path',
85      build_path,
86      '--annotations-file',
87      annotations_filename,
88      '--errors-file',
89      errors_filename,
90  ]
91  rc = common.run_command(command_line)
92
93  # Update the Google Sheets on success, but only on the Windows and ChromeOS
94  # trybot.
95  update_sheet = '--no-update-sheet' not in args.args
96  sheet_config = get_sheet_config(build_path)
97  try:
98    if rc:
99      print('Test failed without updating the annotations sheet.')
100      with open(errors_filename, encoding='utf-8') as f:
101        failures = json.load(f) or ['Please refer to stdout for errors.']
102    else:
103      print('Tests succeeded.')
104      failures = []
105
106      if update_sheet and sheet_config is not None:
107        print('Updating annotations sheet...')
108        config_file = tempfile.NamedTemporaryFile(delete=False, mode='w+')
109        json.dump(sheet_config, config_file, indent=4)
110        config_filename = config_file.name
111        config_file.close()
112        vpython_path = 'vpython3.bat' if is_windows() else 'vpython3'
113
114        command_line = [
115            vpython_path,
116            os.path.join(common.SRC_DIR, 'tools', 'traffic_annotation',
117                         'scripts', 'update_annotations_sheet.py'),
118            '--yes',
119            '--config-file',
120            config_filename,
121            '--annotations-file',
122            annotations_filename,
123        ]
124        rc = common.run_command(command_line)
125        cleanup_file(config_filename)
126
127        if rc:
128          failures = ['Please refer to stdout for errors.']
129
130    common.record_local_script_results('test_traffic_annotation_auditor',
131                                       args.output, failures, True)
132  except (ValueError, OSError) as e:
133    print('Error updating the annotations sheet', e)
134    traceback.print_exc()
135  finally:
136    cleanup_file(annotations_filename)
137
138  return rc
139
140
141def cleanup_file(filename):
142  try:
143    os.remove(filename)
144  except OSError:
145    print('Could not remove file: ', filename)
146
147
148def main_compile_targets(args):
149  json.dump(['traffic_annotation_proto'], args.output)
150
151
152if __name__ == '__main__':
153  funcs = {
154      'run': main_run,
155      'compile_targets': main_compile_targets,
156  }
157  sys.exit(common.run_script(sys.argv[1:], funcs))
158