xref: /aosp_15_r20/external/autotest/utils/Python2MigrationFinder.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
2
3import os
4
5
6def has_match(line):
7    """check if file current line matches py3_strs.
8
9    Args:
10            line: Current line to check.
11
12    return:
13            Boolean True or False.
14    """
15    py3_strs = [
16            "#!/usr/bin/python3", "#!/usr/bin/env python3",
17            "# lint as: python2, python3", "# lint as: python3"
18    ]
19    for match in py3_strs:
20        if match in line:
21            return True
22    return False
23
24
25def need_to_skip(fullname):
26    """check if this file or folder that needs to be skipped from skip_strs.
27
28    Args:
29            fullname: Current file or folder name.
30
31    return:
32            Boolean True or False.
33    """
34    skip_strs = ["__init__.py", "autotest_lib", "common.py", "site_tests"]
35    for match in skip_strs:
36        if match in fullname:
37            return True
38    return False
39
40
41def list_files_to_txt(upper_dir, file, suffix, nums_line_to_check):
42    """List results to .txt file by check all target files.
43    under the folder and subfolder.
44
45    Args:
46            upper_dir: The folder path need to check. The default.
47                    is the ipper path of this script.
48            file: output .txt file. The default is Python2MigrationTarget.txt.
49            suffix: File extensions that need to be checked.
50            nums_line_to_check: The number of rows to check.
51
52    return:
53            All file names and paths that meet the standard.
54    """
55    exts = suffix.split(" ")
56    files = os.listdir(upper_dir)
57    for filename in files:
58        fullname = os.path.join(upper_dir, filename)
59        if need_to_skip(fullname):
60            continue
61        if os.path.isdir(fullname):
62            list_files_to_txt(fullname, file, suffix, nums_line_to_check)
63        else:
64            for ext in exts:
65                if filename.endswith(ext):
66                    filename = fullname
67                    with open(filename, "r") as f:
68                        for i in range(nums_line_to_check):
69                            line = str(f.readline().strip()).lower()
70                            if has_match(line):
71                                tail = filename.split("third_party")[-1]
72                                file.write("%s, 3\n" % tail)
73                            else:
74                                tail = filename.split("third_party")[-1]
75                                file.write("%s, 2\n" % tail)
76                            break
77
78
79def main():
80    """This is main function"""
81    upper_dir = os.path.abspath(
82            os.path.join(os.path.dirname("__file__"), os.path.pardir))
83    outfile = "Python2MigrationTarget.txt"
84    suffix = ".py"
85    nums_line_to_check = 20
86    file = open(outfile, "w")
87    if not file:
88        print("cannot open the file %s " % outfile)
89    list_files_to_txt(upper_dir, file, suffix, nums_line_to_check)
90    file.close()
91
92
93if __name__ == "__main__":
94
95    main()
96