def latest_version(self): # pylint: disable=no-self-use """Latest version available for current platform.""" try: releases = requests.get(RELEASES_API).json() if 'message' in releases: text = 'Error while downloading release information: {}'.format( releases['message']) logging.error(colors.stylize(text, colors.ERROR)) raise LatestVersionCheckError(str(text)) for release in releases: if release['prerelease']: continue for each_asset in release['assets']: if sdk_utils.PLATFORM in each_asset.get('name', '').lower(): return release['tag_name'] raise LatestVersionCheckError( 'Unable to find a release for {} platform.'.format( sdk_utils.PLATFORM)) except LatestVersionCheckError: raise except Exception as err: logging.exception(colors.stylize(str(err), colors.ERROR)) text = 'Unable to check for the latest version: {}'.format( str(err)) logging.error(colors.stylize(text, colors.ERROR)) raise LatestVersionCheckError(str(err))
def print(self, log_function): label_max_width = 0 for label, _, _, _ in self.messages: label_max_width = max(len(label), label_max_width) for label, message, label_color, message_color in self.messages: if label_color: label = colors.stylize(label.rjust(label_max_width), label_color) if message_color: message = colors.stylize(message, message_color) log_function('{} {}'.format(label, message))
def translations_machine(pod_path, locale): """Translates the pod message catalog using machine translation.""" root = os.path.abspath(os.path.join(os.getcwd(), pod_path)) pod = pods.Pod(root, storage=storage.FileStorage) with pod.profile.timer('grow_translations_machine'): pod.catalogs.extract() for identifier in locale: catalog = pod.catalogs.get(identifier) catalog.update() catalog.machine_translate() pod.logger.info(colors.stylize( 'WARNING! Use machine translations with caution.', colors.CAUTION)) pod.logger.info(colors.stylize( 'Machine translations are not intended for use in production.', colors.CAUTION)) return pod
def trigger_file_changed(self, pod_path): try: self.pod.extensions_controller.trigger('dev_file_change', pod_path) except Exception: # pylint: disable=broad-except # Avoid an inconsistent state where preprocessor doesn't run again # if it encounters an exception. https://github.com/grow/grow/issues/528 colored_pod_path = colors.stylize(pod_path, colors.ERROR) self.pod.logger.exception( 'Found an error -> {}'.format(colored_pod_path))
def translations_machine(pod_path, locale, use_reroute): """Translates the pod message catalog using machine translation.""" root = os.path.abspath(os.path.join(os.getcwd(), pod_path)) pod = pods.Pod(root, storage=storage.FileStorage, use_reroute=use_reroute) with pod.profile.timer('grow_translations_machine'): pod.catalogs.extract() for identifier in locale: catalog = pod.catalogs.get(identifier) catalog.update() catalog.machine_translate() pod.logger.info( colors.stylize('WARNING! Use machine translations with caution.', colors.CAUTION)) pod.logger.info( colors.stylize( 'Machine translations are not intended for use in production.', colors.CAUTION)) return pod
def print_server_ready_message(pod, host, port): home_doc = pod.get_home_doc() root_path = home_doc.url.path if home_doc else '/' url = 'http://{}:{}{}'.format(host, port, root_path) logging.info('Pod: '.rjust(20) + pod.root) logging.info('Address: '.rjust(20) + url) ready_message = colors.stylize('Server ready. '.rjust(20), colors.HIGHLIGHT) logging.info(ready_message + 'Press ctrl-c to quit.') return url
def _display(title, message, color=None): message_format = '{} {}' if color: pod.logger.info( message_format.format( colors.stylize(title.rjust(first_column_width), color), message)) return pod.logger.info( message_format.format(title.rjust(first_column_width), message))
def verify_required_version(self): """Verify that the required version for the pod is met.""" if self.pod.grow_version is None: return sem_current = semantic_version.Version(self.current_version) spec_required = semantic_version.Spec(self.pod.grow_version) if sem_current not in spec_required: text = 'ERROR! Pod requires Grow SDK version: {}'.format( self.pod.grow_version) logging.error(colors.stylize(text, colors.ERROR)) raise LatestVersionCheckError(text)
def handle(self, event=None): if event is not None and event.is_directory: return try: if self.num_runs == 0: self.preprocessor.first_run() else: self.preprocessor.run() except Exception: # pylint: disable=broad-except # Avoid an inconsistent state where preprocessor doesn't run again # if it encounters an exception. https://github.com/grow/grow/issues/81 text = colors.stylize('Preprocessor error.', colors.ERROR) self.preprocessor.pod.logger.exception(text) self.num_runs += 1
def verify_required_version(self): """Verify that the required version for the pod is met.""" if self.pod.grow_version is None: return sem_current = semantic_version.Version(self.current_version) grow_version_pattern = '{}'.format(self.pod.grow_version) # Include pre-releases in the version check. if '-' not in grow_version_pattern: grow_version_pattern = '{}-'.format(grow_version_pattern) spec_required = semantic_version.SimpleSpec(grow_version_pattern) if sem_current not in spec_required: text = 'ERROR! Pod requires Grow SDK version: {}'.format( self.pod.grow_version) logging.error(colors.stylize(text, colors.ERROR)) raise LatestVersionCheckError(text)
def check_for_updates(self, auto_update_prompt=False, force=False): """Check for updates to the sdk.""" grow_rc_config = rc_config.RC_CONFIG # Rate limited update checks. if not grow_rc_config.needs_update_check and not force: return try: sem_current = semantic_version.Version(self.current_version) sem_latest = semantic_version.Version(self.latest_version) # Mark that we have performed a check for the update. grow_rc_config.reset_update_check() grow_rc_config.write() except LatestVersionCheckError: return if sem_latest <= sem_current: return url = TAGS_URL_FORMAT.format(self.latest_version) if sem_latest.major > sem_current.major: logging.info('') logging.info(' A new major version of the Grow SDK is available.') logging.info( ' Major version changes can be backwards incompatible.') logging.info( ' Please check the release notes for upgrade instructions.') logging.info(' Release notes: {}'.format(url)) logging.info(' Your version: {}, latest version: {}'.format( colors.stylize(str(sem_current), colors.EMPHASIS), colors.stylize(str(sem_latest), colors.EMPHASIS))) return logging.info('') logging.info(' Please update to the newest version of the Grow SDK.') logging.info(' Release notes: {}'.format(url)) logging.info(' Your version: {}, latest version: {}'.format( colors.stylize(str(sem_current), colors.EMPHASIS), colors.stylize(str(sem_latest), colors.EMPHASIS))) install_command = INSTALLER_COMMAND.format(version=sem_latest) if auto_update_prompt: use_auto_update = grow_rc_config.get('update.always', False) if use_auto_update: logging.info(' > Auto-updating to version: {}'.format( colors.stylize(str(sem_latest), colors.HIGHLIGHT))) else: # pragma: no cover try: choice = input('Auto update now? [Y]es / [n]o / [a]lways: ' ).strip().lower() except KeyboardInterrupt: choice = 'n' if choice not in ('y', 'a', ''): return if choice == 'a': grow_rc_config.set('update.always', True) grow_rc_config.write() if subprocess.call((install_command), shell=True) == 0: logging.info('Restarting...') try: # Restart on successful install. os.execl(sys.argv[0], *sys.argv) except OSError: logging.info( 'Unable to restart. Please manually restart grow.') sys.exit(-1) else: text = 'In-place update failed. Update manually or use:\n {}' logging.error(text.format(install_command)) sys.exit(-1) else: logging.info(' Update using: {}'.format(install_command)) logging.info('') return True
def success(cls, message, extras=None): """Generate a formatted success message.""" message = cls.format_message(message, extras) return colors.stylize(MESSAGE_FORMAT.format('✓', message), colors.SUCCESS)
def pre_install(cls, message, extras=None): """Generate a formatted pre install message.""" message = cls.format_message(message, extras) return colors.stylize(MESSAGE_FORMAT.format(' ', message), colors.SUCCESS)
def failure(cls, message, extras=None): """Generate a formatted failure message.""" message = cls.format_message(message, extras) return colors.stylize(MESSAGE_FORMAT.format('✘', message), colors.ERROR)
def check_for_updates(self, auto_update_prompt=False): """Check for updates to the sdk.""" grow_rc_config = rc_config.RC_CONFIG # Rate limited update checks. if not grow_rc_config.needs_update_check: return try: sem_current = semantic_version.Version(self.current_version) sem_latest = semantic_version.Version(self.latest_version) # Mark that we have performed a check for the update. grow_rc_config.reset_update_check() grow_rc_config.write() except LatestVersionCheckError: return if sem_latest <= sem_current: return url = TAGS_URL_FORMAT.format(self.latest_version) logging.info('') logging.info(' Please update to the newest version of the Grow SDK.') logging.info(' Release notes: {}'.format(url)) logging.info(' Your version: {}, latest version: {}'.format( colors.stylize(str(sem_current), colors.EMPHASIS), colors.stylize(str(sem_latest), colors.EMPHASIS))) if utils.is_packaged_app() and auto_update_prompt: use_auto_update = grow_rc_config.get('update.always', False) if use_auto_update: logging.info(' > Auto-updating to version: {}'.format( colors.stylize(str(sem_latest), colors.HIGHLIGHT))) else: # pragma: no cover try: choice = raw_input( 'Auto update now? [Y]es / [n]o / [a]lways: ').strip( ).lower() except KeyboardInterrupt: choice = 'n' if choice not in ('y', 'a', ''): return if choice == 'a': grow_rc_config.set('update.always', True) grow_rc_config.write() if subprocess.call(INSTALLER_COMMAND, shell=True) == 0: logging.info('Restarting...') try: # Restart on successful install. os.execl(sys.argv[0], *sys.argv) except OSError: logging.info( 'Unable to restart. Please manually restart grow.') sys.exit(-1) else: text = ('In-place update failed. Update manually or use:\n' ' curl https://install.grow.io | bash') logging.error(text) sys.exit(-1) else: logging.info( ' Update using: ' + colors.stylize('pip install --upgrade grow', colors.CAUTION)) logging.info('')