scripts/search-index
author Tero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 00:33:21 +0200
changeset 89 2dc6de43f317
parent 88 0b8e2ba5f76f
child 93 48fca00689e3
permissions -rwxr-xr-x
add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
     1
#!/usr/bin/env python2.5
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
     2
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
    Tool for accessing the search index
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
"""
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
     7
# XXX: fix path
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
import datetime, pytz
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    12
# configuration and the LogSearchIndex module
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    13
import config, log_search, channels
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    14
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    15
def _open_index (options, open_mode) :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    16
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    17
        Opens the LogSearchIndex
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    18
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    19
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    20
    return log_search.LogSearchIndex(config.LOG_CHANNELS, options.index_path, open_mode)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    21
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    22
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    23
def _open_index_and_channel (options, channel_name, open_mode) :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    24
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    25
        Opens+returns a LogSearchIndex and a LogChannel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    26
    """
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    27
    
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    28
    # open the LogSearchIndex
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    29
    index = _open_index(options, open_mode)
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    30
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    31
    # open the channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    32
    channel = config.LOG_CHANNELS.lookup(channel_name)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    33
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    34
    # return
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    35
    return index, channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    36
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    37
def _load_channel_date (index, options, channel, date) :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    38
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    39
        Loads the logs for the given date from the channel's LogSource into the given LogSearchIndex
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    40
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    41
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    42
    if not options.quiet :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    43
        print "%s %s..." % (channel.id, date.strftime(channel.source.filename_fmt)),
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    44
        
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    45
    try :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    46
        # load lines for date
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    47
        lines = channel.source.get_date(date)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    48
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    49
    except Exception, e :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    50
        if not options.skip_missing :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    51
            raise
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    52
            
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    53
        if not options.quiet :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    54
            print "Skipped: %s" % (e, )
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    55
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    56
    else :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    57
        # insert -> count
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    58
        count = index.insert(channel, lines)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    59
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    60
        if not options.quiet :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    61
            print "OK: %d lines" % count
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    63
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    64
def _parse_date (options, date_str, tz=None, fmt='%Y-%m-%d') :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    65
    """
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    66
        Parse the given datetime, using the given timezone(defaults to options.tz) and format
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    67
    """
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    68
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    69
    # default tz
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    70
    if not tz :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    71
        tz = options.tz
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    72
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    73
    try :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    74
        # parse
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    75
        return datetime.datetime.strptime(date_str, fmt).replace(tzinfo=tz)
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    76
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    77
    except Exception, e :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    78
        raise CommandError("[ERROR] Invalid date: %s: %s" % (date_str, e))
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    79
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    80
def _output_lines (options, lines) :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    81
    """
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    82
        Display the formatted LogLines
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    83
    """
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    84
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    85
    # display as plaintext
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    86
    for line, txt_data in options.formatter.format_txt(lines, full_timestamps=True) :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    87
        print txt_data
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
    88
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    89
class CommandError (Exception) :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    90
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    91
        Error with command-line arguments
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    92
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    93
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    94
    pass
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    95
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    96
def cmd_create (options) :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    97
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    98
        Creates a new index
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
    99
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   100
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   101
    # open index
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   102
    index = _open_index(options, 'c' if not options.force else '*')
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   103
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   104
    # that's all
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   105
    pass
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   106
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
def cmd_load (options, channel_name, *dates) :
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    """
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   109
        Loads the logs for a specific channel for the given dates (in terms of the channe logs' timezone) into the index
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
    """
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   112
    # open index/channel
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   113
    index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a')
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   115
    # handle each date
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   116
    for date_str in dates :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   117
        # prase date
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   118
        try :
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   119
            date = _parse_date(options, date_str, channel.source.tz)
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   120
        
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   121
        # handle errors
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   122
        except CommandError, e :
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   123
            if options.skip_missing :
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   124
                print "[ERROR] %s" % (date_name, e)
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   125
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   126
            else :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   127
                raise
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   128
        
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   129
        # otherwise, load
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   130
        else :        
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   131
            _load_channel_date(index, options, channel, date)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   133
def cmd_load_month (options, channel_name, *months) :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   134
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   135
        Loads the logs for a specific channel for the given months (in terms of the channel's timezone) into the index
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   136
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   137
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   138
    # open index/channel
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   139
    index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a')
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   140
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   141
    # handle each date
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   142
    for month_str in months :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   143
        # prase date
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   144
        try :
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   145
            month = _parse_date(options, month_str, channel.source.tz, '%Y-%m')
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   146
        
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   147
        # handle errors
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   148
        except CommandError, e :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   149
            # skip?
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   150
            if options.skip_missing :
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   151
                print "[ERROR] %s" % (date_name, e)
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   152
                continue
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   153
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   154
            else :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   155
                raise
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   156
        
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   157
        # get the set of days
83
a34e9f56ddda improve parser resilience, improve get_month_days, add 'Channel' item to general menu
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
   158
        days = list(channel.source.get_month_days(month))
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   159
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   160
        print "Loading %d days of logs:" % (len(days))
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   161
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   162
        # load each day
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   163
        for date in days :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   164
            # convert to datetime
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   165
            dt = datetime.datetime.combine(date, datetime.time(0)).replace(tzinfo=channel.source.tz)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   166
            
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   167
            # load
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   168
            _load_channel_date(index, options, channel, dt)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
def cmd_search (options, channel_name, query) :
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
    """
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
        Search the index for events on a specific channel with the given query
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
    """
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
    
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   175
    # sanity-check
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   176
    if options.create :
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   177
        raise Exception("--create doesn't make sense for 'search'")
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   178
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   179
    # open index/channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   180
    index, channel = _open_index_and_channel(options, channel_name, 'r')
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    # search
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
    lines = index.search_simple(channel, query)
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
    
89
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   185
    # display
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   186
    _output_lines(options, lines)
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   187
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   188
def cmd_list (options, channel_name, *dates) :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   189
    """
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   190
        List the indexed events for a specific date
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   191
    """
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   192
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   193
    # sanity-check
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   194
    if options.create :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   195
        raise Exception("--create doesn't make sense for 'search'")
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   196
    
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   197
    # open index/channel
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   198
    index, channel = _open_index_and_channel(options, channel_name, 'r')
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   199
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   200
    # ...for each date
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   201
    for date_str in dates :
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   202
        # parse date
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   203
        date = _parse_date(options, date_str)
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   204
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   205
        # list
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   206
        lines = index.list(channel, date)
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   207
        
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   208
        # display
2dc6de43f317 add utils.to/from_utc_timestamp functions, fix LogSearchIndex to store all LogLine attributes, add list() method to get LogLines for a given date, and improve scripts/search-index
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   209
        _output_lines(options, lines)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   211
def cmd_help (options, *args) :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   212
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   213
        Help about commands
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   214
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   215
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   216
    import inspect
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   217
    
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   218
    # general help stuff
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   219
    options._parser.print_help()
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   220
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   221
    # specific command?
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   222
    if args :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   223
        # the command name
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   224
        command, = args
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   225
        
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   226
        # XXX: display info about specific command
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   227
        xxx
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   228
    
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   229
    # general
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   230
    else :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   231
        print
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   232
        print "Available commands:"
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   233
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   234
        # build list of all cmd_* objects
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   235
        cmd_objects = [(name, obj) for name, obj in globals().iteritems() if name.startswith('cmd_') and inspect.isfunction(obj)]
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   236
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   237
        # sort alphabetically
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   238
        cmd_objects.sort()
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   239
        
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   240
        # iterate through all cmd_* objects
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   241
        for cmd_func_name, cmd_func in cmd_objects :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   242
            # remove cmd_ prefix
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   243
            cmd_name = cmd_func_name[4:]
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   244
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   245
            # inspect
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   246
            cmd_args, cmd_varargs, cmd_varkw, cmd_default = inspect.getargspec(cmd_func)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   247
            cmd_doc = inspect.getdoc(cmd_func)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   248
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   249
            # remove the "options" arg
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   250
            cmd_args = cmd_args[1:]
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   251
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   252
            # display
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   253
            print "\t%10s %-30s : %s" % (cmd_name, inspect.formatargspec(cmd_args, cmd_varargs, None, cmd_default), cmd_doc)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   254
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   255
def main (argv) :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   256
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   257
        Command-line main, with given argv
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   258
    """
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   259
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
    from optparse import OptionParser
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
    # define parser
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
    parser = OptionParser(
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   264
        usage           = "%prog [options] <command> [ ... ]",
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   265
        add_help_option = False,
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   266
    )
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   267
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   268
    # define command-line arguments
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   269
    parser.add_option('-h', "--help",           dest="help",            help="Show this help message and exit",     action="store_true")
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   270
    parser.add_option('-F', "--formatter",      dest="formatter_name",  help="LogFormatter to use",                 metavar="FMT",  type="choice", default="irssi",
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   271
        choices=[fmt_name for fmt_name in config.LOG_FORMATTERS.iterkeys()])
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   272
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   273
    parser.add_option('-I', "--index",          dest="index_path",      help="Index database path",                 metavar="PATH", default="logs/index")
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   274
    parser.add_option('-Z', "--timezone",       dest="tz_name",         help="Timezone for output",                 metavar="TZ",   default="UTC")
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   275
    parser.add_option('-f', "--force",          dest="force",           help="Force dangerous operation",           action="store_true")
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   276
    parser.add_option(      "--create",         dest="create",          help="Create index database",               action="store_true")
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   277
    parser.add_option(      "--skip-missing",   dest="skip_missing",    help="Skip missing logfiles",               action="store_true")
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   278
    parser.add_option(      "--quiet",          dest="quiet",           help="Supress status messages",             action="store_true")
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   280
    # parse
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   281
    options, args = parser.parse_args(argv[1:])
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   282
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   283
    # postprocess stuff
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   284
    options._parser = parser
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   285
    options.tz = pytz.timezone(options.tz_name)
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   286
    options.formatter = config.LOG_FORMATTERS[options.formatter_name](options.tz, "%H:%M:%S", None, None)
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   287
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   288
    # special-case --help
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   289
    if options.help :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   290
        return cmd_help(options, *args)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   291
    
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   292
    # must have at least the command argument
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   293
    if not args :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   294
        raise CommandError("Missing command")
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
    # pop command
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
    command = args.pop(0)
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   298
    
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   299
    # get func
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   300
    func = globals().get('cmd_%s' % command)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   301
    
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   302
    # unknown command?
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   303
    if not func :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   304
        raise CommandError("Unknown command: %s" % command)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
    # call
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
    func(options, *args)
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   308
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   309
if __name__ == '__main__' :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   310
    try :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   311
        main(sys.argv)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   312
        sys.exit(0)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   313
88
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   314
    except CommandError, e :
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   315
        print e
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   316
        sys.exit(1)
0b8e2ba5f76f improve scripts/search-index with better help info
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   317