Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # coding: utf-8 | |
2 | |
3 # This file is part of the Adblock Plus web scripts, | |
4 # Copyright (C) 2006-2015 Eyeo GmbH | |
5 # | |
6 # Adblock Plus is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU General Public License version 3 as | |
8 # published by the Free Software Foundation. | |
9 # | |
10 # Adblock Plus is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 import MySQLdb, itertools, json, os, sys | |
19 from sitescripts.utils import get_config | |
20 import sitescripts.filterhits.common as common | |
Sebastian Noack
2015/03/02 10:04:01
Nit: from ... import ...
kzar
2015/03/02 10:39:03
Done.
| |
21 import sitescripts.filterhits.db as db | |
22 import sitescripts.filterhits.geometrical_mean as geometrical_mean | |
23 | |
24 last_log_file = None | |
25 | |
26 def log_files(dir): | |
27 """ | |
28 Provides a generator of filter hits log files for the given directory. | |
29 Works recursively, relative path of log file is returned. | |
30 """ | |
31 for root, subdirs, files in os.walk(dir): | |
32 for f in files: | |
33 if os.path.splitext(f)[1] == ".log" and f[0].isdigit(): | |
34 yield os.path.join(root, f) | |
35 | |
36 def read_data(log_file): | |
37 """ | |
38 Read, parse and return the JSON data for the given log file name. | |
39 (As a side effect sets the global last_log_file to the log file name.) | |
40 """ | |
41 global last_log_file | |
42 try: | |
43 with open(log_file, "r") as f: | |
44 # Skip past the date and GET parameters | |
45 current = last = "" | |
46 while last + current != '" ': | |
47 last, current = current, f.read(1) | |
48 if not current: | |
49 sys.exit("Unexpected EOF in log file %s" % log_file) | |
50 | |
51 # Read the JSON | |
52 data = json.load(f) | |
53 # Keep track of the current log file in global variable in case we need to | |
54 # identify it later if there's a problem. (This works because the files ar e | |
55 # processed lazily.) | |
56 last_log_file = log_file | |
57 except IOError: | |
58 sys.exit("Could not read log file %s" % log_file) | |
59 return data | |
60 | |
61 if __name__ == "__main__": | |
62 if not len(sys.argv) == 2: | |
63 print "Usage: python -m sitescripts.filterhits.bin.process_logs /path/to/log s" | |
64 sys.exit(1) | |
65 | |
66 interval = get_config().get("filterhitstats", "interval") | |
67 | |
68 def read_update(f): | |
69 return geometrical_mean.update(interval, read_data(f)) | |
70 | |
71 if sys.argv[1].endswith(".log"): | |
72 sql = read_update(sys.argv[1]) | |
73 else: | |
74 sql = itertools.chain.from_iterable(itertools.imap(read_update, | |
75 log_files(sys.argv[1]))) | |
76 | |
77 try: | |
78 db_connection = db.connect() | |
79 db.write(db_connection, sql) | |
80 except (KeyError, MySQLdb.Error), e: | |
81 if isinstance(e, KeyError): | |
Sebastian Noack
2015/03/02 10:04:01
Where does the KeyError come from? Certainly not f
kzar
2015/03/02 10:39:03
KeyError comes from processing invalid data, this
| |
82 message = "KeyError (%s)" % str(e.args[0]) | |
83 elif isinstance(e, MySQLdb.Error): | |
84 message = "MySQL error (%d) \"%s\"" % (e.args[0], e.args[1]) | |
85 sys.exit("Failed to process file %s, all changes rolled back. %s\n" % ( | |
86 last_log_file, message | |
87 )) | |
88 finally: | |
89 if db_connection: | |
Sebastian Noack
2015/03/02 10:04:01
Again, if db.connect() fails, this results into a
kzar
2015/03/02 10:39:03
Done.
Sebastian Noack
2015/03/02 10:56:36
Sorry, I meant:
try:
db_connection = db.connect
kzar
2015/03/02 11:00:52
Yea I realised that but I figure it's OK. This scr
Sebastian Noack
2015/03/02 11:06:05
The exception would be printed as well if handled
kzar
2015/03/02 11:18:50
Well my line of reasoning was that if the database
| |
90 db_connection.close() | |
OLD | NEW |