summaryrefslogtreecommitdiffstats
path: root/feed/sse_feed.py
blob: c6f0de992ef15ca5db4e730a2dc5492d227c6802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()