-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_files.py
146 lines (134 loc) · 3.9 KB
/
scan_files.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
#-*- coding: utf-8 -*-
import glob, os, mysql.connector
import database_losysweb_rj as db
from datetime import datetime
#FUNCAO PARA SELECIONAR O PATH A PARTIR DE UMA ESCOLHA
def selectPath():
while True:
opcao = input('>>> ')
if opcao == '1':
storage_id = '2a17c8cd-58c0-11e4-84ad-fa224f896c15'
storage_path = '\\\\10.4.168.16\\StorageHR01'
elif opcao == '2':
storage_id = '4453e749-58c0-11e4-84ad-fa224f896c15'
storage_path = '\\\\10.4.168.17\\StorageHRMirror01'
elif opcao == '3':
storage_id = '70461df9-58c0-11e4-84ad-fa224f896c15'
storage_path = '\\\\10.4.168.16\\StorageHR02'
elif opcao == '4':
storage_id = '9aa4b825-58c0-11e4-84ad-fa224f896c15'
storage_path = '\\\\10.4.168.17\\StorageHRMirror02'
else:
print('Opcao invalida! Selecione de 1 a 4')
continue
return storage_id, storage_path
break
# FUNCAO PARA LISTAR OS ARQUIVOS
def fileList(p):
file_list = []
# ZERANDO O ARQUIVO
open('./file_list.txt', 'w').close()
# ABRINDO O ARQUIVO PARA LER E ESCREVER
f = open('./file_list.txt', 'a')
print('[LOADING] Carregando os arquivos e diretorios de pastas')
for root, dirs, files in os.walk(p):
for file in files:
fp = os.path.join(root, file)
if len(fp) >= 45:
file_list.append(fp)
f.write(fp + '\n')
f.close()
return file_list
# FUNCAO PARA LISTAR AS PASTAS
def folderList(p):
folder_list = []
# ZERANDO O ARQUIVO
open('./folder_list.txt', 'w').close()
# ABRINDO O ARQUIVO PARA LER E ESCREVER
f = open('./folder_list.txt', 'a')
print('[LOADING] Carregando os diretorios de pastas')
for root, dirs, files in os.walk(p):
for d in dirs:
fp = os.path.join(root, d)
if len(fp) >= 45:
fp = fp.replace(p, '')
fp = fp.replace('\\', '/')
fp = fp[1:]
f.write(fp + '\n')
folder_list.append(fp)
f.close()
return folder_list
def selectTable(uuid):
registry_list = []
conf = db.cfg()
try:
conn = mysql.connector.connect(**conf)
print('[DB] Conectado')
except Exception as e:
print('[DB] Erro ao conectar\n[CFG] Verifique as configuracoes de conexao.\n[CFG] Processo abortado!')
raise e
exit()
query = ("SELECT path FROM mam.file f \
INNER JOIN mam.file_storage fs ON \
f.uuid = fs.file_uuid \
WHERE fs.storage_uuid = '%s'" % uuid)
sql = conn.cursor()
try:
sql.execute(query)
print('[SQL] OK')
except Exception as e:
print('[SQL] Falha ao executar SQL')
conn.close()
print('[DB] Fechado')
exit()
results = sql.fetchall()
registry_list = [x[0] for x in results]
try:
conn.close()
print('[DB] Fechado')
except Exception as e:
print('[DB] Falha ao fechar o banco')
raise e
return registry_list
######## INICIO DO SCRIPT ########
print("""
#######################################
####### STORAGE CLEANER - GNEWS #######
#######################################
Selecione uma das opcoes abaixo para scannear:
1) StorageHR01 (10.4.168.16)
2) StorageHRMirror01 (10.4.168.16)
3) StorageHR02 (10.4.168.17)
4) StorageHRMirror02 (10.4.168.17)
""")
#SCRIPT EXECUTADO
start = datetime.now()
# SELECIONA O VALOR DO STORAGE_UUID E O STORAGE_NAME
storage_id, storage_path = selectPath()
# CRIA A LISTA DE ACORDO O RETORNO DO SELECT
query_list = selectTable(storage_id)
# DIFF
folders_list = folderList(storage_path)
# ZERANDO O ARQUIVO
open('./delete_list.txt', 'w').close()
# ABRINDO O ARQUIVO PARA LER E ESCREVER
f = open('./delete_list.txt', 'a')
# VARIAVEL TEMPORARIA PARA CAPTURAR OS ITENS EXISTES NO BANCO
tmp = []
# VERIFICACAO (DIFF)
for x in folders_list:
for y in query_list:
if x in str(y):
tmp.append(x)
# REMOVENDO ARQUIVOS REGISTRADO DA LISTA
[folders_list.remove(x) for x in tmp if x in folders_list]
# IMPRIMINDO ARQUIVOS NAO REGISTRADOS
for x in folders_list:
f.write(x + '\n')
print('[FILE] Arquivo delete_list.txt gerado com sucesso.')
#FIM DO SCRIPT
end = datetime.now()
diff = end - start
print('[START]:' + str(start))
print('[END]: ' + str(end))
print('[END] Processo demorou: ' + str(diff) + ' segundos.')