OpenFiles/fileCsv2Json.py

60 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python
import csv
import json
skip_list = [
'D:\\home',
'D:\\shares\\hr',
'D:\\shares\\payroll',
'D:\\shares\\cis'
]
share_names = {
'D:\\shares\\acad': ['acad', 'V:\\'],
'D:\\shares\\common': ['common', 'X:\\'],
'D:\\shares\\commercial': ['commercial', 'N:\\'],
'D:\\shares\\scans': ['scans', 'J:\\'],
'D:\\shares\\costing': ['costing', 'K:\\'],
'D:\\shares\\marketing': ['marketing', 'M:\\'],
'D:\\shares\\almar': ['almar', 'O:\\'],
'D:\\shares\\production': ['production', 'P:\\'],
'D:\\shares\\recreation': ['recreation', 'R:\\'],
'D:\\shares\\photos': ['photos', 'U:\\'],
}
f = open("/tmp/dhcp.csv")
csv_f = csv.reader(f)
x = csv_f.next()
dhcp_list = {}
for row in csv_f:
dhcp_list[row[0]] = row[8][:-20].upper()
f = open('/tmp/files.csv')
csv_f = csv.reader(f)
x = csv_f.next()
file_list = []
for row in csv_f:
item = {}
if any(share in row[8] for share in skip_list):
continue
if row[8][-1:] == '\\':
continue
item['user'] = row[2][11:].lower()
item['computer'] = dhcp_list.get(row[1], 'Unknown')
if len(row[12]) > 0:
item['share'] = share_names[row[8][:len(row[8]) - len(row[12]) - 1]][0]
item['file'] = (share_names[row[8][:len(row[8]) -
len(row[12]) - 1]][1] +
row[12])
else:
item['share'] = share_names[row[8]][0]
item['file'] = share_names[row[8]][1]
file_list.append(item)
result = {'count': len(file_list), 'data': file_list}
with open('/var/www/openfiles/files.json', 'w') as f:
json.dump(result, f)