1#!/usr/bin/env python3 2# 3# Copyright 2019 - 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 17"""ide_common_util 18 19This module has a collection of helper functions of the ide_util module. 20""" 21 22import fnmatch 23import glob 24import logging 25import os 26import subprocess 27 28from aidegen import constant 29 30_IDEA_FOLDER = '.idea' 31_IML_EXTENSION = '.iml' 32 33 34def get_script_from_internal_path(ide_paths, ide_name): 35 """Get the studio.sh script path from internal path. 36 37 Args: 38 ide_paths: A list of IDE installed paths to be checked. 39 ide_name: The IDE name. 40 41 Returns: 42 The list of the full path of IDE or None if the IDE doesn't exist. 43 """ 44 for ide_path in ide_paths: 45 ls_output = glob.glob(ide_path, recursive=True) 46 ls_output = sorted(ls_output) 47 if ls_output: 48 logging.debug('The script%s for %s %s found.', 49 's' if len(ls_output) > 1 else '', ide_name, 50 'are' if len(ls_output) > 1 else 'is') 51 return ls_output 52 logging.error('There is not any script of %s found.', ide_name) 53 return None 54 55 56def _run_ide_sh(run_sh_cmd, project_path): 57 """Run IDE launching script with an IntelliJ project path as argument. 58 59 Args: 60 run_sh_cmd: The command to launch IDE. 61 project_path: The path of IntelliJ IDEA project content. 62 """ 63 assert run_sh_cmd, 'No suitable IDE installed.' 64 logging.debug('Run command: "%s" to launch project.', run_sh_cmd) 65 try: 66 subprocess.check_call(run_sh_cmd, shell=True) 67 except subprocess.CalledProcessError as err: 68 logging.error('Launch project path %s failed with error: %s.', 69 project_path, err) 70 71 72def _walk_tree_find_ide_exe_file(top, ide_script_name): 73 """Recursively descend the directory tree rooted at top and filter out the 74 IDE executable script we need. 75 76 Args: 77 top: the tree root to be checked. 78 ide_script_name: IDE file name such i.e. IdeIntelliJ._INTELLIJ_EXE_FILE. 79 80 Returns: 81 the IDE executable script file(s) found. 82 """ 83 logging.info('Searching IDE script %s in path: %s.', ide_script_name, top) 84 for root, _, files in os.walk(top): 85 logging.debug('Search all files under %s to get %s, %s.', top, root, 86 files) 87 for file_ in fnmatch.filter(files, ide_script_name): 88 exe_file = os.path.join(root, file_) 89 if os.access(exe_file, os.X_OK): 90 logging.debug('Use file name filter to find %s in path %s.', 91 file_, exe_file) 92 yield exe_file 93 94 95def get_run_ide_cmd(sh_path, project_file): 96 """Get the command to launch IDE. 97 98 Args: 99 sh_path: The idea.sh path where IDE is installed. 100 project_file: The path of IntelliJ IDEA project file. 101 102 Returns: 103 A string: The IDE launching command. 104 """ 105 # In command usage, the space ' ' should be '\ ' for correctness. 106 return ' '.join([ 107 constant.NOHUP, sh_path.replace(' ', r'\ '), project_file, 108 constant.IGNORE_STD_OUT_ERR_CMD 109 ]) 110 111 112def _get_scripts_from_file_path(input_path, ide_file_name): 113 """Get IDE executable script file from input file path. 114 115 Args: 116 input_path: the file path to be checked. 117 ide_file_name: the IDE executable script file name. 118 119 Returns: 120 A list of the IDE executable script path if exists otherwise None. 121 """ 122 if os.path.basename(input_path).startswith(ide_file_name): 123 files_found = glob.glob(input_path) 124 if files_found: 125 return sorted(files_found) 126 return None 127 128 129def get_scripts_from_dir_path(input_path, ide_file_name): 130 """Get an IDE executable script file from input directory path. 131 132 Args: 133 input_path: the directory to be searched. 134 ide_file_name: the IDE executable script file name. 135 136 Returns: 137 A list of an IDE executable script paths if exist otherwise None. 138 """ 139 logging.debug('Call get_scripts_from_dir_path with %s, and %s', input_path, 140 ide_file_name) 141 files_found = list(_walk_tree_find_ide_exe_file(input_path, 142 ide_file_name + '*')) 143 if files_found: 144 return sorted(files_found) 145 return None 146 147 148def launch_ide(project_path, run_ide_cmd, ide_name): 149 """Launches relative IDE by opening the passed project file. 150 151 Args: 152 project_path: The full path of the IDE project content. 153 run_ide_cmd: The command to launch IDE. 154 ide_name: the IDE name is to be launched. 155 """ 156 assert project_path, 'Empty content path is not allowed.' 157 if ide_name == constant.IDE_ECLIPSE: 158 logging.info( 159 'Launch %s with workspace: %s.', ide_name, constant.ECLIPSE_WS) 160 else: 161 logging.info('Launch %s for project content path: %s.', ide_name, 162 project_path) 163 _run_ide_sh(run_ide_cmd, project_path) 164 165 166def is_intellij_project(project_path): 167 """Checks if the path passed in is an IntelliJ project content. 168 169 Args: 170 project_path: The full path of IDEA project content, which contains 171 .idea folder and .iml file(s). 172 173 Returns: 174 True if project_path is an IntelliJ project, False otherwise. 175 """ 176 if not os.path.isfile(project_path): 177 return os.path.isdir(project_path) and os.path.isdir( 178 os.path.join(project_path, _IDEA_FOLDER)) 179 180 _, ext = os.path.splitext(os.path.basename(project_path)) 181 if ext and _IML_EXTENSION == ext.lower(): 182 path = os.path.dirname(project_path) 183 logging.debug('Extracted path is: %s.', path) 184 return os.path.isdir(os.path.join(path, _IDEA_FOLDER)) 185 return False 186 187 188def get_script_from_input_path(input_path, ide_file_name): 189 """Get correct IntelliJ executable script path from input path. 190 191 1. If input_path is a file, check if it is an IDE executable script file. 192 2. It input_path is a directory, search if it contains IDE executable script 193 file(s). 194 195 Args: 196 input_path: input path to be checked if it's an IDE executable 197 script. 198 ide_file_name: the IDE executable script file name. 199 200 Returns: 201 IDE executable file(s) if exists otherwise None. 202 """ 203 if not input_path: 204 return None 205 ide_path = [] 206 if os.path.isfile(input_path): 207 ide_path = _get_scripts_from_file_path(input_path, ide_file_name) 208 if os.path.isdir(input_path): 209 ide_path = get_scripts_from_dir_path(input_path, ide_file_name) 210 if ide_path: 211 logging.debug('IDE installed path from user input: %s.', ide_path) 212 return ide_path 213 return None 214 215 216def get_intellij_version_path(version_path): 217 """Locates the IntelliJ IDEA launch script path by version. 218 219 Args: 220 version_path: IntelliJ CE or UE version launch script path. 221 222 Returns: 223 A list of the sh full paths, or None if no such IntelliJ version is 224 installed. 225 """ 226 ls_output = glob.glob(version_path, recursive=True) 227 if not ls_output: 228 return None 229 ls_output = sorted(ls_output, reverse=True) 230 logging.debug('Result for checking IntelliJ path %s after sorting:%s.', 231 version_path, ls_output) 232 return ls_output 233 234 235def ask_preference(all_versions, ide_name): 236 """Ask users which version they prefer. 237 238 Args: 239 all_versions: A list of all CE and UE version launch script paths. 240 ide_name: The IDE name is going to be launched. 241 242 Returns: 243 An users selected version. 244 """ 245 options = [] 246 for i, sfile in enumerate(all_versions, 1): 247 options.append('\t{}. {}'.format(i, sfile)) 248 query = ('You installed {} versions of {}:\n{}\nPlease select ' 249 'one.\t').format(len(all_versions), ide_name, '\n'.join(options)) 250 return _select_intellij_version(query, all_versions) 251 252 253def _select_intellij_version(query, all_versions): 254 """Select one from different IntelliJ versions users installed. 255 256 Args: 257 query: The query message. 258 all_versions: A list of all CE and UE version launch script paths. 259 """ 260 all_numbers = [] 261 for i in range(len(all_versions)): 262 all_numbers.append(str(i + 1)) 263 input_data = input(query) 264 while input_data not in all_numbers: 265 input_data = input('Please select a number:\t') 266 return all_versions[int(input_data) - 1] 267