def log_pretty(self): logging.info(utils.colorize('\n{green}Adding files: (%s){/green}' % len(self.adds))) [logging.info(' {}'.format(add)) for add in self.adds] logging.info(utils.colorize('{yellow}Editing files: (%s){/yellow}'% len(self.edits))) [logging.info(' {}'.format(edit)) for edit in self.edits] logging.info(utils.colorize('{red}Deleting files: (%s){/red}' % len(self.deletes))) [logging.info(' {}'.format(delete)) for delete in self.deletes] logging.info(utils.colorize('{white}Unchanged files: (%s){/red}\n' % len(self.nochanges)))
def deploy(self, pod): logging.info('Deploying to: {}'.format(self.get_destination_address())) self.prelaunch() deployed_index = self.get_index_at_destination() paths_to_content = pod.dump() new_index = index.Index() new_index.update(paths_to_content) diffs = new_index.diff(deployed_index) if not diffs: text = utils.colorize('{white}Diff is empty, nothing to launch, aborted.{/white}') logging.info(text) return if self.dry_run: return if self.confirm: diffs.log_pretty() if not utils.interactive_confirm('Proceed with launch?'): logging.info('Launch aborted.') return self.start_time = time.time() index.Index.apply_diffs(diffs, paths_to_content, write_func=self.write_file, delete_func=self.delete_file) self.write_index_at_destination(new_index) logging.info('Wrote index: /{}'.format(index.Index.BASENAME)) self.postlaunch() return diffs
def deploy(self, pod, dry_run=False, confirm=False): destination_address = self.get_destination_address() logging.info('Deploying to: {}'.format(destination_address)) self._prelaunch() self.prelaunch(dry_run=dry_run) try: deployed_index = self.get_index_at_destination() paths_to_content = pod.dump() new_index = index.Index() new_index.update(paths_to_content) diffs = new_index.diff(deployed_index) if not diffs: text = utils.colorize('{white}Diff is empty, nothing to launch, aborted.{/white}') logging.info(text) return if dry_run: return if confirm: diffs.log_pretty() logging.info('About to launch => {}'.format(destination_address)) if not utils.interactive_confirm('Proceed with launch?'): logging.info('Launch aborted.') return self.start_time = time.time() index.Index.apply_diffs(diffs, paths_to_content, write_func=self.write_file, delete_func=self.delete_file, threaded=self.threaded) # TODO(jeremydw): Index should only be updated if the diff was entirely # successfully applied. self.write_index_at_destination(new_index) finally: self.postlaunch() self._postlaunch() return diffs
def get_latest_version(): logging.info('Checking for updates to the Grow SDK...') try: releases = json.loads(urllib.urlopen(RELEASES_API).read()) return releases[0]['tag_name'] except Exception as e: text = 'Could not check for updates to the SDK. Are you online?' logging.error(utils.colorize('{red}%s{/red}' % text)) raise LatestVersionCheckError(str(e))
def Run(self, argv): root = os.path.abspath(os.path.join(os.getcwd(), argv[-1])) pod = pods.Pod(root, storage=storage.FileStorage) if not FLAGS.locale: raise appcommands.AppCommandsError("Must specify: --locale.") text = ( "---\n{red}WARNING!{/red} Use machine translation with caution." " It is not intended for use in production.\n---" ) logging.info(utils.colorize(text)) translations = pod.get_translations() translations.extract() for locale in FLAGS.locale: translation = translations.get_translation(locale) translation.update_catalog() translation.machine_translate()
def check_version(auto_update_prompt=False): try: theirs = get_latest_version() yours = config.VERSION except LatestVersionCheckError: return if theirs > yours: logging.info('---') logging.info(utils.colorize('{green}Your version: %s,{/green} {yellow}latest version: %s{/yellow}' % (yours, theirs))) logging.info('A newer version of the SDK is available, so please update yours. See http://growsdk.org.') if utils.is_packaged_app() and auto_update_prompt: # If the installation was successful, restart the process. if (raw_input('Auto update now? [y/N]: ').lower() == 'y' and subprocess.call(INSTALLER_COMMAND, shell=True) == 0): logging.info('Restarting...') os.execl(sys.argv[0], *sys.argv) else: logging.info('Update using: pip install --upgrade grow') logging.info('---') else: logging.info('You have the latest version: {}'.format(yours))
def run(self): # TODO(jeremydw): Ideally, this would be capable of flushing the gettext cache and # recompiling the translations itself without requiring user action. text = "{yellow}Detected changes to translations.{/yellow} Restart the server to see changes." logging.info(utils.colorize(text))
def _start(pod, host=None, port=None, open_browser=False): root = pod.root preprocessors = pod.list_preprocessors() # Add the translation preprocessor as a builtin. preprocessors.insert(0, translation_preprocessor.TranslationPreprocessor(pod=pod)) try: # TODO(jeremydw): Custom server logs. # logger_format = ('[%(time)s] "%(REQUEST_METHOD)s %(REQUEST_URI)s" %(status)s' # if use_simple_log_format else None) # Map directory names to preprocessors. dirs_to_preprocessors = {} for preprocessor in preprocessors: for watched_dir in preprocessor.list_watched_dirs(): if watched_dir not in dirs_to_preprocessors: dirs_to_preprocessors[watched_dir] = [] dirs_to_preprocessors[watched_dir].append(preprocessor) # Run all preprocessors for the pod. [preprocessor.first_run() for preprocessor in preprocessors] # Create file watchers for each preprocessor. file_watchers_to_preprocessors = {} for dirname, preprocessors in dirs_to_preprocessors.iteritems(): dirname = os.path.join(pod.root, dirname.lstrip('/')) change_watcher = file_watchers.get_file_watcher([dirname]) change_watcher.start() file_watchers_to_preprocessors[change_watcher] = preprocessors # Start a thread where preprocessors can run if there are changes. quit_event = threading.Event() change_watcher_thread = threading.Thread( target=_loop_watching_for_changes, args=(pod, file_watchers_to_preprocessors, quit_event)) change_watcher_thread.start() # Create the development server. root = os.path.abspath(os.path.normpath(root)) handlers.set_pod_root(root) app = main_lib.application port = 8080 if port is None else port host = 'localhost' if host is None else host httpd = simple_server.make_server(host, int(port), app) except: logging.exception('Failed to start server.') quit_event.set() change_watcher_thread.join() sys.exit() try: root_path = pod.get_root_path() url = 'http://{}:{}{}'.format(host, port, root_path) logging.info('---') logging.info(utils.colorize('{blue}The Grow SDK is experimental.{/blue} Expect backwards incompatibility until v0.1.0.')) logging.info('Thank you for testing and contributing! Visit http://growsdk.org for resources.') logging.info('---') logging.info('Serving pod {} => {}'.format(root, url)) text = '{green}READY!{/green} Press Ctrl+C to shut down. Tip: Use --open to open a browser automatically.' logging.info(utils.colorize(text)) def start_browser(server_ready_event): server_ready_event.wait() if open_browser: webbrowser.open(url) server_ready_event = threading.Event() browser_thread = threading.Thread(target=start_browser, args=(server_ready_event,)) browser_thread.start() server_ready_event.set() httpd.serve_forever() browser_thread.join() except KeyboardInterrupt: logging.info('Shutting down...') httpd.server_close() # Clean up once serve exits. quit_event.set() change_watcher_thread.join() sys.exit()