Source

pypy / pypy / doc / parse_logs.py

#!/usr/bin/env python
import log_reader

def daily_date_hasher(date):
    return ((date.year, date.month, date.day))

def read_log(filename, path_guesser, date_hasher=daily_date_hasher,
             max_reads=None):
    assert callable(path_guesser)
    lr = log_reader.ApacheReader(filename)
    ret = {}
    keys = []
    for num, i in enumerate(lr):
        if max_reads and num > max_reads:
            return keys, ret
        if 'path' in i:
            if path_guesser(i['path']):
                hash = date_hasher(i['time'])
                if hash in ret:
                    ret[hash] += 1
                else:
                    ret[hash] = 1
                    keys.append(hash)
    return keys, ret

def test_daily_date_hasher():
    from datetime import datetime
    one = datetime(2007, 2, 25, 3, 10, 50)
    two = datetime(2007, 2, 25, 7, 1, 5)
    three = datetime(2007, 3, 25, 3, 10, 50)
    assert daily_date_hasher(one) == daily_date_hasher(two)
    assert daily_date_hasher(one) != daily_date_hasher(three)

def test_read_log():
    def path_guesser(path):
        return path.startswith("/viewvc")
    keys, ret = read_log("code.log", path_guesser)
    assert sum(ret.values()) == 51
    assert sorted(keys) == keys
    assert ret[(2007, 2, 25)] == 51

if __name__ == "__main__":
    import sys
    import re
    if len(sys.argv) == 3:
        max_reads = int(sys.argv[2])
    elif len(sys.argv) != 2:
        print "Usage ./parse_logs.py <logfile> [max reads]"
        sys.exit(1)
    else:
        max_reads = None

    def path_guesser(path):
        return "pypy" in path
    
    keys, ret = read_log(sys.argv[1], path_guesser, max_reads=max_reads)
    for key in keys:
        year, month, day = key
        print "%d-%d-%d, %d" % (year, month, day, ret[key])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.