• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2#
3# Copyright 2020 The ANGLE Project Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# gen_restricted_traces.py:
8#   Generates integration code for the restricted trace tests.
9
10import getpass
11import glob
12import fnmatch
13import re
14import json
15import os
16import sys
17
18CIPD_TRACE_PREFIX = 'angle/traces'
19EXPERIMENTAL_CIPD_PREFIX = 'experimental/google.com/%s/angle/traces'
20DEPS_PATH = '../../../DEPS'
21DEPS_START = '# === ANGLE Restricted Trace Generated Code Start ==='
22DEPS_END = '# === ANGLE Restricted Trace Generated Code End ==='
23DEPS_TEMPLATE = """\
24  'src/tests/restricted_traces/{trace}': {{
25      'packages': [
26        {{
27            'package': '{trace_prefix}/{trace}',
28            'version': 'version:{version}',
29        }},
30      ],
31      'dep_type': 'cipd',
32      'condition': 'checkout_angle_restricted_traces',
33  }},
34"""
35
36
37def reject_duplicate_keys(pairs):
38    found_keys = {}
39    for key, value in pairs:
40        if key in found_keys:
41            raise ValueError("duplicate key: %r" % (key,))
42        else:
43            found_keys[key] = value
44    return found_keys
45
46
47def read_json(json_file):
48    with open(json_file) as map_file:
49        return json.loads(map_file.read(), object_pairs_hook=reject_duplicate_keys)
50
51
52def update_deps(trace_pairs):
53    # Generate substitution string
54    replacement = ""
55    for (trace, version) in trace_pairs:
56        if 'x' in version:
57            version = version.strip('x')
58            trace_prefix = EXPERIMENTAL_CIPD_PREFIX % getpass.getuser()
59        else:
60            trace_prefix = CIPD_TRACE_PREFIX
61        sub = {'trace': trace, 'version': version, 'trace_prefix': trace_prefix}
62        replacement += DEPS_TEMPLATE.format(**sub)
63
64    # Update DEPS to download CIPD dependencies
65    new_deps = ""
66    with open(DEPS_PATH) as f:
67        in_deps = False
68        for line in f:
69            if in_deps:
70                if DEPS_END in line:
71                    new_deps += replacement
72                    new_deps += line
73                    in_deps = False
74            else:
75                if DEPS_START in line:
76                    new_deps += line
77                    in_deps = True
78                else:
79                    new_deps += line
80        f.close()
81
82    with open(DEPS_PATH, 'w') as f:
83        f.write(new_deps)
84        f.close()
85
86    return True
87
88
89def main():
90    json_file = 'restricted_traces.json'
91
92    json_data = read_json(json_file)
93    if 'traces' not in json_data:
94        print('Trace data missing traces key.')
95        return 1
96    trace_pairs = [trace.split(' ') for trace in json_data['traces']]
97    traces = [trace_pair[0] for trace_pair in trace_pairs]
98
99    # auto_script parameters.
100    if len(sys.argv) > 1:
101        inputs = [json_file]
102
103        # Note: we do not include DEPS in the list of outputs to simplify the integration.
104        # Otherwise we'd continually need to regenerate on any roll. We include .gitignore
105        # in the outputs so we have a placeholder value.
106        outputs = ['.gitignore']
107
108        if sys.argv[1] == 'inputs':
109            print(','.join(inputs))
110        elif sys.argv[1] == 'outputs':
111            print(','.join(outputs))
112        else:
113            print('Invalid script parameters.')
114            return 1
115        return 0
116
117    if not update_deps(trace_pairs):
118        print('DEPS file update failed')
119        return 1
120
121    return 0
122
123
124if __name__ == '__main__':
125    sys.exit(main())
126