-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_database.py
95 lines (73 loc) · 3.62 KB
/
update_database.py
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
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import sqlite3
import MySQLdb as mdb
def text_btw(text, start, end, reverse = False):
try:
if not reverse:
num_s = text.index( start ) + len( start )
text = text[num_s:]
num_e = text.find(end)
return text[:num_e].decode('utf8')
elif reverse:
num_e = text.find(end)
text = text[:num_e]
num_s = text.index( start ) + len( start )
return text[num_s:].decode('utf8')
except Exception, e:
return ""
def text_btw_replace(text, start, end, replace):
try:
num_s = text.index( start )+len(start)
num_e = text.find(end)
print num_s, num_e
text_pre = text[:num_s]
text_post= text[num_e:]
out = (text_pre+replace+text_post)
return out
except Exception, e:
return text
mlab_rep = os.path.expanduser("~/repos/Modules/")
#db = sqlite3.connect('MLABdb.db')
#cursor = db.cursor()
db = mdb.connect('localhost', 'root', 'root', 'MLAB')
cur = db.cursor()
for root, dirs, files in os.walk(mlab_rep):
for file in files:
if file.endswith('README.md'):
root_head, root_tail = os.path.split(root)
readme = open(os.path.join(root, file), 'r').read()
name = text_btw(readme, "<!--- Name:", ":")
author = text_btw(readme, "<!--- Author:", ":")
email = text_btw(readme, "<!--- AuthorEmail:", ":")
Tags = text_btw(readme, "<!--- Tags:", ":").split('|')
LongName = text_btw(readme, "<!--- LongName --->", "<!--- ELongName --->")
lead = text_btw(readme, "<!--- Lead --->", "<!--- ELead --->")
Description = text_btw(readme, "<!--- Description --->", "<!--- EDescription --->")
Content = text_btw(readme, "<!--- Content --->", "<!--- EContent --->")
leadImg = text_btw(readme, "![LeadImg](", ")")
#lead = ""
try:
#cur.execute("INSERT INTO module (name, long_name_en, description_en, author, directory) VALUES('%s', '%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE id=id;" %(name, LongName, lead, author, root_head))
cur.execute("REPLACE INTO module (name, long_name_en, description_en, author, directory) VALUES('%s', '%s', '%s', '%s', '%s');" %(name, LongName, lead, author, root_head))
#cur.execute("REPLACE INTO module SET name = '%s', long_name_en = '%s', description_en = '%s', author = '%s', directory = '%s';" %(name, LongName, lead.rstrip(), author, root_head))
#cur.execute("UPDATE module SET name = '%s', long_name_en = '%s', description_en = '%s', author = '%s', directory = '%s';" %(name, LongName, lead.rstrip(), author, root_head, name))
cur.execute("SELECT id FROM module WHERE name = '%s';" %(name))
module_id = cur.fetchone()[0]
cur.execute("DELETE FROM module_tag WHERE id_module = '%s';" %(module_id))
for tag in Tags:
try:
cur.execute("SELECT id FROM tags WHERE name = '%s';" %(tag))
tag_id = cur.fetchone()[0]
print tag_id, module_id
cur.execute("INSERT INTO module_tag (id_module, id_tag) VALUES('%s', '%s') ON DUPLICATE KEY UPDATE id=id;" %(module_id, tag_id))
except Exception, e:
print e
print "2>>>> ERR", root, name
except Exception, e:
print e
print "1>>>> ERR", root, name
db.commit()
db.close()