1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import os 6 7from telemetry.core import util 8from catapult_base import util as catapult_util # pylint: disable=import-error 9 10# TODO(aiolos): Move these functions to catapult_base or here. 11GetBaseDir = util.GetBaseDir 12GetTelemetryDir = util.GetTelemetryDir 13GetUnittestDataDir = util.GetUnittestDataDir 14GetChromiumSrcDir = util.GetChromiumSrcDir 15GetBuildDirectories = util.GetBuildDirectories 16 17IsExecutable = catapult_util.IsExecutable 18 19 20def FindInstalledWindowsApplication(application_path): 21 """Search common Windows installation directories for an application. 22 23 Args: 24 application_path: Path to application relative from installation location. 25 Returns: 26 A string representing the full path, or None if not found. 27 """ 28 search_paths = [os.getenv('PROGRAMFILES(X86)'), 29 os.getenv('PROGRAMFILES'), 30 os.getenv('LOCALAPPDATA')] 31 search_paths += os.getenv('PATH', '').split(os.pathsep) 32 33 for search_path in search_paths: 34 if not search_path: 35 continue 36 path = os.path.join(search_path, application_path) 37 if IsExecutable(path): 38 return path 39 40 return None 41 42 43def IsSubpath(subpath, superpath): 44 """Returns True iff subpath is or is in superpath.""" 45 subpath = os.path.realpath(subpath) 46 superpath = os.path.realpath(superpath) 47 48 while len(subpath) >= len(superpath): 49 if subpath == superpath: 50 return True 51 subpath = os.path.split(subpath)[0] 52 return False 53 54 55def ListFiles(base_directory, should_include_dir=lambda _: True, 56 should_include_file=lambda _: True): 57 matching_files = [] 58 for root, dirs, files in os.walk(base_directory): 59 dirs[:] = [dir_name for dir_name in dirs if should_include_dir(dir_name)] 60 matching_files += [os.path.join(root, file_name) 61 for file_name in files if should_include_file(file_name)] 62 return sorted(matching_files) 63