Пример #1
0
def main(argv):
    # Setup.
    util.InitLogging()

    # Print an alert if one was specified.
    flags = Flags()

    # If no arguments were given, it may be best to show --help.
    if len(argv) == 1:
        config = util.Config()
        if config.Sets('DEFAULT_QUERY'):
            flags.query = config.GetString('DEFAULT_QUERY')
        elif not config.GetBool('HIDE_USAGE_FOR_NO_ARGS'):
            flags.PrintHelp()

    # Initialize the formatters that will display the results of the query.
    InitFormatters()
    Formatter.show_headings = not flags.hide_headings
    if flags.list_formats:
        Formatter.PrintTypes()
        return 0

    # Read the queries from the config files.
    Queries.Init()
    Queries.show_headings = not flags.hide_headings
    if flags.list_queries:
        Queries.PrintQueries()

    elif flags.print_query:
        raw, sql = Queries.Get(flags.print_query)
        if not raw:
            sys.stderr.write('Query not found: %s\n' % flags.print_query)
            return 1
        if raw.strip() != sql.strip():
            msg = 'Query: %s\nTemplate Form:\n%s\nActual SQL:\n%s'
            print(msg % (flags.print_query, raw, sql))
        else:
            print('Query: %s\n%s' % (flags.print_query, sql))

    elif flags.query:
        # Get the formatter to be used to print the result set.
        default = util.Config().GetString('DEFAULT_FORMAT') or 'aligned'
        format_name = flags.format or default
        fmt = Formatter.Get(format_name)
        if not fmt:
            sys.stderr.write('Unknown format: %s\n' % format_name)
            return 1

        sql = Queries.Get(flags.query)[1]
        rs = util.Database().Fetch(sql, limit=flags.limit)
        fmt.Print(rs)

    return 0
Пример #2
0
def main(argv):
  # If ASH_DISABLED is set, we skip everything and exit without error.
  if os.getenv('ASH_DISABLED'): return 0

  # Setup.
  util.InitLogging()

  # Log the command, if debug logging is enabled.
  if logging.getLogger().isEnabledFor(logging.DEBUG):
    command = []
    for arg in argv:
      command.append('[%d]=\'%s\'' % (len(command), arg))
    logging.debug('argv = "' + ','.join(command) + '"')

  # Print an alert if one was specified.
  flags = Flags()
  if flags.alert:
    print >> sys.stderr, flags.alert

  # If no arguments were given, it may be best to show --help.
  if len(argv) == 1 and not util.Config().GetBool('HIDE_USAGE_FOR_NO_ARGS'):
    flags.PrintHelp()

  # Create the session id, if not already set in the environment.
  session_id = os.getenv('ASH_SESSION_ID')
  if flags.get_session_id:
    if session_id is None:
      session_id = Session().Insert()
    print session_id

  # Insert a new command into the database, if one was supplied.
  command_flag_used = bool(flags.command
    or flags.command_exit
    or flags.command_pipe_status
    or flags.command_start
    or flags.command_finish
    or flags.command_number)
  if command_flag_used:
    Command(
      flags.command, flags.command_exit, flags.command_start,
      flags.command_finish, flags.command_number, flags.command_pipe_status
    ).Insert()

  # End the current session.
  if flags.end_session:
    Session().Close()

  # Return the desired exit code.
  return flags.exit
Пример #3
0
    def Init(cls):
        if cls.queries: return

        # Load the queries from the system query file, and also the user file.
        data = []
        system_queries = util.Config().GetString('SYSTEM_QUERY_FILE')
        user_queries = os.path.join(os.getenv('HOME'), '.ash', 'queries')
        for filename in (system_queries, user_queries):
            if not filename or not os.path.exists(filename): continue
            lines = [
                x for x in open(filename).readlines() if x and x[0] != '#'
            ]
            data.extend([x[:-1] for x in lines if x[:-1]])

        # Parse the loaded config files.
        cls.queries = {}  # {name: (description, sql)}
        for match in cls.parser.finditer('\n'.join(data)):
            query_name = match.group('query_name')
            description = match.group('description') or '""'
            cls.queries[query_name] = (description[1:-1], match.group('sql'))