-
Notifications
You must be signed in to change notification settings - Fork 3
/
install_templates_to_db.py
148 lines (96 loc) · 4.4 KB
/
install_templates_to_db.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import mysql.connector
import argparse
import os, glob
class Template:
def __init__(self, fileName):
self.name = ''
templatefile = open(fileName, 'r')
data = templatefile.readlines()
self.text = '\n'.join(data)
self.name = " ".join(data[0].strip().split(' ')[1:])
def getTemplate(self):
return self.text
def getName(self):
return self.name
def listTemplates(installTemplate = False):
baseFolder = os.getcwd()
if installTemplate:
baseFolder += os.path.sep + "install_templates" + os.path.sep + "*.igui"
else:
baseFolder += os.path.sep + "templates" + os.path.sep + "*.gui"
print("Searching file: " + baseFolder)
globIt = glob.iglob(baseFolder )
vReturn = []
for x in globIt:
vReturn.append(x)
return vReturn
def createUser(cnx, cursor, username, usermail):
add_template = ("insert into users (name, email) values (%s, %s)")
cursor.execute(add_template, (username, usermail))
cnx.commit()
return cursor.lastrowid
def getUserID(cnx, cursor, args, simulate):
findUser_template = ("select id from users where name = %s and email = %s")
setFoundUsers = set()
if cnx != None:
cursor.execute(findUser_template, (args.user_name, args.user_email))
for (id, ) in cursor:
setFoundUsers.add(id)
if len(setFoundUsers) == 0:
if simulate:
return -1
return createUser(cnx, cursor, args.user_name, args.user_email)
return list(setFoundUsers)[0]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Merge templates from hdd to database")
parser.add_argument('-s', '--host', type=str, default="localhost", help="database host")
parser.add_argument('-d', '--database', type=str, required=True, help="database name")
parser.add_argument('-u', '--user', type=str, required=True, help="database user")
parser.add_argument('-p', '--password', type=str, required=True, help="database user password")
parser.add_argument('--port', type=int, default=3306, help="database port")
parser.add_argument('-un', '--user_name', type=str, required=True, help="user name")
parser.add_argument('-ue', '--user_email', type=str, required=True,
help="user email (not showed if --anonymous supplied)")
parser.add_argument('-a', '--anonymous', action='store_true', default=False, help="submit anonymously")
parser.add_argument('--simulate', action='store_true', default=False, help="does not create any db entries")
args = parser.parse_args()
cnx = None
cursor = None
try:
cnx = mysql.connector.connect(user=args.user, password=args.password, host=args.host, database=args.database, port=args.port)
cursor = cnx.cursor()
except mysql.connector.Error as err:
print(err)
userid = getUserID(cnx, cursor, args, args.simulate)
add_template = ("insert into templates (displayname, template, type, user, anonym, omictools) values (%s, %s, %s, %s, %s, '')")
delete_existing_template = ("delete from templates where user = %s and type = %s and displayname = %s")
install_templates = listTemplates(True)
print("Inserting files: " + str(install_templates))
anonym = 1 if args.anonymous == True else 0
templateList = open("./template_list", "w")
for templateFile in install_templates:
template = Template(templateFile)
insertdata = (template.getName(), template.getTemplate(), 1, userid, anonym)
print("Inserting Install Template: " + insertdata[0])
print("Inserting length : " + str(len(insertdata[1])))
print("Inserting for user : " + str(insertdata[3]))
print("Inserting anonymously : " + str(insertdata[4]))
templateList.write(template.getName() + "\n")
if args.simulate:
continue
try:
print("Delete old")
deletedata = (insertdata[3], insertdata[2], insertdata[0])
print(deletedata)
cursor.execute(delete_existing_template, deletedata)
cnx.commit()
print("Insert new")
cursor.execute(add_template, insertdata)
cnx.commit()
except mysql.connector.Error as err:
print("Error:")
print(err)
print("in")
print(cursor.statement)
if cnx != None:
cnx.close()