diff options
Diffstat (limited to 'feed/sse_feed.py')
-rwxr-xr-x | feed/sse_feed.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/feed/sse_feed.py b/feed/sse_feed.py new file mode 100755 index 0000000..c6f0de9 --- /dev/null +++ b/feed/sse_feed.py @@ -0,0 +1,81 @@ +#!/usr/bin/python + +import sys, os, MySQLdb, stat +from popen2 import Popen3 + +supported = [".c", ".h"] + +def supported_source(fn): + + for e in supported: + if fn.endswith(e): + return True + + return False + +def last_insert_id(cursor): + cursor.execute("SELECT LAST_INSERT_ID()"); + return cursor.fetchone()[0] + +def process_file(package_id, root, path): + global cursor + print "Processing %s" % path + + cursor.execute("INSERT INTO file (package_id, path, language_id) VALUES (%i, '%s', '0')" % (package_id, path)); + + file_id = last_insert_id(cursor); + + p = Popen3("lex-c %s" % (os.path.join(root, path))) + + for identifier in p.fromchild: + text = identifier.strip() + + cursor.execute("INSERT IGNORE INTO word (text, type, file_id) VALUES ('%s', 'word', '%i')" % (text, file_id)) + cursor.execute("UPDATE word SET cnt=cnt+1 WHERE text='%s' AND type='word' AND file_id=%i" % (text, file_id)) + + if p.wait() != 0: + print "WARNING: Subprocess failed!" + + del p + +def handle_file(package_id, root, path, filename): + + t = sys.lstat(os.path.join(path, filename)) + + if stat.F_ISREG(t.st_mode): + + extension = filename.split(".")[-1] + + if extension in ("c", "h"): + process_file(package_id, root, os.path.join(path, filename)) + return + + os.unlink(os.path.join(root, path, filename)) + +def handle_tree(path, name, url, md): + global cursor + + cursor.execute("INSERT INTO package (path, name, url, timestamp, md) VALUES ('%s', '%s', '%s', NOW(), '%s')" % (path + "/%s", name, url, md)); + package_id = last_insert_id(cursor); + + path = os.path.realpath(path) + + for dirpath, dirs, files in os.walk(path): + for f in files: + assert path + "/" == (dirpath + "/") [:len(path)+1] + + handle_file(package_id, path, dirpath[len(path)+1:], f) + +if __name__ == "__main__": + db = MySQLdb.connect(host = "localhost", user = "sse_web", passwd = "ece6Yoli", db = "sse") + cursor = db.cursor(); + cursor.execute("SET AUTOCOMMIT=0") + cursor.execute("START TRANSACTION") + + assert len(sys.argv) == 5 + + handle_tree(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) + + cursor.execute("COMMIT") + cursor.close() + db.close() |