def process_install(install): # Build commands from descriptor commands = [] # Dockerfile commands if "dockerfile" in install: # Remove RUN and \ at the end of lines from commands commands += [ command.replace("RUN ", "").replace(" \\\n", "\n").replace(" \\", " ") for command in install["dockerfile"] ] # Run install commands for command in commands: logging.debug("[Plugins] Install command: " + str(command)) process = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, executable=shutil.which("bash") if sys.platform == "win32" else "/bin/bash", ) return_code = process.returncode stdout = utils.decode_utf8(process.stdout) logging.debug(f"[Plugins] Result ({str(return_code)}): {stdout}") if return_code != 0: raise Exception( f"[Plugins] Error while running install command {command}:\n{stdout}" )
def run_command(command_info, log_key, mega_linter): # Run a command in Docker image root or in workspace root cwd = os.getcwd() if command_info.get("cwd", "root") == "workspace": cwd = mega_linter.workspace logging.info(f"{log_key} run: [{command_info['command']}] in cwd [{cwd}]") # Run command process = subprocess.run( command_info["command"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=os.path.realpath(cwd), executable=shutil.which("bash") if sys.platform == "win32" else "/bin/bash", ) return_code = process.returncode return_stdout = utils.decode_utf8(process.stdout) if return_code == 0: logging.info(f"{log_key} [{return_stdout}] {return_stdout}") else: logging.error(f"{log_key} [{return_stdout}] {return_stdout}") # If user defined command to fail in case of crash, stop running Mega-Linter if return_code > 0 and command_info.get("continue_if_failed", True) is False: raise Exception( f"{log_key}: User command failed, stop running Mega-Linter") return { "command_info": command_info, "status": return_code, "stdout": return_stdout, }
def get_linter_help(self): help_command = self.build_help_command() return_code = 666 output = "" command = "" for command in [help_command] + self.cli_help_extra_commands: try: if isinstance(command, str): command = command.split(" ") if sys.platform == "win32": cli_absolute = shutil.which(command[0]) if cli_absolute is not None: command[0] = cli_absolute logging.debug("Linter help command: " + str(command)) process = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) return_code = process.returncode output += utils.decode_utf8(process.stdout) logging.debug("Linter help result: " + str(return_code) + " " + output) except FileNotFoundError: logging.warning("Unable to call command [" + " ".join(command) + "]") return_code = 666 output += "ERROR" break if return_code != self.help_command_return_code or output.strip() == "": logging.warning("Unable to get help for linter [" + self.linter_name + "]") logging.warning(f"{str(command)} returned output: ({return_code}) {output}") return "ERROR" else: return output
def get_linter_version_output(self): command = self.build_version_command() if sys.platform == "win32": cli_absolute = shutil.which(command[0]) if cli_absolute is not None: command[0] = cli_absolute logging.debug("Linter version command: " + str(command)) try: process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) return_code = process.returncode output = utils.decode_utf8(process.stdout) logging.debug("Linter version result: " + str(return_code) + " " + output) except FileNotFoundError: logging.warning("Unable to call command [" + " ".join(command) + "]") return_code = 666 output = "ERROR" if return_code != self.version_command_return_code: logging.warning("Unable to get version for linter [" + self.linter_name + "]") logging.warning(" ".join(command) + f" returned output: ({str(return_code)}) " + output) return "ERROR" else: return output
def execute_lint_command(self, command): cwd = ( os.getcwd() if self.cli_lint_mode in ["file", "list_of_files"] else self.workspace ) cwd = os.path.abspath(cwd) logging.debug(f"[{self.linter_name}] CWD: {cwd}") subprocess_env = {**os.environ, "FORCE_COLOR": "0"} if type(command) == str: # Call linter with a sub-process process = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=cwd, env=subprocess_env, executable=shutil.which("bash") if sys.platform == "win32" else "/bin/bash", ) else: # Use full executable path if we are on Windows if sys.platform == "win32": cli_absolute = shutil.which(command[0]) if cli_absolute is not None: command[0] = cli_absolute else: msg = "Unable to find command: " + command[0] logging.error(msg) return errno.ESRCH, msg # Call linter with a sub-process (RECOMMENDED: with a list of strings corresponding to the command) process = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=subprocess_env, cwd=cwd, ) return_code = process.returncode return_stdout = utils.decode_utf8(process.stdout) # Return linter result return return_code, return_stdout