1#!/usr/bin/env python
2
3import json
4import os
5import pprint
6import subprocess
7import sys
8
9# =============================================================================
10# Service funcs
11# =============================================================================
12
13
14def _build_path(path, *paths):
15    return str(os.path.normpath(os.path.join(path, *paths)).replace('\\', '/'))
16
17
18def _unify_paths(path_list):
19    return [_build_path(p) for p in path_list]
20
21
22def _exclude_by_pat_list(path_list, ignore_list):
23    print('- Applying ignore list')
24    path_list_res = list(path_list)
25    for ign in ignore_list:
26        if len(ign.strip()):
27            for p in path_list:
28                if p.find(ign) != -1:
29                    try:
30                        path_list_res.remove(p)
31                    except ValueError:
32                        pass
33    return path_list_res
34
35
36def _file2linelist(path):
37    with open(path) as f:
38        lines = [line.rstrip() for line in f]
39    return [str(line) for line in lines]
40
41
42# =============================================================================
43# Test funcs
44# =============================================================================
45
46
47def get_idf_path(path, *paths):
48    IDF_PATH = os.getenv('IDF_PATH')
49    return _build_path(IDF_PATH, path, *paths)
50
51
52def _get_apps(target, build_system):
53    print('- Getting paths of apps')
54    args = [sys.executable,
55            get_idf_path('tools/find_apps.py'),
56            '-p',
57            get_idf_path('examples'),
58            '--recursive',
59            '--target', target,
60            '--build-system', build_system]
61    output = subprocess.check_output(args).decode('utf-8')
62    o_list = output.split('\n')
63    json_list = []
64    for j in o_list:
65        if j:
66            json_list.append(json.loads(j))
67    app_paths = []
68    for j in json_list:
69        app_paths.append(j['app_dir'])
70    return _unify_paths(app_paths)
71
72
73def get_apps(target, build_system, ignorelist):
74    apps = _get_apps(target, build_system)
75    if len(ignorelist):
76        return _exclude_by_pat_list(apps, ignorelist)
77    else:
78        return apps
79
80
81def get_cmake_ignore_list():
82    print('- Getting CMake ignore list')
83    return _file2linelist(
84        get_idf_path('tools', 'ci',
85                     'check_examples_cmake_make-cmake_ignore.txt'))
86
87
88def get_make_ignore_list():
89    print('- Getting Make ignore list')
90    return _file2linelist(
91        get_idf_path('tools', 'ci',
92                     'check_examples_cmake_make-make_ignore.txt'))
93
94
95def diff(first, second):
96    print('- Comparing...')
97    first = set(first)
98    second = set(second)
99    res = list(first - second) + list(second - first)
100    return res
101
102
103def main():
104    cmake_ignore = get_cmake_ignore_list()
105    make_ignore = get_make_ignore_list()
106    cmakes = get_apps('esp32', 'cmake', cmake_ignore)
107    makes = get_apps('esp32', 'make', make_ignore)
108
109    res = diff(cmakes, makes)
110
111    if len(res):
112        pp = pprint.PrettyPrinter(indent=4)
113        print(
114            '[ ERROR ] Some projects are not containing Make and Cmake project files:'
115        )
116        pp.pprint(res)
117        raise ValueError('Test is not passed')
118    else:
119        print('[ DONE ]')
120
121
122if __name__ == '__main__':
123    main()
124