Repository navigation
Expand file tree
/
Copy pathProjectTime.py
More file actions
executable file
·370 lines (301 loc) · 11.2 KB
/
Copy pathProjectTime.py
File metadata and controls
executable file
·370 lines (301 loc) · 11.2 KB
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
#!/usr/bin/python
# coding: ISO-8859-15
"""
Ermittelt anhand der timestamp von Verz. und Dateien die Arbeitszeit
an einem Projekt.
http://www.jensdiemer.de
license = GNU General Public License v3 or above
en -> http://www.opensource.org/licenses/gpl-license.php
de -> http://jensdiemer.de/Programmieren/GPL-License
"""
__version__ = "v0.1"
__history__ = """
v0.1
- erste Version
"""
import os, datetime, math
weekDaysMap = {
"Mon": "MO",
"Tue": "DI",
"Wed": "MI",
"Thu": "DO",
"Fri": "FR",
"Sat": "SA",
"Sun": "SO"
}
class TimeStore(dict):
def add(self, time, filePath):
if not self.has_key(time):
self[time] = []
self[time].append(filePath)
class Chronologer:
def __init__(self,
projectPath, # Pfad in dem die Projectdateinen liegen
hourlyRate, # Stundenlohn
maxTimeDiff=60 * 60, # max Zeit, die ein Arbeitsblock trennt
minBlockTime=30 * 60, # kleinste Zeiteinheit, für einen Block
# Erster und letzter Tag, andere Zeiten werden ignoriert
projectStart=datetime.datetime(year=1977, month=1, day=1),
projectEnd=datetime.datetime(year=2200, month=12, day=31),
exclude_dirs=None,
debug=False,
):
self.projectPath = projectPath
self.maxTimeDiff = maxTimeDiff
self.minBlockTime = minBlockTime
self.hourlyRate = hourlyRate
self.projectStart = projectStart
self.projectEnd = projectEnd
self.exclude_dirs = exclude_dirs or []
self.debug = debug
self.statistics = {}
self.dayData = {}
self.timeStore = TimeStore()
print "read dir...",
count = self.readDir(self.projectPath)
print "OK (%s items)" % count
print "calc blocks...",
timeBlocks = self.makeBlocks()
print "OK"
print "analyseBlocks...",
self.analyseBlocks(timeBlocks)
print "OK"
def readDir(self, path):
""" Einlesen der Verzeichnis Informationen """
def exclude_dir(dir):
dir = "\\%s\\" % dir.strip("\\")
for exclude_dir in self.exclude_dirs:
exclude_dir = "\\%s\\" % exclude_dir.strip("\\")
#~ print dir, exclude_dir
#~ if dir.startswith(exclude_dir):
if exclude_dir in dir:
return True
return False
count = 0
if not os.path.isdir(path):
raise SystemError("Path '%s' not found!" % path)
for root, dirs, files in os.walk(path):
if exclude_dir(root):
if self.debug:
print "Skip dir %r..." % root
continue
for dir in dirs:
count += 1
self.statFile(os.path.join(root, dir))
for fileName in files:
count += 1
self.statFile(os.path.join(root, fileName))
return count
def statFile(self, path):
""" Zeiten für Verz.Eintrag festhalten """
try:
pathStat = os.stat(path)
except Exception, err:
print "Error getting stat for %r: %s" % (path, err)
return
creationTime = pathStat.st_ctime
lastAccess = pathStat.st_atime
lastModification = pathStat.st_mtime
if self.debug:
print "File %r:" % path
print "creation time...: %r" % datetime.datetime.fromtimestamp(creationTime)
print "last access time: %r" % datetime.datetime.fromtimestamp(lastAccess)
print "last modify time: %r" % datetime.datetime.fromtimestamp(lastModification)
self.timeStore.add(creationTime, path)
self.timeStore.add(lastAccess, path)
self.timeStore.add(lastModification, path)
def makeBlocks(self):
""" Zusammenhängende Arbeitszeiten ermitteln """
timeList = self.timeStore.keys()
timeList.sort()
# Ende-Datum soll inkl. des angegebenen Tags sein
projectEnd = self.projectEnd + datetime.timedelta(days=1)
timeBlocks = []
lastTime = 0
for t in timeList:
d = datetime.datetime.fromtimestamp(t)
if d < self.projectStart: continue
if d > projectEnd: continue
if (t - lastTime) > self.maxTimeDiff:
# Ein neuer Block, weil die Differenz zu gro� ist
timeBlocks.append([])
timeBlocks[-1].append(t)
lastTime = t
return timeBlocks
def blockTime(self, block):
""" Liefert die Zeit eines Blocks zur�ck """
if len(block) == 1:
return self.minBlockTime
workTime = block[-1] - block[0]
if workTime < self.minBlockTime:
return self.minBlockTime
return workTime
def analyseBlocks(self, timeBlocks):
"""
Ermittelt gesammt Arbeitszeit und kalkuliert die Zeit für
jeden Arbeits-Block
"""
self.statistics["totalTime"] = 0
days = {}
for block in timeBlocks:
blockTime = self.blockTime(block)
self.statistics["totalTime"] += blockTime
d = datetime.datetime.fromtimestamp(block[0])
key = d.strftime("%y%m%d")
if self.dayData.has_key(key):
self.dayData[key]["blockTime"] += blockTime
self.dayData[key]["block"].append(block)
else:
self.dayData[key] = {
"dayName" : weekDaysMap[d.strftime("%a")],
"week" : d.strftime("%W"),
"dateStr" : d.strftime("%d.%m.%Y"),
"blockTime" : blockTime,
"block" : [block],
}
def displayBlock(self, dayBlock, verbose):
"""
Anzeige eines zusammenhängenden Blockes
"""
for coherentBlocks in dayBlock:
dirInfo = {}
for timestamp in coherentBlocks:
fileList = self.timeStore[timestamp]
for path in fileList:
dir, file = os.path.split(path)
if not dirInfo.has_key(dir):
dirInfo[dir] = []
dirInfo[dir].append(file)
dirs = dirInfo.keys()
dirs.sort()
for dirName in dirs:
shortDirName = ".%s" % dirName[len(self.projectPath):]
if verbose == 1:
print "\t%3s files in %s" % (
len(dirInfo[dirName]), shortDirName
)
else:
print "\t%s" % shortDirName
for fileName in dirInfo[dirName]:
print "\t\t%s" % fileName
print
def displayResults(self, verbose=0):
"""
Anzeigen der Ergebnisse
verbose = 0 -> normale Ansicht, nur Tag und Std.-Anzahl
verbode = 1 -> Anzahl der geänderten Dateien
verbode = 2 -> Auflistung der geänderten Dateien
"""
totalTime = self.statistics["totalTime"]
print
print ">display Results"
print
print "totalTime: %sSec -> %.2fStd -> %.2f 8-Std-Tage" % (
totalTime, totalTime / 60.0 / 60.0,
totalTime / 60.0 / 60.0 / 8.0
)
keys = self.dayData.keys()
keys.sort()
print "-" * 40
lastWeek = 0
totalStd = 0
for i, key in enumerate(keys):
dayName = self.dayData[key]["dayName"]
week = self.dayData[key]["week"]
dateStr = self.dayData[key]["dateStr"]
blockTime = self.dayData[key]["blockTime"]
block = self.dayData[key]["block"]
if week > lastWeek: # Leerzeile zwischen den Wochen
print
print "Woche: %s" % week
lastWeek = week
blockTime = int(math.ceil(blockTime / 60.0 / 60.0)) # Aufrunden
print "%4s - %s, %s:%3i Std ->%4s€" % (
i + 1, dayName, dateStr, blockTime, blockTime * self.hourlyRate
)
totalStd += blockTime
if verbose:
self.displayBlock(block, verbose)
print "_" * 40
totalCosts = totalStd * self.hourlyRate
print " %s Std * %s€ = %s€" % (totalStd, self.hourlyRate, totalCosts)
def displayPushedResults(self, exchangeRatio,
displayMoneyOnly=False, maxDayTime=14):
"""
Rechnet die Anzahl der Stunden pro Tag so um, das unterm Strich
ungefähr der gleiche Betrag, trotz eines anderen Stundenlohns
herrauskommt.
maxDayTime - Max. Arbeitszeit pro Tag, überzählige Stunden werden auf
den nächten Tag "verschoben"
"""
print
print "-" * 40
print ">PushedResults (exchangeRatio: %s, maxDayTime: %s)" % (
exchangeRatio, maxDayTime
)
keys = self.dayData.keys()
keys.sort()
lastWeek = 0
totalStd = 0
overhang = 0
for i, key in enumerate(keys):
dayName = self.dayData[key]["dayName"]
week = self.dayData[key]["week"]
dateStr = self.dayData[key]["dateStr"]
blockTime = self.dayData[key]["blockTime"]
block = self.dayData[key]["block"]
if week > lastWeek: # Leerzeile zwischen den Wochen
print
print "Woche: %s" % week
lastWeek = week
blockTime = blockTime * self.hourlyRate / exchangeRatio
pushedTime = int(math.ceil(blockTime / 60.0 / 60.0))
# T�gliche Abeitszeit begrenzen
pushedTime += overhang
if pushedTime > maxDayTime:
overhang = pushedTime - maxDayTime
pushedTime = maxDayTime
else:
overhang = 0
if displayMoneyOnly:
print "%3s - %s, %s:%4i€" % (i + 1, dayName, dateStr, pushedTime * exchangeRatio)
else:
print "%3s - %s, %s:%3i Std" % (i + 1, dayName, dateStr, pushedTime)
totalStd += pushedTime
if overhang > 0:
print "-> rest overhang: %sStd !!!" % overhang
print "_" * 40
totalCosts = totalStd * exchangeRatio
if displayMoneyOnly:
print "%5s€" % totalCosts
else:
print " %s Std * %s€ = %s€" % (totalStd, exchangeRatio, totalCosts)
if __name__ == "__main__":
#~ c = Chronologer(
#~ projectPath = r".",
#~ hourlyRate = 80,
#~ ).displayResults()
c = Chronologer(
projectPath=r".",
hourlyRate=80,
maxTimeDiff=60 * 60,
minBlockTime=55 * 60,
projectStart=datetime.datetime(year=2005, month=3, day=1),
projectEnd=datetime.datetime(year=2007, month=3, day=31),
)
print "\n" * 3, "="*79
print "(verbose: 0)"
c.displayResults()
print "\n" * 3, "="*79
print "(verbose: 1)"
c.displayResults(verbose=1)
print "\n" * 3, "="*79
print "(verbose: 2)"
c.displayResults(verbose=2)
print "\n" * 3, "="*79
# Umrechnung auf anderen Stundenlohn
c.displayPushedResults(exchangeRatio=35)
print "\n" * 3, "="*79
c.displayPushedResults(exchangeRatio=30)
print "\n" * 3, "="*79
c.displayPushedResults(exchangeRatio=35, displayMoneyOnly=True)