1 |
commit: e5bdc5bc099096186e00e61604484fc6a1ecdeb2 |
2 |
Author: Antanas Uršulis <antanas.ursulis <AT> gmail <DOT> com> |
3 |
AuthorDate: Mon Jul 29 18:55:51 2013 +0000 |
4 |
Commit: Antanas Ursulis <antanas.ursulis <AT> gmail <DOT> com> |
5 |
CommitDate: Mon Jul 29 18:55:51 2013 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/log-analysis.git;a=commit;h=e5bdc5bc |
7 |
|
8 |
Database (SQL) class for functionality common across all processors |
9 |
|
10 |
Currently uses MySQLdb. Schema included. |
11 |
|
12 |
--- |
13 |
database.py | 22 ++++++++++++++++++++++ |
14 |
flask_app.py | 18 ++++++++++++++---- |
15 |
portage_processor.py | 9 +++++---- |
16 |
schema.sql | 12 ++++++++++++ |
17 |
simple_client.py | 1 + |
18 |
submission.proto | 3 ++- |
19 |
6 files changed, 56 insertions(+), 9 deletions(-) |
20 |
|
21 |
diff --git a/database.py b/database.py |
22 |
new file mode 100644 |
23 |
index 0000000..5202876 |
24 |
--- /dev/null |
25 |
+++ b/database.py |
26 |
@@ -0,0 +1,22 @@ |
27 |
+from contextlib import closing |
28 |
+import MySQLdb |
29 |
+ |
30 |
+class DatabaseConnection: |
31 |
+ def __init__(self, conn): |
32 |
+ self.conn = conn |
33 |
+ |
34 |
+ def insert_file(self, path, group_id): |
35 |
+ with closing(self.conn.cursor()) as c: |
36 |
+ c.execute("insert into `files` (`path`, `group_id`) values (%s, %s)", (path, group_id)) |
37 |
+ self.conn.commit() |
38 |
+ return c.lastrowid |
39 |
+ |
40 |
+ def insert_group(self, name, provider, date): |
41 |
+ with closing(self.conn.cursor()) as c: |
42 |
+ c.execute("insert into `groups` (`name`, `provider`, `date`) values (%s, %s, %s)", (name, provider, date)) |
43 |
+ self.conn.commit() |
44 |
+ return c.lastrowid |
45 |
+ |
46 |
+def get_connection(user, passwd, db): |
47 |
+ conn = MySQLdb.connect(user=user, passwd=passwd, db=db) |
48 |
+ return DatabaseConnection(conn) |
49 |
|
50 |
diff --git a/flask_app.py b/flask_app.py |
51 |
index 832702c..5356bc4 100644 |
52 |
--- a/flask_app.py |
53 |
+++ b/flask_app.py |
54 |
@@ -4,14 +4,24 @@ When run as a script, the Flask development server is started. |
55 |
""" |
56 |
|
57 |
import os, socket |
58 |
-import submission_pb2, storage |
59 |
-from flask import Flask, request |
60 |
+import submission_pb2, storage, database |
61 |
+from flask import Flask, request, g |
62 |
|
63 |
from portage_processor import PortageProcessor |
64 |
|
65 |
app = Flask(__name__) |
66 |
store = storage.FilesystemStorage('logs/') |
67 |
-processors = {'portage' : PortageProcessor(None, store)} # TODO: initialise from config file |
68 |
+processors = {'portage' : PortageProcessor(store)} # TODO: initialise from config file |
69 |
+ |
70 |
+@×××.before_request |
71 |
+def before_request(): |
72 |
+ g.db = database.get_connection('gsoc', 'gsocpasswd', 'loganalysis') |
73 |
+ |
74 |
+@×××.teardown_request |
75 |
+def teardown_request(exception): |
76 |
+ db = getattr(g, 'db', None) |
77 |
+ if db is not None: |
78 |
+ db.conn.close() |
79 |
|
80 |
@app.route('/') |
81 |
def index(): |
82 |
@@ -23,7 +33,7 @@ def submit(): |
83 |
submission.ParseFromString(request.data) |
84 |
source = socket.getfqdn(request.remote_addr) # TODO: is this ok? |
85 |
|
86 |
- processors[submission.provider].process(submission, source) |
87 |
+ processors[submission.provider].process(submission, source, g.db) |
88 |
return '' |
89 |
|
90 |
if __name__ == '__main__': |
91 |
|
92 |
diff --git a/portage_processor.py b/portage_processor.py |
93 |
index 2403cdf..66fb970 100644 |
94 |
--- a/portage_processor.py |
95 |
+++ b/portage_processor.py |
96 |
@@ -1,4 +1,4 @@ |
97 |
-import re, StringIO |
98 |
+import os, re, StringIO, time |
99 |
|
100 |
class PortageProcessor: |
101 |
_r = { |
102 |
@@ -10,11 +10,11 @@ class PortageProcessor: |
103 |
'escapes' : re.compile(r"\x1b\[[^\x40-\x7e]*[\x40-\x7e]") |
104 |
} |
105 |
|
106 |
- def __init__(self, db, storage): |
107 |
- self.db = db |
108 |
+ def __init__(self, storage): |
109 |
self.storage = storage |
110 |
|
111 |
- def process(self, request, source): |
112 |
+ def process(self, request, source, db): |
113 |
+ group_id = db.insert_group(request.group_name, 'portage', int(time.time())) |
114 |
for f in request.files: |
115 |
matches = 0 |
116 |
pkg_failed = False |
117 |
@@ -72,3 +72,4 @@ class PortageProcessor: |
118 |
''') |
119 |
|
120 |
self.storage.save_file(source, f.filename, output.getvalue()) |
121 |
+ file_id = db.insert_file(os.path.join(source, f.filename), group_id) |
122 |
|
123 |
diff --git a/schema.sql b/schema.sql |
124 |
new file mode 100644 |
125 |
index 0000000..564385e |
126 |
--- /dev/null |
127 |
+++ b/schema.sql |
128 |
@@ -0,0 +1,12 @@ |
129 |
+create table if not exists `files` ( |
130 |
+ `id` int primary key auto_increment, |
131 |
+ `path` text not null, |
132 |
+ `group_id` int not null |
133 |
+); |
134 |
+ |
135 |
+create table if not exists `groups` ( |
136 |
+ `id` int primary key auto_increment, |
137 |
+ `name` text not null, |
138 |
+ `provider` varchar(16) not null, |
139 |
+ `date` int not null |
140 |
+); |
141 |
|
142 |
diff --git a/simple_client.py b/simple_client.py |
143 |
index ab4bccf..c89b6c1 100644 |
144 |
--- a/simple_client.py |
145 |
+++ b/simple_client.py |
146 |
@@ -7,6 +7,7 @@ import submission_pb2, sys, urllib2, os |
147 |
def send_submission(filenames): |
148 |
submission = submission_pb2.Submission() |
149 |
submission.provider = "portage" |
150 |
+ submission.group_name = "Manual submission" |
151 |
|
152 |
for f in filenames: |
153 |
new_file = submission.files.add() |
154 |
|
155 |
diff --git a/submission.proto b/submission.proto |
156 |
index 42cf97c..3cbf474 100644 |
157 |
--- a/submission.proto |
158 |
+++ b/submission.proto |
159 |
@@ -5,5 +5,6 @@ message Submission { |
160 |
} |
161 |
|
162 |
required string provider = 1; |
163 |
- repeated File files = 2; |
164 |
+ optional string group_name = 2; |
165 |
+ repeated File files = 3; |
166 |
} |