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 glob 6import os 7 8from telemetry.core import util 9import py_utils as catapult_util 10 11# TODO(aiolos): Move these functions to catapult_util or here. 12GetBaseDir = util.GetBaseDir 13GetTelemetryDir = util.GetTelemetryDir 14GetUnittestDataDir = util.GetUnittestDataDir 15GetChromiumSrcDir = util.GetChromiumSrcDir 16GetBuildDirectories = util.GetBuildDirectories 17 18IsExecutable = catapult_util.IsExecutable 19 20 21def _HasWildcardCharacters(input_string): 22 # Could make this more precise. 23 return '*' in input_string or '+' in input_string 24 25 26def FindInstalledWindowsApplication(application_path): 27 """Search common Windows installation directories for an application. 28 29 Args: 30 application_path: Path to application relative from installation location. 31 Returns: 32 A string representing the full path, or None if not found. 33 """ 34 search_paths = [os.getenv('PROGRAMFILES(X86)'), 35 os.getenv('PROGRAMFILES'), 36 os.getenv('LOCALAPPDATA')] 37 search_paths += os.getenv('PATH', '').split(os.pathsep) 38 for search_path in search_paths: 39 if not search_path: 40 continue 41 path = os.path.join(search_path, application_path) 42 if _HasWildcardCharacters(path): 43 paths = glob.glob(path) 44 else: 45 paths = [path] 46 for p in paths: 47 if IsExecutable(p): 48 return p 49 return None 50 51 52def IsSubpath(subpath, superpath): 53 """Returns True iff subpath is or is in superpath.""" 54 subpath = os.path.realpath(subpath) 55 superpath = os.path.realpath(superpath) 56 57 while len(subpath) >= len(superpath): 58 if subpath == superpath: 59 return True 60 subpath = os.path.split(subpath)[0] 61 return False 62 63 64def ListFiles(base_directory, should_include_dir=lambda _: True, 65 should_include_file=lambda _: True): 66 matching_files = [] 67 for root, dirs, files in os.walk(base_directory): 68 dirs[:] = [dir_name for dir_name in dirs if should_include_dir(dir_name)] 69 matching_files += [os.path.join(root, file_name) 70 for file_name in files if should_include_file(file_name)] 71 return sorted(matching_files) 72