/
__init__.py
366 lines (335 loc) · 19.7 KB
/
__init__.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
__author__ = 'Elisey Sharov' #name of UsefulDB author
#importing standart packages for utility
import os
import json
import time
import shutil
import datetime
import platform
import configparser
#importing PIP-packages
import requests
import bs4
import pymysql.cursors
VERSION = '1.0.5' #version
work_dir = os.getcwd() #getting working directory
config = configparser.ConfigParser()
config.read('{}/config.ini'.format(work_dir), encoding='utf-8-sig')
#checking availabling information in config.ini
try:
with open('config.ini'.format(work_dir), mode='r') as f:
VERSION = config.get('main_info', 'version')
host = config.get('user_info', 'host')
db_user = config.get('user_info', 'user')
db_pass = config.get('user_info', 'password')
with open('{}\config.ini'.format(work_dir), mode='r') as f:
VERSION = config.get('main_info', 'version')
host = config.get('user_info', 'host')
db_user = config.get('user_info', 'user')
db_pass = config.get('user_info', 'password')
except Exception as er:
print(er)
host = input('Insert your host: ')
db_user = input('Insert host user: ')
db_pass = input('Insert user password: ')
with open('{}\config.ini'.format(work_dir), mode='w') as f:
f.write('[main_info]\nversion={}\n'.format(VERSION)
+ '[user_info]\nhost={}\nuser={}\npassword={}'.format(host, db_user, db_pass))
with open('config.ini', mode='w') as f:
f.write('[main_info]\nversion={}\n'.format(VERSION)
+ '[user_info]\nhost={}\nuser={}\npassword={}'.format(host, db_user, db_pass))
try: #are you have correct info of database name?
with open('host.json', mode='r') as f:
data = json.load(f)
db = data['database']
except: #if not
answer = input('Insert the database name: ')
with open('host.json', mode='w') as f:
dict = {}
dict['database'] = answer
json.dump(dict, f)
try: #try to check new version and condition of internet-connection
s = requests.get('http://scgofficial.esy.es/version.html')
b = bs4.BeautifulSoup(s.text, "html.parser")
p1 = b.select('.version .fw')
result_ver = p1[0].getText()
if VERSION == result_ver:
NET = True #you have internet-connection
else:
if VERSION == 'Beta': #you wanna to crash your PC?
NET = True #you have internet-connection
print('You have a Beta-version of UsefulDB. This version may contain more bugs and errors, than simple version')
elif VERSION == '1.0.0' or '1.0.1' or '1.0.2' or '1.0.3' or '1.0.4' or '1.0.4 Beta': #if you use this versions - you're a bad man
NET = True #you have internet-connection
print('You have old version of UsefulDB. Please, update the utility for get new features and bug fixes!')
else: #another versions is not can be
NET = True #you have internet-connection
print("Are you change the 'config.json' file?")
except: #if you haven't internet-connection or sever have problems
NET = False #you haven't internet-connection
print("System can't check avialable of new versions. Check the internet-connection and try again")
class create: #create something
def __init__(self, i):
self.Item = i
def info():
print('Wanna to create something? Call this class!')
class user: #create super-user
def __init__(self, n, p):
self.Name = n
self.Pass = p
def info():
print('Wanna get the acces to command panel? Create a super-user!')
def params(name, password, confirm=False): #name - username, password - pass, confirm - creation of super-user
#give you acces to the command panel. Its panel can break something
#please, be accurate
if confirm: #are you confirm creating os super-user? It can break something
try:
with open('root.json', mode='r') as f: #are you have super-user yet?
data = json.load(f)
user = data['user']
password = data['pass']
print('You already have super-user')
except: #if not
with open('root.json', mode='w') as f:
dict = {}
dict['user'] = name
dict['pass'] = password
json.dump(dict, f)
print('Super-user {} is created. Now you can use command panel'.format(name))
else: #if not
pass
class database: #create database
def __init__(self, n):
self.Name = n
def info():
print('Fastly database creation')
def params(name): #name - database name
now = datetime.datetime.now() #time right now
connection = pymysql.connect(host=host,
user=db_user,
password=db_pass,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
) #connection with your host
try: #trying to add new database
with connection.cursor() as cursor:
sql = 'CREATE DATABASE `%s`;' #sql-code
cursor.execute(sql, ('{}'.format(name))) #sql-code running
connection.commit() #saving changes
with open('connection_log.txt', mode='a') as f: #info fot connection_log.txt If you have a
#problem, you should send me that file too
f.write('[{}]: SQL: {}\n'.format(now, sql))
except Exception as er:
print('Something is wrong: ', er)
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: SQL: {}\n'.format(now, sql))
finally:
connection.close() #if we don't close connection, it can doing something bad
class table: #create table in database
def __init__(self, n):
self.Name = n
def info():
print('Wanna create table? Call create.table.params(name, columns)!')
def params(name, columns): #name - table name, columns - count of columns
columns = int(columns) #if you write like '3'(must be 3), system converting str to int
#connecting to database
connection = pymysql.connect(host=host,
user=db_user,
password=db_pass,
db=db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
) #connection with your host
number = 0 #for checking number of column
start_num = 0 #for cheking primary
while number < columns: #calculator for checking creating columns: it's first? second? comething else?
columnName = input('Name of column: ') #column name
columnLength = int(input('Length: ')) #column length
columnType = input('Type (int, varchar or another): ') #column type
columnCondition = bool(input('Is this column primary? (True/False): ')) #is this primary?
if start_num <= 0: #create CONST, its requiered for table with containg more than 1 columns
MAINCOLUMN = columnName
else: #if column number isn't 0(no-primary)
columnCondition = False #if something is wrong, we don't have contain 2 primary columns
try:
now = datetime.datetime.now() #time right now
if columnCondition:
start_num += 1
with connection.cursor() as cursor:
if columnName == 'id': #it's cool feature: id always needed in A_I
sql = 'CREATE TABLE `%s`(`{}` {}({}) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));'.format(columnName,
columnType,
columnLength
)
else: #or not?
sql = 'CREATE TABLE `%s`(`{}` {}({}) NOT NULL);'.format(columnName,
columnType,
columnLength
)
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: columnID: {}; columnName: {}; columnType: {}; '.format(now, number,
columnName,
columnType
)
+ 'columnLength: {}, columnCondition: {}; SQL: {}\n'.format(columnLength,
columnCondition,
sql))
cursor.execute(sql, ('{}'.format(name)))
connection.commit() #saving changes
else:
with connection.cursor() as cursor:
sql = "ALTER TABLE `%s` ADD `{}` {}({}) NOT NULL AFTER `{}`;".format(columnName,
columnType,
columnLength,
MAINCOLUMN
)
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: columnID: {}; columnName: {}; columnType: {}; '.format(now, number,
columnName,
columnType
)
+ 'columnLength: {}, columnCondition: {}; SQL: {}\n'.format(columnLength,
columnCondition,
sql))
cursor.execute(sql, ('{}'.format(name)))
connection.commit() #saving changes
except Exception as er:
print('Something wrong: ', er)
now = datetime.datetime.now() #time right now
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: columnID: {}; columnName: {}; columnType: {}; '.format(now, number,
columnName,
columnType)
+ 'columnLength: {}, columnCondition: {}; SQL: {} Error: {};\n'.format(columnLength,
columnCondition,
sql, er))
finally:
number += 1 #if something is broken, we must add that. you aren't needed in infinity cycle
if number > columns:
connection.close() #if we don't close connection, it can do something bad
break
class delete: #delete something
def __init__(self, i):
self.Item = i
def info():
print('Wanna delete something? Call this class!')
class user: #delete user
def __init__(self, n, p):
self.Name = n
self.Pass = p
def info():
print('Its funcion will delete super-user')
def params(name, password, confirm=False, save=True, why='No one can know that\n'):
if confirm: #are you confirm deleting user?
if save: #do you want to save data?
try:
with open('root.json', mode='r') as f:
data = json.load(f)
rName = data['user']
rPass = data['pass']
if name != rName: #it's a super-user. another rules. only hardcore. unauthorized access?
print('Username "{}" is not defined'.format(name))
elif password != rPass: #it's a super-user. another rules. only hardcore. unauthorized access?
print("Password isn't correct")
else: #if your info is correctly, run function
shutil.copy(r'root.json', r'root_SAVED.json') #saving file
os.remove(r'root.json') #deleting file
print('User {} succesfully deleted'.format(name))
with open('why.txt', mode='a') as f:
f.write('{}'.format(why))
except Exception as er:
print("You don't have super-user", er)
else: #if you don't want to save
try:
with open('root.json', mode='r') as f:
data = json.load(f)
rName = data['user']
rPass = data['pass']
if name != rName: #it's a super-user. another rules. only hardcore. unauthorized access?
print('Username "{}" is not matched'.format(name))
elif password != rPass: #it's a super-user. another rules. only hardcore. unauthorized access?
print("Password isn't correct")
else: #if your info is correctly, run function
os.remove(r'root.json') #deleting file
#where is saving???
print('User {} succesfully deleted'.format(name))
with open('why.txt', mode='a') as f:
f.write('{}'.format(why))
except Exception as er:
print("You don't have super-user", er)
else: #if you don't confirm - command isn't running
pass
class database: #delete database
def __init__(self, n):
self.Name = n
def info():
print('Wanna delete database? Just call this function')
def params(name, confirm=False):
connection = pymysql.connect(host=host,
user=db_user,
password=db_pass,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
) #connection with your host
now = datetime.datetime.now() #time right now
if confirm: #are you confirm your choise? do you really want to delete database?
try:
with connection.cursor() as cursor:
sql = 'DROP DATABASE `%s`;' #sql-code
cursor.execute(sql, ('{}'.format(name)))
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: SQL: {}\n'.format(now, sql))
connection.commit() #saving changes
print('Database {} succesfully deleted'.format(name))
except Exception as er:
print('Something is wrong: ', er)
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: SQL: {} Error: {}\n'.format(now, sql, er))
raise er
finally:
connection.close() #closing connection for safely exit
else:
pass
class table: #delete table
def __init__(self, n):
self.Name = n
def info():
print('Wanna delete table? Just call this function')
def params(name, confirm=False):
now = datetime.datetime.now() #time right now
connection = pymysql.connect(host=host,
user=db_user,
password=db_pass,
db=db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
) #connection with your host
if confirm: #are you confirm your choise?
try:
with connection.cursor() as cursor:
sql = 'DROP TABLE `%s`;' #sql-code
cursor.execute(sql, ('{}'.format(name)))
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: SQL {}'.format(now, sql))
connection.commit() #saving changes
print('Table {} succesfully deleted'.format(name))
except Exception as er:
print('Something is wrong: ', er)
with open('connection_log.txt', mode='a') as f:
f.write('[{}]: SQL: DROP TABLE `%s`; Error: {}\n'.format(now, er))
finally:
connection.close()
else:
pass