[8319] | 1 | import sys |
---|
| 2 | import os |
---|
| 3 | import re |
---|
| 4 | import anuga.utilities.log as log |
---|
[8322] | 5 | import csv |
---|
[8319] | 6 | |
---|
[8322] | 7 | defaultOutputFile ='timing.csv' |
---|
[8319] | 8 | timingDelimiter = log.TimingDelimiter |
---|
| 9 | |
---|
[8322] | 10 | def AnalyseLog(path, output_file, log_file='anuga.log'): |
---|
| 11 | """ |
---|
| 12 | path - the directory to look for log files in. |
---|
| 13 | log_file - the |
---|
| 14 | |
---|
| 15 | """ |
---|
| 16 | log_pairs = build_log_info(path, log_file) |
---|
| 17 | if log_pairs is not None: |
---|
| 18 | write_meta_log(log_pairs, output_file) |
---|
[8319] | 19 | |
---|
[8322] | 20 | def build_log_info(path, log_file): |
---|
| 21 | log_pairs = [] |
---|
[8319] | 22 | for (path, dirs, files) in os.walk(path): |
---|
| 23 | for file in files: |
---|
[8322] | 24 | if log_file in file: |
---|
| 25 | dictResults = {} |
---|
[8319] | 26 | for line in open(os.path.join(path,file)): |
---|
| 27 | if line.find(timingDelimiter)>-1: |
---|
[8322] | 28 | key_value = line.split(timingDelimiter)[1] |
---|
| 29 | # FIXME remove the magic comma |
---|
| 30 | key_value_list = key_value.split(',') |
---|
| 31 | key = key_value_list[0].strip() |
---|
| 32 | value = key_value_list[1].strip() |
---|
| 33 | dictResults[key] = value |
---|
| 34 | #print "key", key |
---|
| 35 | #print "value", value |
---|
| 36 | log_pairs.append(dictResults) |
---|
| 37 | return log_pairs |
---|
| 38 | |
---|
| 39 | def write_meta_log(log_pairs, output_file): |
---|
| 40 | """Write the info from the log files to a file""" |
---|
| 41 | |
---|
| 42 | all_keys = {} # values aren't needed, but are there |
---|
| 43 | for log_p in log_pairs: |
---|
| 44 | all_keys.update(log_p) |
---|
| 45 | |
---|
| 46 | # sort the keys alphabetacally |
---|
| 47 | sorted_all_keys = sorted(all_keys.keys()) |
---|
| 48 | print "output_file", output_file |
---|
| 49 | han = open(output_file, 'w') |
---|
| 50 | writer = csv.DictWriter(han, delimiter=',', fieldnames=sorted_all_keys, |
---|
| 51 | extrasaction='ignore') |
---|
| 52 | # Title |
---|
| 53 | writer.writerow(dict(zip(sorted_all_keys, sorted_all_keys))) |
---|
| 54 | |
---|
| 55 | for pair in log_pairs: |
---|
| 56 | writer.writerow(pair) |
---|
| 57 | |
---|
| 58 | han.close() |
---|
[8319] | 59 | |
---|
| 60 | #################################################### |
---|
| 61 | if __name__ == '__main__': |
---|
| 62 | |
---|
[8322] | 63 | path = sys.argv[1] |
---|
[8319] | 64 | |
---|
[8322] | 65 | if len(sys.argv) < 3: |
---|
| 66 | outputFile = open(defaultOutputFile, "a") |
---|
| 67 | else: |
---|
| 68 | outputFile = open(sys.argv[2], "a") |
---|
[8319] | 69 | |
---|
[8322] | 70 | AnalyseLog(path, outputFile) |
---|
| 71 | |
---|
[8319] | 72 | |
---|
| 73 | |
---|