1#!/usr/bin/env python
2#
3# Copyright (C) 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from typing import List
18from glob import glob
19from pathlib import Path
20from os.path import join, relpath
21from itertools import chain
22import argparse
23
24class FileLister:
25    def __init__(self, args) -> None:
26        self.out_file = args.out_file
27
28        self.folder_dir = args.dir
29        self.extensions = [e if e.startswith(".") else "." + e for e in args.extensions]
30        self.root = args.root
31        self.files_list : List[str] = list()
32        self.classes = args.classes
33
34    def get_files(self) -> None:
35        """Get all files directory in the input directory including the files in the subdirectories
36
37        Recursively finds all files in the input directory.
38        Set file_list as a list of file directory strings,
39        which do not include directories but only files.
40        List is sorted in alphabetical order of the file directories.
41
42        Args:
43            dir: Directory to get the files. String.
44
45        Raises:
46            FileNotFoundError: An error occurred accessing the non-existing directory
47        """
48
49        if not dir_exists(self.folder_dir):
50            raise FileNotFoundError(f"Directory {self.folder_dir} does not exist")
51
52        if self.folder_dir[:-2] != "**":
53            self.folder_dir = join(self.folder_dir, "**")
54
55        self.files_list = list()
56        for file in sorted(glob(self.folder_dir, recursive=True)):
57            if Path(file).is_file():
58                if self.root:
59                    file = join(self.root, relpath(file, self.folder_dir[:-2]))
60                self.files_list.append(file)
61
62
63    def list(self) -> None:
64        self.get_files()
65        self.files_list = [f for f in self.files_list if not self.extensions or Path(f).suffix in self.extensions]
66
67        # If files_list is as below:
68        # A/B/C.java
69        # A/B/D.java
70        # A/B/E.txt
71        # --classes flag converts files_list in the following format:
72        # A/B/C.class
73        # A/B/C$*.class
74        # A/B/D.class
75        # A/B/D$*.class
76        # Additional `$*`-suffixed line is appended after each line
77        # to take multiple top level classes in a single java file into account.
78        # Note that non-java files in files_list are filtered out.
79        if self.classes:
80            self.files_list = list(chain.from_iterable([
81                (class_files := str(Path(ff).with_suffix(".class")),
82                 class_files.replace(".class", "$*.class"))
83                 for ff in self.files_list if ff.endswith(".java")
84            ]))
85
86        self.write()
87
88    def write(self) -> None:
89        if self.out_file == "":
90            pprint(self.files_list)
91        else:
92            write_lines(self.out_file, self.files_list)
93
94###
95# Helper functions
96###
97def pprint(l: List[str]) -> None:
98    for line in l:
99        print(line)
100
101def dir_exists(dir: str) -> bool:
102    return Path(dir).exists()
103
104def write_lines(out_file: str, lines: List[str]) -> None:
105    with open(out_file, "w+") as f:
106        f.writelines(line + '\n' for line in lines)
107
108if __name__ == '__main__':
109    parser = argparse.ArgumentParser()
110    parser.add_argument('dir', action='store', type=str,
111                        help="directory to list all subdirectory files")
112    parser.add_argument('--out', dest='out_file',
113                        action='store', default="", type=str,
114                        help="optional directory to write subdirectory files. If not set, will print to console")
115    parser.add_argument('--root', dest='root',
116                        action='store', default="", type=str,
117                        help="optional directory to replace the root directories of output.")
118    parser.add_argument('--extensions', nargs='*', default=list(), dest='extensions',
119                        help="Extensions to include in the output. If not set, all files are included")
120    parser.add_argument('--classes', dest='classes', action=argparse.BooleanOptionalAction,
121                        help="Optional flag. If passed, outputs a list of pattern of class files \
122                                that will be produced by compiling java files in the input dir. \
123                                Non-java files in the input directory will be ignored.")
124
125    args = parser.parse_args()
126
127    file_lister = FileLister(args)
128    file_lister.list()
129