1*800a58d9SAndroid Build Coastguard Worker#!/usr/bin/env python 2*800a58d9SAndroid Build Coastguard Worker# 3*800a58d9SAndroid Build Coastguard Worker# Copyright 2018 - The Android Open Source Project 4*800a58d9SAndroid Build Coastguard Worker# 5*800a58d9SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*800a58d9SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*800a58d9SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*800a58d9SAndroid Build Coastguard Worker# 9*800a58d9SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*800a58d9SAndroid Build Coastguard Worker# 11*800a58d9SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*800a58d9SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*800a58d9SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*800a58d9SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*800a58d9SAndroid Build Coastguard Worker# limitations under the License. 16*800a58d9SAndroid Build Coastguard Worker"""Base setup subtask runner. 17*800a58d9SAndroid Build Coastguard Worker 18*800a58d9SAndroid Build Coastguard WorkerBaseTaskRunner defines basic methods which will be called in the setup process. 19*800a58d9SAndroid Build Coastguard Worker 20*800a58d9SAndroid Build Coastguard Workerthe flow in each child task runner will be in below manner: 21*800a58d9SAndroid Build Coastguard WorkerCheck ShouldRun() -> PrintWelcomMessage() -> _Run() 22*800a58d9SAndroid Build Coastguard Worker""" 23*800a58d9SAndroid Build Coastguard Worker 24*800a58d9SAndroid Build Coastguard Workerfrom __future__ import print_function 25*800a58d9SAndroid Build Coastguard Workerimport logging 26*800a58d9SAndroid Build Coastguard Workerimport os 27*800a58d9SAndroid Build Coastguard Workerimport textwrap 28*800a58d9SAndroid Build Coastguard Worker 29*800a58d9SAndroid Build Coastguard Worker 30*800a58d9SAndroid Build Coastguard Workerlogger = logging.getLogger(__name__) 31*800a58d9SAndroid Build Coastguard Worker_PARAGRAPH_BREAK = "=" 32*800a58d9SAndroid Build Coastguard Worker 33*800a58d9SAndroid Build Coastguard Worker 34*800a58d9SAndroid Build Coastguard Workerclass BaseTaskRunner(): 35*800a58d9SAndroid Build Coastguard Worker """A basic task runner class for setup cmd.""" 36*800a58d9SAndroid Build Coastguard Worker 37*800a58d9SAndroid Build Coastguard Worker # WELCOME_MESSAGE and WELCOME_MESSAGE_TITLE should both be defined as 38*800a58d9SAndroid Build Coastguard Worker # strings. 39*800a58d9SAndroid Build Coastguard Worker WELCOME_MESSAGE = None 40*800a58d9SAndroid Build Coastguard Worker WELCOME_MESSAGE_TITLE = None 41*800a58d9SAndroid Build Coastguard Worker 42*800a58d9SAndroid Build Coastguard Worker def PrintWelcomeMessage(self): 43*800a58d9SAndroid Build Coastguard Worker """Print out the welcome message in a fancy format. 44*800a58d9SAndroid Build Coastguard Worker 45*800a58d9SAndroid Build Coastguard Worker This method will print out the welcome message in the following manner 46*800a58d9SAndroid Build Coastguard Worker given the following example: 47*800a58d9SAndroid Build Coastguard Worker e.g. 48*800a58d9SAndroid Build Coastguard Worker WELCOME_MESSAGE_TITLE = "title" 49*800a58d9SAndroid Build Coastguard Worker WELCOME_MESSAGE = ( 50*800a58d9SAndroid Build Coastguard Worker "this is one long str " 51*800a58d9SAndroid Build Coastguard Worker "broken into multiple lines " 52*800a58d9SAndroid Build Coastguard Worker "based on the screen width" 53*800a58d9SAndroid Build Coastguard Worker ) 54*800a58d9SAndroid Build Coastguard Worker 55*800a58d9SAndroid Build Coastguard Worker actual output will be: 56*800a58d9SAndroid Build Coastguard Worker =========================== 57*800a58d9SAndroid Build Coastguard Worker [title] 58*800a58d9SAndroid Build Coastguard Worker this is one long str 59*800a58d9SAndroid Build Coastguard Worker broken into multiple lines 60*800a58d9SAndroid Build Coastguard Worker based on the screen width 61*800a58d9SAndroid Build Coastguard Worker =========================== 62*800a58d9SAndroid Build Coastguard Worker """ 63*800a58d9SAndroid Build Coastguard Worker if not self.WELCOME_MESSAGE and not self.WELCOME_MESSAGE_TITLE: 64*800a58d9SAndroid Build Coastguard Worker logger.debug("No welcome message for %s", self.__class__.__name__) 65*800a58d9SAndroid Build Coastguard Worker return 66*800a58d9SAndroid Build Coastguard Worker 67*800a58d9SAndroid Build Coastguard Worker # define the layout of message. 68*800a58d9SAndroid Build Coastguard Worker console_width = int(os.popen('stty size', 'r').read().split()[1]) 69*800a58d9SAndroid Build Coastguard Worker break_width = int(console_width / 2) 70*800a58d9SAndroid Build Coastguard Worker 71*800a58d9SAndroid Build Coastguard Worker # start to print welcome message. 72*800a58d9SAndroid Build Coastguard Worker print("\n" +_PARAGRAPH_BREAK * break_width) 73*800a58d9SAndroid Build Coastguard Worker print(" [%s] " % self.WELCOME_MESSAGE_TITLE) 74*800a58d9SAndroid Build Coastguard Worker print(textwrap.fill( 75*800a58d9SAndroid Build Coastguard Worker self.WELCOME_MESSAGE, 76*800a58d9SAndroid Build Coastguard Worker break_width - 2, 77*800a58d9SAndroid Build Coastguard Worker initial_indent=" ", 78*800a58d9SAndroid Build Coastguard Worker subsequent_indent=" ")) 79*800a58d9SAndroid Build Coastguard Worker print(_PARAGRAPH_BREAK * break_width + "\n") 80*800a58d9SAndroid Build Coastguard Worker 81*800a58d9SAndroid Build Coastguard Worker # pylint: disable=no-self-use 82*800a58d9SAndroid Build Coastguard Worker def ShouldRun(self): 83*800a58d9SAndroid Build Coastguard Worker """Check if setup should run. 84*800a58d9SAndroid Build Coastguard Worker 85*800a58d9SAndroid Build Coastguard Worker Returns: 86*800a58d9SAndroid Build Coastguard Worker Boolean, True if setup should run False otherwise. 87*800a58d9SAndroid Build Coastguard Worker """ 88*800a58d9SAndroid Build Coastguard Worker return True 89*800a58d9SAndroid Build Coastguard Worker 90*800a58d9SAndroid Build Coastguard Worker def Run(self, force_setup=False): 91*800a58d9SAndroid Build Coastguard Worker """Main entry point to the task runner. 92*800a58d9SAndroid Build Coastguard Worker 93*800a58d9SAndroid Build Coastguard Worker Args: 94*800a58d9SAndroid Build Coastguard Worker force_setup: Boolean, True to force execute Run method no matter 95*800a58d9SAndroid Build Coastguard Worker the result of ShoudRun. 96*800a58d9SAndroid Build Coastguard Worker """ 97*800a58d9SAndroid Build Coastguard Worker if self.ShouldRun() or force_setup: 98*800a58d9SAndroid Build Coastguard Worker self.PrintWelcomeMessage() 99*800a58d9SAndroid Build Coastguard Worker self._Run() 100*800a58d9SAndroid Build Coastguard Worker else: 101*800a58d9SAndroid Build Coastguard Worker logger.info("Skipping setup step: %s", self.__class__.__name__) 102*800a58d9SAndroid Build Coastguard Worker 103*800a58d9SAndroid Build Coastguard Worker def _Run(self): 104*800a58d9SAndroid Build Coastguard Worker """run the setup procedure.""" 105*800a58d9SAndroid Build Coastguard Worker raise NotImplementedError() 106