示例#1
0
def usage(command_usage):
    """
    Instead of the docopt way to show the usage information we
    provide an azurectl specific usage information. The usage
    data now always consists of

    * the generic call
      azurectl [global options] service <command> [<args>]

    * the command specific usage defined by the docopt string
      short form by default, long form with -h | --help

    * the global options
    """
    with open(Defaults.project_file('cli.py'), 'r') as cli:
        program_code = cli.readlines()

    global_options = '\n'
    process_lines = False
    for line in program_code:
        if line.rstrip().startswith('global options'):
            process_lines = True
        if line.rstrip() == '"""':
            process_lines = False
        if process_lines:
            global_options += format(line)

    print 'usage: azurectl [global options] service <command> [<args>]\n'
    print format(command_usage).replace('usage:', '      ')
    if 'global options' not in command_usage:
        print format(global_options)

    if not format(command_usage).startswith('usage:'):
        error_details = format(command_usage).splitlines()[0]
        print(error_details)
示例#2
0
 def __get_command_implementations(self, service):
     command_implementations = []
     glob_match = Defaults.project_file('.') + '/*task.py'
     for source_file in glob.iglob(glob_match):
         with open(source_file, 'r') as source:
             for line in source:
                 if re.search('usage: (.*)', line):
                     command_path = os.path.basename(source_file).split('_')
                     if command_path[0] == service:
                         command_path.pop()
                         command_implementations.append(
                             ' '.join(command_path)
                         )
                     break
     return command_implementations
示例#3
0
 def load_command(self):
     if self.loaded:
         return self.loaded
     command = self.get_command()
     service = self.get_servicename()
     if not command:
         raise AzureLoadCommandUndefined(
             'No command specified for %s service' % service
         )
     command_source_file = Defaults.project_file(
         'commands/' + service + '_' + command + '.py'
     )
     if not os.path.exists(command_source_file):
         prefix = 'usage:'
         for service_command in self.__get_command_implementations(service):
             print '%s azurectl %s' % (prefix, service_command)
             prefix = '      '
         raise SystemExit
     self.loaded = importlib.import_module(
         'azurectl.commands.' + service + '_' + command
     )
     return self.loaded
示例#4
0
 def load_command(self):
     command = self.get_command()
     service = self.get_servicename()
     if service == 'compat':
         return self.invoke_kiwicompat(
             self.all_args['<legacy_args>'][1:]
         )
     if not command:
         raise KiwiLoadCommandUndefined(
             'No command specified for %s service' % service
         )
     command_source_file = Defaults.project_file(
         service + '_' + command + '_task.py'
     )
     if not os.path.exists(command_source_file):
         from logger import log
         log.info('Did you mean')
         for service_command in self.__get_command_implementations(service):
             log.info('--> kiwi %s', service_command)
         raise SystemExit
     self.command_loaded = importlib.import_module(
         'kiwi.' + service + '_' + command + '_task'
     )
     return self.command_loaded