• 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 write_json(json_file, data):
53    with open(json_file, 'w') as map_file:
54        json.dump(data, map_file, indent=4, sort_keys=True)
55
56
57def update_deps(trace_pairs):
58    # Generate substitution string
59    replacement = ""
60    for (trace, version) in trace_pairs:
61        if 'x' in version:
62            version = version.strip('x')
63            trace_prefix = EXPERIMENTAL_CIPD_PREFIX % getpass.getuser()
64        else:
65            trace_prefix = CIPD_TRACE_PREFIX
66        sub = {'trace': trace, 'version': version, 'trace_prefix': trace_prefix}
67        replacement += DEPS_TEMPLATE.format(**sub)
68
69    # Update DEPS to download CIPD dependencies
70    new_deps = ""
71    with open(DEPS_PATH) as f:
72        in_deps = False
73        for line in f:
74            if in_deps:
75                if DEPS_END in line:
76                    new_deps += replacement
77                    new_deps += line
78                    in_deps = False
79            else:
80                if DEPS_START in line:
81                    new_deps += line
82                    in_deps = True
83                else:
84                    new_deps += line
85        f.close()
86
87    with open(DEPS_PATH, 'w') as f:
88        f.write(new_deps)
89        f.close()
90
91    return True
92
93
94def main():
95    json_file = 'restricted_traces.json'
96
97    json_data = read_json(json_file)
98    if 'traces' not in json_data:
99        print('Trace data missing traces key.')
100        return 1
101    trace_pairs = [trace.split(' ') for trace in json_data['traces']]
102    traces = [trace_pair[0] for trace_pair in trace_pairs]
103
104    # auto_script parameters.
105    if len(sys.argv) > 1:
106        inputs = [json_file]
107
108        # Note: we do not include DEPS in the list of outputs to simplify the integration.
109        # Otherwise we'd continually need to regenerate on any roll. We include .gitignore
110        # in the outputs so we have a placeholder value.
111        outputs = ['.gitignore']
112
113        if sys.argv[1] == 'inputs':
114            print(','.join(inputs))
115        elif sys.argv[1] == 'outputs':
116            print(','.join(outputs))
117        else:
118            print('Invalid script parameters.')
119            return 1
120        return 0
121
122    if not update_deps(trace_pairs):
123        print('DEPS file update failed')
124        return 1
125
126    return 0
127
128
129if __name__ == '__main__':
130    sys.exit(main())
131