Flutter Engine
The Flutter Engine
android_illegal_imports.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright 2013 The Flutter 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
7import argparse
8import os
9import subprocess
10import sys
11
12ANDROID_LOG_CLASS = 'android.util.Log'
13FLUTTER_LOG_CLASS = 'io.flutter.Log'
14
15ANDROIDX_TRACE_CLASS = 'androidx.tracing.Trace'
16ANDROID_TRACE_CLASS = 'android.tracing.Trace'
17FLUTTER_TRACE_CLASS = 'io.flutter.util.TraceSection'
18
19ANDROID_BUILD_VERSION_CODE_CLASS = 'VERSION_CODES'
20
21
22def CheckBadFiles(bad_files, bad_class, good_class):
23 if bad_files:
24 print('')
25 print('Illegal import %s detected in the following files:' % bad_class)
26 for bad_file in bad_files:
27 print(' - ' + bad_file)
28 print('Use %s instead.' % good_class)
29 print('')
30 return True
31
32 return False
33
34
35def main():
36 parser = argparse.ArgumentParser(
37 description='Checks Flutter Android library for forbidden imports'
38 )
39 parser.add_argument('--stamp', type=str, required=True)
40 parser.add_argument('--files', type=str, required=True, nargs='+')
41 args = parser.parse_args()
42
43 open(args.stamp, 'a').close()
44
45 bad_log_files = []
46 bad_trace_files = []
47 bad_version_codes_files = []
48
49 for file in args.files:
50 if (file.endswith(os.path.join('io', 'flutter', 'Log.java')) or
51 file.endswith(os.path.join('io', 'flutter', 'util', 'TraceSection.java')) or
52 file.endswith(os.path.join('io', 'flutter', 'Build.java'))):
53 continue
54 with open(file) as f:
55 contents = f.read()
56 if ANDROID_LOG_CLASS in contents:
57 bad_log_files.append(file)
58 if ANDROIDX_TRACE_CLASS in contents or ANDROID_TRACE_CLASS in contents:
59 bad_trace_files.append(file)
60 if ANDROID_BUILD_VERSION_CODE_CLASS in contents:
61 bad_version_codes_files.append(file)
62
63 # Flutter's Log class allows additional configuration around verbosity.
64
65 # Flutter's tracing class makes sure we do not violate string lengths that
66 # cause crashes at runtime.
67
68 # Flutter's Build.API_LEVELS class is clearer to read about which API version
69 # is used.
70 has_bad_files = CheckBadFiles(bad_log_files, ANDROID_LOG_CLASS,
71 FLUTTER_LOG_CLASS) or CheckBadFiles(
72 bad_trace_files, 'android[x].tracing.Trace', FLUTTER_TRACE_CLASS
73 ) or CheckBadFiles(
74 bad_version_codes_files, 'android.os.Build.VERSION_CODES',
75 'io.flutter.Build.API_LEVELS'
76 )
77
78 if has_bad_files:
79 return 1
80
81 return 0
82
83
84if __name__ == '__main__':
85 sys.exit(main())
def CheckBadFiles(bad_files, bad_class, good_class)
Definition: main.py:1
def print(*args, **kwargs)
Definition: run_tests.py:49