-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdprStatus.py
107 lines (92 loc) · 3.75 KB
/
dprStatus.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
#!/usr/bin/env python3.5
import os
import time
from datetime import datetime
"""The purpose of the script is to" +
"generate a report of when was the time a DPR was updated."""
monthDict = {"1": "Jan", "2": "Feb", "3": "Mar", "4": "Apr",
"5": "May", "6": "Jun",
"7": "Jul", "8": "Aug",
"9": "Sep", "10": "Oct",
"11": "Nov", "12": "Dec"}
todayMonth = datetime.now().month
todayMonth = monthDict[str(todayMonth)]
todayDate = str(datetime.now().day)
if len(todayDate) == 1:
todayDate = "0" + todayDate
fileList = []
timeList = []
employeeCount = 0
catchFileList = []
catchTimeList = []
path = r"Z:\DPR\2019" # Path to the DPR folder
dirs = os.listdir(path)
for folder in dirs: # For every DPR in the folder extract last modified time
folderpath = path + "\\"
if os.path.isdir(folderpath):
employeeCount += 1
files = os.listdir(folderpath)
for file in files:
if str(file)[0] != "~" and "DPR_2019" in str(file):
fileList.append(file)
filepath = folderpath + "\\" + str(file)
catchTime = time.ctime(os.stat(filepath).st_mtime)
# When the date is in single digit
if catchTime[8] == " ":
catchTime = catchTime[:8] + "0" + catchTime[9:]
catchTime = catchTime.replace(" ", ",")
timeList.append(catchTime)
day = catchTime[8:10]
month = catchTime[4:7]
if day != todayDate or month != todayMonth:
catchFileList.append(str(file))
catchTimeList.append(catchTime)
# when the date is not in single digit
elif catchTime[8] != " ":
catchTime = catchTime.replace(" ", ",")
timeList.append(catchTime)
day = catchTime[8:10]
month = catchTime[4:7]
if day != todayDate or month != todayMonth:
catchFileList.append(str(file))
catchTimeList.append(catchTime)
result = dict(zip(fileList, timeList))
keyList = result.keys()
keyList = sorted(keyList)
catchResult = dict(zip(catchFileList, catchTimeList))
catchKeyList = catchResult.keys()
catchKeyList = sorted(catchKeyList)
# Error Report
nameList = (", ".join(files)) #Create a list out of array in simple text form
if "~" or "Autosaved" in nameList:
endLine = "Some DPRs are not named properly Or May have multiple files of a user"
else:
endLine = "All DPRs are set properly"
# Path of Report file to be created
try:
csvPath = r"\\192.168.1.100\msi\DPR Status\DPR Update Status.csv"
f = open(csvPath, "w")
# Header for the file
updateTime = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
f.write("This is an auto generated DPR update report." +
"It shows when was a DPR last updated." + "\n")
f.write("This report was last generated on " + updateTime + "\n")
f.write(endLine + "\n")
f.write("Developed By : MSA R & D" + "\n")
f.write("\n")
# DPR report
for key in keyList:
line = key + "," + result[key]
f.write(line + "\n")
f.write("\n")
f.write("*******************************************************" + "\n")
f.write("\n")
f.write("An Email to be sent to following people \n")
f.write("\n")
for key in catchKeyList:
line = key + "," + catchResult[key]
f.write(line + "\n")
f.close()
print("File written successfully! Yeah!")
except Exception as e:
print("\n" + "Something is wrong! File was not written!" + "\n" + str(e))