-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_db.py
484 lines (380 loc) · 14.1 KB
/
update_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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import os
import pymysql.cursors
import re
import json
import glob
repos_home = '/home/roman/repos/Modules/'
connection = pymysql.connect(host='localhost',
user='root',
password='root',
db='MLAB',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
cursorobj = connection.cursor()
'''
# toto projde vsechny slozky a pokud obsahuje prj info, tak to zapise do DB ...
for top, dirs, files in os.walk(repos_home):
if "PrjInfo.txt" in files:
module = os.path.basename(top)
root = top[26:]
#print os.path.basename(top), top[26:]
print module, root
print cursorobj.execute("UPDATE `MLAB`.`Modules` SET `root` = '%s', WHERE `name` = '%s';"
%(root, module))
print cursorobj.execute("INSERT INTO `MLAB`.`Modules` (`name`, `root`, `longname_cs`, `longname_en`, `status`) VALUES ('%s', '%s', '%s', '%s', 2) ON DUPLICATE KEY UPDATE `name` = '%s';" %(module, root, os.path.basename(top), os.path.basename(top), module))
connection.commit()
'''
'''
# toto projde Moduly v db a priradi je to k zakladnim kategoriim
cursorobj.execute("SELECT * FROM `MLAB`.`Modules`;")
result = cursorobj.fetchall()
for module in result:
print module['id'], module['name'], module['root'].split('/')[0]
try:
cursorobj.execute("""
INSERT INTO `MLAB`.`module_to_category` (`module`, `category`)
VALUES
(
(SELECT Modules.id as 'mod_id' FROM MLAB.Modules WHERE Modules.name = '%s'),
(SELECT Categories.id as 'cat_id' FROM MLAB.Categories WHERE Categories.default_root = '%s')
);
""" %(module['name'], module['root'].split('/')[0]))
connection.commit()
except Exception as e:
print repr(e)
'''
'''
#toto projde PrjInfo.txt soubory a ziska zakladni popisy modulu
for top, dirs, files in os.walk(repos_home):
if "PrjInfo.txt" in files:
module = os.path.basename(top)
file = top+"/PrjInfo.txt"
data = open(file, 'r').read()
#print os.path.basename(top), top[26:]
print module
print ""
short_cs = None
short_en = None
long_en = None
long_cs = None
ust = None
wiki = None
try:
zacatek = data.find('[InfoShortDescription.en]')+27
if zacatek > 26:
konec = data[zacatek:].find('[')+zacatek
short_en = data[zacatek:konec].replace("\r","").replace("\n","").replace(" "," ").replace(" "," ")
print short_en
except Exception as e:
print "neni '[InfoShortDescription.en]"
try:
zacatek = data.find('[InfoShortDescription.cs]')+27
if zacatek > 26:
konec = data[zacatek:].find('[')+zacatek
short_cs = data[zacatek:konec].replace("\r","").replace("\n","").replace(" "," ").replace(" "," ")
print short_cs
except Exception as e:
print "neni '[InfoShortDescription.cs]"
try:
zacatek = data.find('[InfoLongDescription.en]')+25
if zacatek > 24:
konec = data[zacatek:].find('[')+zacatek
long_en = data[zacatek:konec].replace("\r","").replace("\n","").replace(" "," ").replace(" "," ")
print long_en
except Exception as e:
print "neni '[InfoLongDescription.en]"
try:
zacatek = data.find('[InfoLongDescription.cs]')+25
if zacatek > 24:
konec = data[zacatek:].find('[')+zacatek
out = data[zacatek:konec]
long_cs = data[zacatek:konec].replace("\r","").replace("\n","").replace(" "," ").replace(" "," ")
print long_cs
except Exception as e:
print "neni '[InfoLongtDescription.cs]"
try:
zacatek = data.find('[InfoBuyUST]')+13
if zacatek > 12:
konec = data[zacatek:].find('[')+zacatek
ust = data[zacatek:konec]
print data[zacatek:konec]
except Exception as e:
print "neni '[ust]"
try:
zacatek = data.find('[WIKI]')+7
if zacatek > 6:
konec = data[zacatek:].find('[')+zacatek
wiki = data[zacatek:konec]
print data[zacatek:konec]
except Exception as e:
print "neni '[wiki]"
try:
cursorobj.execute("UPDATE `MLAB`.`Modules` SET `longname_cs`='%s', `longname_en`='%s', `short_cs`='%s', `short_en`='%s', `ust`='%s' WHERE `name` = '%s';"
%(short_cs, short_en, long_cs, long_en, ust, module))
#cursorobj.execute("INSERT INTO `MLAB`.`Modules` (`name`, `root`, `longname_cs`, `longname_en`) VALUES ('%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `name` = '%s';" %(os.path.basename(top), top[26:], os.path.basename(top), os.path.basename(top), module))
connection.commit()
except:
pass
connection.close()
'''
'''
for top, dirs, files in os.walk(repos_home):
#if 'jpg' in files.lower() or 'png' in files.lower():
# if files != files.lower():
# print files
#print top
for file in files:
#print ">>", file
if 'module.json' in file:
print os.path.join(top,file), ">>",
print os.path.join(top, os.path.basename(top)+'.json')
os.rename(os.path.join(top,file), os.path.join(top, os.path.basename(top)+'.json') )
'''
#
# prejmenovat slozky (udelat z nich mala pismena)
#
#
dic ={
'PCB_SCH': 'pcb_sch',
'pcb_sch': 'sch_pcb',
'PCB': 'pcb',
'CAM_DOC': 'cam_doc',
'DOC': 'doc',
'CAM': 'cam',
'DATA': 'data',
'CAD': 'cad',
'Python': 'python',
'LIB': 'lib',
'MICROCHIP': 'microchip',
'Arduino': 'Aaduino',
'Text_Prog': 'text_prog',
'Komponenty_upravene': 'komponenty_upravene',
'Doc': 'doc',
'PDF': 'pdf',
'LINUX': 'linux',
'Wiring': 'wiring',
'Utilities': 'utilities',
'Boxes': 'boxes',
'Src': 'src',
'SRC': 'src',
'HTML': 'html',
'FZP': 'fzp',
'Map': 'map',
'Libraries': 'libraries',
'GAL': 'gal',
'ARM': 'arm',
'CCS': 'ccs',
'Debug': 'debug',
'Files': 'files',
'PIN': 'pin',
'PIC': 'pic',
'FLIR': 'flir',
'Project': 'project',
'PragoBoard': 'prago_board',
'CAM_AMA': 'cam_ama',
'SCH_PCB': 'sch_pcb',
'SCH': 'sch',
'AMF': 'amf',
'CAM_PROFI': 'cam_profi',
'AVR': 'avr',
'WIN': 'win',
'SW': 'sw'
}
folders = {}
for top, dirs, files in os.walk(repos_home):
if not '.git' in top:
#print top
folder_parent, folder = os.path.split(top)
folders[folder] = folder
if folder in dic:
print top, " >> ", os.path.join(folder_parent, dic[folder])
os.rename(top, os.path.join(folder_parent, dic[folder]))
#print folders
'''
#
# Prejmenuje nazvy souboru napr LTS01A_Big_Top.JPG na LTS01A_big_top.jpg
#
#
def keymap_replace(
string,
mappings,
lower_keys=False,
lower_values=False,
lower_string=False,
):
"""Replace parts of a string based on a dictionary.
This function takes a string a dictionary of
replacement mappings. For example, if I supplied
the string "Hello world.", and the mappings
{"H": "J", ".": "!"}, it would return "Jello world!".
Keyword arguments:
string -- The string to replace characters in.
mappings -- A dictionary of replacement mappings.
lower_keys -- Whether or not to lower the keys in mappings.
lower_values -- Whether or not to lower the values in mappings.
lower_string -- Whether or not to lower the input string.
"""
replaced_string = string.lower() if lower_string else string
for character, replacement in mappings.items():
replaced_string = replaced_string.replace(
character.lower() if lower_keys else character,
replacement.lower() if lower_values else replacement
)
return replaced_string
dic ={
'Top': 'top',
'Small': 'small',
'TOP': 'top',
'SMALL': 'small',
'Big': 'big',
'BIG': 'big',
'Bottom': 'bottom',
'BOTTOM': 'bottom',
'JPG': 'jpg',
'SVG': 'svg',
'PNG': 'png',
'PDF': 'pdf',
'TXT': 'txt'
}
for top, dirs, files in os.walk(repos_home):
if not '.git' in top:
#print top
for file in files:
print "\t ", os.path.join(top,file), ">>", os.path.join(top,keymap_replace(file, dic))
os.rename(os.path.join(top,file), os.path.join(top,keymap_replace(file, dic)))
'''
'''
#
# zmeni umisteni obrazku z /doc/src/img/ do /doc/img/
#
#
for top, dirs, files in os.walk(repos_home):
if not '.git' in top:
#print top
if 'src/img' in top:
#print top, ">>",
directory = top.replace('/doc/src/img', '/doc/img')
if not os.path.exists(directory): os.makedirs(directory)
for file in files:
print os.path.join(top,file), ">>", os.path.join(directory, file)
print file
os.rename(os.path.join(top,file), os.path.join(top.replace('/doc/src/img', '/doc/img'),file))
if len(files) == 0:
print "odstranim", top
try:
os.rmdir(top)
except Exception as e:
print e
'''
#
# zmeni umisteni slozek z /sch_pcb /cam_ama, /cam_profi, /cam_doc do /hw/*/
#
#
for top, dirs, files in os.walk(repos_home):
if not '.git' in top:
#print top
if ('/cam_profi' in top or '/sch_pcb' in top or '/cam_ama' in top or '/cam_doc' in top) and not 'hw' in top:
print top, ">>",
directory = top
if 'sch_pcb' in top:
directory = top.replace('/sch_pcb', '/hw/sch_pcb')
elif 'cam_profi' in top:
directory = top.replace('/cam_profi', '/hw/cam_profi')
elif 'cam_ama' in top:
directory = top.replace('/cam_ama', '/hw/cam_ama')
elif 'cam_doc' in top:
directory = top.replace('/cam_doc', '/hw/cam_doc')
if not os.path.exists(directory): os.makedirs(directory)
print directory
for file in files:
print os.path.join(top,file), ">>", os.path.join(directory, file)
print file
os.rename(os.path.join(top,file), os.path.join(directory,file))
if len(files) == 0:
print "odstranim", top
try:
os.rmdir(top)
pass
except Exception as e:
print e
for top, dirs, files in os.walk(repos_home):
if not '.git' in top:
#print top
if ('/pdf' in top ) and not 'doc' in top:
print top, ">>",
directory = top
if '/pdf' in top:
directory = top.replace('/pdf', '/doc/pdf')
if not os.path.exists(directory): os.makedirs(directory)
print directory
for file in files:
print os.path.join(top,file), ">>", os.path.join(directory, file)
print file
os.rename(os.path.join(top,file), os.path.join(directory,file))
if len(files) == 0:
print "odstranim", top
try:
os.rmdir(top)
pass
except Exception as e:
print e
for top, dirs, files in os.walk(repos_home):
if not '.git' in top:
for file in files:
if 'module.en.html' in file or 'module.cs.html' in file or 'module.html' in file:
print top, file
os.remove(os.path.join(top, file))
elif 'module' in file:
print top, file, files
err_list = []
cursorobj.execute("SELECT * FROM MLAB.Modules")
result = cursorobj.fetchall()
for module in result:
print ""
print module['name']
project_file = os.path.join(repos_home, module['root'], module['name']+'.json')
project_file_alternative = os.path.join(repos_home, module['root'], 'module.json')
images = glob.glob(os.path.join(repos_home, module['root'],"doc/img/*"))
image = None
for img in images:
if 'top_big' in img:
image = img.replace(os.path.join(repos_home, module['root']), "")
elif 'QRcode' in img and not image:
image = img.replace(os.path.join(repos_home, module['root']), "")
print image
if image:
print cursorobj.execute("UPDATE `MLAB`.`Modules` SET `image` = '%s' WHERE `name` = '%s';" %(image, module['name']))
if os.path.exists(project_file) and os.path.isfile(project_file):
print "FILE EXISTS"
with open(project_file) as data_file:
project_param = json.load(data_file)
porkacovat = True
elif os.path.exists(project_file_alternative) and os.path.isfile(project_file_alternative):
print "FILE EXISTS ALTERNATIVE"
os.rename(project_file_alternative, project_file)
with open(project_file) as data_file:
project_param = json.load(data_file)
porkacovat = True
else:
print "MUSIM SOUBOR VYTVORIT"
err_list.append(module['name'])
porkacovat = False
project_param = {}
#if porkacovat:
project_param['wiki'] = module['wiki']
project_param['image'] = module['image']
project_param['status'] = module['status']
project_param['ust'] = module['ust']
project_param['root'] = module['root']
project_param['name'] = module['name']
project_param['short_en'] = module['short_en']
project_param['short_cs'] = module['short_cs']
project_param['longname_en'] = module['longname_en']
project_param['longname_cs'] = module['longname_cs']
project_param['doc_en'] = module['doc_en']
project_param['doc_cs'] = module['doc_cs']
print project_file
with open(project_file, 'w') as outfile:
json.dump(project_param, outfile, indent=4)
print err_list
connection.commit()