1# Copyright (C) 2011 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29"""This module is used to find files used by run-webkit-tests and
30perftestrunner. It exposes one public function - find() - which takes
31an optional list of paths, optional set of skipped directories and optional
32filter callback.
33
34If a list is passed in, the returned list of files is constrained to those
35found under the paths passed in. i.e. calling find(["LayoutTests/fast"])
36will only return files under that directory.
37
38If a set of skipped directories is passed in, the function will filter out
39the files lying in these directories i.e. find(["LayoutTests"], set(["fast"]))
40will return everything except files in fast subfolder.
41
42If a callback is passed in, it will be called for the each file and the file
43will be included into the result if the callback returns True.
44The callback has to take three arguments: filesystem, dirname and filename."""
45
46import itertools
47
48
49def find(filesystem, base_dir, paths=None, skipped_directories=None, file_filter=None, directory_sort_key=None):
50    """Finds the set of tests under a given list of sub-paths.
51
52    Args:
53      paths: a list of path expressions relative to base_dir
54          to search. Glob patterns are ok, as are path expressions with
55          forward slashes on Windows. If paths is empty, we look at
56          everything under the base_dir.
57    """
58
59    paths = paths or ['*']
60    skipped_directories = skipped_directories or set(['.svn', '_svn'])
61    return _normalized_find(filesystem, _normalize(filesystem, base_dir, paths), skipped_directories, file_filter, directory_sort_key)
62
63
64def _normalize(filesystem, base_dir, paths):
65    return [filesystem.normpath(filesystem.join(base_dir, path)) for path in paths]
66
67
68def _normalized_find(filesystem, paths, skipped_directories, file_filter, directory_sort_key):
69    """Finds the set of tests under the list of paths.
70
71    Args:
72      paths: a list of absolute path expressions to search.
73          Glob patterns are ok.
74    """
75
76    paths_to_walk = itertools.chain(*(filesystem.glob(path) for path in paths))
77
78    def sort_by_directory_key(files_list):
79        if directory_sort_key:
80            files_list.sort(key=directory_sort_key)
81        return files_list
82
83    all_files = itertools.chain(*(sort_by_directory_key(filesystem.files_under(path, skipped_directories, file_filter)) for path in paths_to_walk))
84    return all_files
85