"""Runway context."""from__future__importannotationsimportloggingimportsysfromtypingimportTYPE_CHECKING,Any,castfrom..compatimportcached_propertyfrom..core.componentsimportDeployEnvironmentfrom._baseimportBaseContextifTYPE_CHECKING:frompathlibimportPathfrom.._loggingimportPrefixAdaptor,RunwayLoggerfrom..core.type_defsimportRunwayActionTypeDefLOGGER=cast("RunwayLogger",logging.getLogger(__name__))defstr2bool(v:str)->bool:"""Return boolean value of string."""returnv.lower()in("yes","true","t","1","on","y")
[docs]classRunwayContext(BaseContext):"""Runway context object."""command:RunwayActionTypeDef|None"""Runway command/action being run."""
[docs]def__init__(self,*,command:RunwayActionTypeDef|None=None,deploy_environment:DeployEnvironment|None=None,logger:PrefixAdaptor|RunwayLogger=LOGGER,work_dir:Path|None=None,**_:Any,)->None:"""Instantiate class. Args: command: Runway command/action being run. deploy_environment: The current deploy environment. logger: Custom logger. work_dir: Working directory used by Runway. """super().__init__(deploy_environment=deploy_environmentorDeployEnvironment(),logger=logger,work_dir=work_dir,)self.command=commandself._inject_profile_credentials()
@cached_propertydefno_color(self)->bool:"""Whether to explicitly disable color output. Primarily applies to IaC being wrapped by Runway. """colorize=self.env.vars.get("RUNWAY_COLORIZE")# explicitly enable/disabletry:ifisinstance(colorize,bool):# pyright: ignore[reportUnnecessaryIsInstance]# catch Falsereturnnotcolorizeifcolorizeandisinstance(colorize,str):# type: ignorereturnnotstr2bool(colorize)exceptValueError:pass# likely invalid RUNWAY_COLORIZE valuereturnnotsys.stdout.isatty()@cached_propertydefuse_concurrent(self)->bool:"""Whether to use concurrent.futures or not. Noninteractive is required for concurrent execution to prevent weird user-input behavior. Python 3 is required because backported futures has issues with ProcessPoolExecutor. """ifself.is_noninteractive:ifnotself.sys_info.os.is_posix:LOGGER.warning("parallel execution disabled; only POSIX systems are supported currently")returnFalsereturnTrueLOGGER.warning("parallel execution disabled; not running in CI mode")returnFalse
[docs]defcopy(self)->RunwayContext:"""Copy the contents of this object into a new instance."""returnself.__class__(command=self.command,deploy_environment=self.env.copy(),logger=self.logger,work_dir=self.work_dir,)
[docs]defecho_detected_environment(self)->None:"""Print a helper note about how the environment was determined."""self.env.log_name()