1#!/usr/bin/env python3
2
3# This tool generates `eligible-list-${ver}.csv` and
4# `eligible-list-${ver}-properties.csv`.
5
6import argparse
7import os
8import subprocess
9import sys
10import tempfile
11
12UPDATE_DATASET = os.path.abspath(os.path.join(
13    __file__, '..', 'update_dataset.py'))
14
15LIST_VNDK_MODULE = os.path.abspath(os.path.join(
16    __file__, '..', '..', '..', 'sourcedr', 'blueprint', 'list_vndk_module.py'))
17
18
19def update_eligible_list(path, make_vars, module_info):
20    dirname, basename = os.path.split(path)
21    tmp_fd, tmp_path = tempfile.mkstemp(prefix=basename + '-', dir=dirname)
22    os.close(tmp_fd)
23
24    cmd = [sys.executable, UPDATE_DATASET]
25    cmd.extend(['--make-vars', make_vars])
26    cmd.extend(['--module-info', module_info])
27    cmd.extend(['-o', tmp_path])
28    cmd.append(path)
29
30    print('command:', ' '.join(cmd))
31
32    subprocess.check_call(cmd)
33    os.rename(tmp_path, path)
34
35
36def update_eligible_list_properties(path, build_top):
37    dirname, basename = os.path.split(path)
38    tmp_fd, tmp_path = tempfile.mkstemp(prefix=basename + '-', dir=dirname)
39    os.close(tmp_fd)
40
41    cmd = [sys.executable, LIST_VNDK_MODULE]
42    cmd.extend(['--exclude', '(?:device/)|(?:vendor/)'])
43    cmd.extend(['--namespace', 'hardware/google/av'])
44    cmd.extend(['--namespace', 'hardware/google/interfaces'])
45    cmd.extend(['-o', tmp_path])
46    cmd.append(os.path.join(build_top, 'Android.bp'))
47
48    print('command:', ' '.join(cmd))
49
50    subprocess.check_call(cmd)
51    os.rename(tmp_path, path)
52
53
54def _get_eligible_list_properties_path(eligible_list_path):
55    root, ext = os.path.splitext(eligible_list_path)
56    return root + '-properties' + ext
57
58
59def _parse_args():
60    parser = argparse.ArgumentParser()
61    parser.add_argument('eligible_list',
62                        help='Path to eligible list to be updated')
63    return parser.parse_args()
64
65
66def main():
67    args = _parse_args()
68
69    # Read Android product environment variables.
70    try:
71        build_top = os.environ['ANDROID_BUILD_TOP']
72        product = os.environ['TARGET_PRODUCT']
73        product_out = os.environ['ANDROID_PRODUCT_OUT']
74    except KeyError as e:
75        print('error: Failed to read the environment variable', e.args[0],
76              file=sys.stderr)
77        print('error: Did you lunch an Android target?', file=sys.stderr)
78        sys.exit(1)
79
80    print('----------------------------------------')
81    print('build_top:', build_top)
82    print('product:', product)
83    print('product_out:', product_out)
84
85    out_dir = os.path.normpath(os.path.join(product_out, '..', '..', '..'))
86    make_vars = os.path.join(out_dir, 'soong', 'make_vars-' + product + '.mk')
87    module_info = os.path.join(product_out, 'module-info.json')
88
89    print('----------------------------------------')
90    print('make_vars:', make_vars)
91    print('module_info:', module_info)
92    print('----------------------------------------')
93
94    # Run the commands to update the files.
95    update_eligible_list(args.eligible_list, make_vars, module_info)
96    update_eligible_list_properties(
97        _get_eligible_list_properties_path(args.eligible_list), build_top)
98
99
100if __name__ == '__main__':
101    sys.exit(main())
102