def Ls(self): """ List subservices and actions in the current service. """ w = self.shell.stdout.write def run(): # Print services if self.service.get_subservices(): yield termcolor.colored(' ** Services **', 'cyan') def column_iterator(): for name, service in self.service.get_subservices(): yield termcolor.colored(name, type_of_service(service).color), len(name) for line in in_columns(column_iterator(), self.shell.pty): yield line # Print actions if self.service.get_actions(): yield termcolor.colored(' ** Actions **', 'cyan') def column_iterator(): for name, action in self.service.get_actions(): yield termcolor.colored(name, type_of_action(action).color), len(name) for line in in_columns(column_iterator(), self.shell.pty): yield line lesspipe(run(), self.shell.pty)
def Ls(self, context): """ List subservices and actions in the current service. """ w = self.shell.stdout.write def run(): # Print services if self.service.get_subservices(): yield termcolor.colored(' ** Services **', 'cyan') def column_iterator(): for name, service in self.service.get_subservices(): yield termcolor.colored(name, type_of_service(service).color), len(name) for line in in_columns(column_iterator(), self.shell.pty): yield line # Print actions if self.service.get_actions(): yield termcolor.colored(' ** Actions **', 'cyan') def column_iterator(): for name, action in self.service.get_actions(): yield termcolor.colored(name, type_of_action(action).color), len(name) for line in in_columns(column_iterator(), self.shell.pty): yield line lesspipe(run(), self.shell.pty)
def Find(self, context): def _list_nested_services(service, prefix): for name, action in service.get_actions(): yield '%s %s' % (prefix, termcolor.colored(name, service.get_group().color)) for name, subservice in service.get_subservices(): # Check the parent, to avoid loops. if subservice.parent == service: for i in _list_nested_services(subservice, '%s %s' % (prefix, termcolor.colored(name, subservice.get_group().color))): yield i lesspipe(_list_nested_services(self.service, ''), self.shell.pty)
def Find(self): def _list_nested_services(service, prefix): for name, action in service.get_actions(): yield '%s %s' % (prefix, termcolor.colored(name, service.get_group().color)) for name, subservice in service.get_subservices(): # Check the parent, to avoid loops. if subservice.parent == service: for i in _list_nested_services(subservice, '%s %s' % (prefix, termcolor.colored(name, subservice.get_group().color))): yield i lesspipe(_list_nested_services(self.service, ''), self.shell.pty)
def print_all_completions(self, all_completions): self.stdout.write('\r\n') # Create an iterator which yields all the comments (in their color), # and pass it through in_columns/lesspipe def column_items(): for w in all_completions: handler_type = w[1].handler_type text = '%s%s' % (termcolor.colored(w[0], handler_type.color), termcolor.colored(handler_type.postfix, handler_type.postfix_color)) length = len(w[0]) + len(handler_type.postfix) yield text, length lesspipe(in_columns(column_items(), self.pty), self.pty)
def print_all_completions(self, all_completions): self.stdout.write('\r\n') # Create an iterator which yields all the comments (in their color), # and pass it through in_columns/lesspipe def column_items(): for w in all_completions: handler_type = w[1].handler_type text = '%s%s' % ( termcolor.colored(w[0], handler_type.color), termcolor.colored(handler_type.postfix, handler_type.postfix_color)) length = len(w[0]) + len(handler_type.postfix) yield text, length lesspipe(in_columns(column_items(), self.pty), self.pty)
def SourceCode(self): """ Print the source code of a service. """ import inspect from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import TerminalFormatter from deployer.console import choice, NoInput options = [] for m in self.service.__class__.__mro__: if m.__module__ != 'deployer.service' and m != object: options.append( ('%s.%s' % ( termcolor.colored(m.__module__, 'red'), termcolor.colored(m.__name__, 'yellow')), m) ) if len(options) > 1: try: service_class = choice('Choose service definition', options) except NoInput: return else: service_class = options[0][1] def run(): try: # Retrieve source source = inspect.getsource(service_class) # Highlight code source = highlight(source, PythonLexer(), TerminalFormatter()) for l in source.split('\n'): yield l.rstrip('\n') except IOError: yield 'Could not retrieve source code.' lesspipe(run(), self.shell.pty)
def Inspect(self, context): """ Inspection of the current service. Show host mappings and other information. """ def inspect(): # Print full name yield termcolor.colored(' Service: ', 'cyan') + \ termcolor.colored(self.service.__repr__(path_only=True), 'yellow') # Service class definition created on yield termcolor.colored(' Created on: ', 'cyan') + \ termcolor.colored(self.service._creation_date, 'red') # Print mro yield termcolor.colored(' Mro:', 'cyan') i = 1 for m in self.service.__class__.__mro__: if m.__module__ != 'deployer.service' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored('%s.' % m.__module__, 'red') + \ termcolor.colored('%s' % m.__name__, 'yellow') i += 1 # Print host mappings yield termcolor.colored(' Hosts:', 'cyan') for role in sorted(self.service.hosts._hosts.keys()): items = self.service.hosts._hosts[role] yield termcolor.colored(' "%s"' % role, 'yellow') i = 1 for host in sorted(items, key=lambda h:h.slug): yield termcolor.colored(' %3i ' % i, 'cyan') + \ termcolor.colored('%-25s (%s)' % (host.slug, getattr(host, 'address', '')), 'red') i += 1 # Print the first docstring (look to the parents) for m in self.service.__class__.__mro__: if m.__module__ != 'deployer.service' and m != object and m.__doc__: yield termcolor.colored(' Docstring:\n', 'cyan') + \ termcolor.colored(m.__doc__ or '<None>', 'red') break # Actions yield termcolor.colored(' Actions:', 'cyan') def item_iterator(): for name, a in self.service.get_actions(): yield termcolor.colored(name, 'red'), len(name) for line in in_columns(item_iterator(), self.shell.pty, margin_left=13): yield line # Services yield termcolor.colored(' Sub services:', 'cyan') # Group by service group grouper = lambda i:i[1].get_group() for group, services in groupby(sorted(self.service.get_subservices(), key=grouper), grouper): yield termcolor.colored(' "%s"' % group.__name__, 'yellow') # Create iterator for all the items in this group def item_iterator(): for name, s in services: if s.parent == self.service: text = termcolor.colored(name, type_of_service(s).color) length = len(name) else: text = termcolor.colored('%s -> %s' % (name, s.__repr__(path_only=True)), type_of_service(s).color) length = len('%s -> %s' % (name, s.__repr__(path_only=True))) yield text, length # Show in columns for line in in_columns(item_iterator(), self.shell.pty, margin_left=13): yield line lesspipe(inspect(), self.shell.pty)
def Inspect(self): """ Inspection of the current service. Show host mappings and other information. """ def inspect(): # Print full name yield termcolor.colored(' Service: ', 'cyan') + \ termcolor.colored(self.service.__repr__(path_only=True), 'yellow') # Service class definition created on yield termcolor.colored(' Created on: ', 'cyan') + \ termcolor.colored(self.service._creation_date, 'red') # Print mro yield termcolor.colored(' Mro:', 'cyan') i = 1 for m in self.service.__class__.__mro__: if m.__module__ != 'deployer.service' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored('%s.' % m.__module__, 'red') + \ termcolor.colored('%s' % m.__name__, 'yellow') i += 1 # File names yield termcolor.colored(' Files:', 'cyan') i = 1 for m in self.service.__class__.__mro__: if m.__module__ != 'deployer.service' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored(getfile(m), 'red') i += 1 # Print host mappings yield termcolor.colored(' Hosts:', 'cyan') for role in sorted(self.service.hosts._hosts.keys()): items = self.service.hosts._hosts[role] yield termcolor.colored(' "%s"' % role, 'yellow') i = 1 for host in sorted(items, key=lambda h:h.slug): yield termcolor.colored(' %3i ' % i, 'cyan') + \ termcolor.colored('%-25s (%s)' % (host.slug, getattr(host, 'address', '')), 'red') i += 1 # Print the first docstring (look to the parents) for m in self.service.__class__.__mro__: if m.__module__ != 'deployer.service' and m != object and m.__doc__: yield termcolor.colored(' Docstring:\n', 'cyan') + \ termcolor.colored(m.__doc__ or '<None>', 'red') break # Actions yield termcolor.colored(' Actions:', 'cyan') def item_iterator(): for name, a in self.service.get_actions(): yield termcolor.colored(name, 'red'), len(name) for line in in_columns(item_iterator(), self.shell.pty, margin_left=13): yield line # Services yield termcolor.colored(' Sub services:', 'cyan') # Group by service group grouper = lambda i:i[1].get_group() for group, services in groupby(sorted(self.service.get_subservices(), key=grouper), grouper): yield termcolor.colored(' "%s"' % group.__name__, 'yellow') # Create iterator for all the items in this group def item_iterator(): for name, s in services: if s.parent == self.service: text = termcolor.colored(name, type_of_service(s).color) length = len(name) else: text = termcolor.colored('%s -> %s' % (name, s.__repr__(path_only=True)), type_of_service(s).color) length = len('%s -> %s' % (name, s.__repr__(path_only=True))) yield text, length # Show in columns for line in in_columns(item_iterator(), self.shell.pty, margin_left=13): yield line lesspipe(inspect(), self.shell.pty)