No Description

tp-smart-process.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. '''
  2. Script for processing the log generated by the TPSmart app.
  3. Given a directory and userID will determine the latest log file, decrypt it and
  4. compute the time that was spent on each app.
  5. '''
  6. import datetime
  7. import base64
  8. import os
  9. import glob
  10. import functools
  11. import sys
  12. from Crypto.Cipher import AES
  13. monthDict = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep':9, 'Oct':10, 'Nov': 11, 'Dec' : 12}
  14. def readAndProcess(fileName):
  15. f = open(fileName, 'r')
  16. return processAppTimes(f.read().split('\n'))
  17. def processAppTimes(fileContent):
  18. # fileContent = fileContent.split('\n')
  19. fileContent = [line.split(' ') for line in fileContent if len(line.strip()) > 0]
  20. # dictionary to accumulate the seconds for each application
  21. appDict = {}
  22. # for each line in the text file, extract the tokens, compute the time diff
  23. # and accumulate in dictionary
  24. isFirst = True
  25. for event in fileContent:
  26. # print("evenToken:", tokenTime)
  27. # print (event[tokenTime])
  28. (hour, minutes, seconds) = event[tokenTime].split(':')
  29. (year, month, day) = (int(event[tokenYear]), monthDict[event[tokenMonth]],int(event[tokenDay]))
  30. currDateTime = datetime.datetime(year, month, day, int(hour), int(minutes), int(seconds))
  31. # print(event, currDateTime, event[tokenApp])
  32. if (not isFirst):
  33. timeDelta = currDateTime - prevDateTime
  34. # print(timeDelta, prevApp)
  35. if prevApp in appDict:
  36. appDict[prevApp] = appDict[prevApp] + timeDelta.seconds
  37. else:
  38. appDict[prevApp] = 0 + timeDelta.seconds
  39. else:
  40. isFirst = False
  41. prevDateTime = currDateTime
  42. prevApp = event[tokenApp]
  43. return appDict
  44. def decryptFileAndProcess(encryptedFName):
  45. key = "This is a secret"
  46. f = open(encryptedFName, 'rb')
  47. print("Decrypting the file: %s ..." % encryptedFName)
  48. cipher = AES.new(key)
  49. decrypted = cipher.decrypt(f.read())
  50. fileContent = decrypted.decode("utf-8").split("\n")
  51. # positions of tokens in each line
  52. # print("Is this a time? ", fileContent[0][tokenTime])
  53. # print("Is this a year month day? ", fileContent[0][tokenYear], fileContent[0][tokenMonth], fileContent[0][tokenDay])
  54. # print("If not, modify the token values....")
  55. # print(fileContent)
  56. # print([line for line in fileContent if len(line) > 10])
  57. return processAppTimes([line for line in fileContent if len(line) > 10])
  58. def latestUserFile(searchDir, userID):
  59. # search_dir = "/tmp/images/"
  60. print("Searching dir %s for latest file of userID: %s ...." % (searchDir, userID))
  61. files = [f for f in glob.glob(searchDir + "/" + userID + "-*") if os.path.isfile(f)]
  62. return max(files, key = lambda x: os.path.getmtime(x))
  63. '''
  64. The main program....
  65. '''
  66. if len(sys.argv) != 3:
  67. print("Usage: " + sys.argv[0] + " (directory) (UserID)")
  68. print("Example: " + sys.argv[0] + " /tmp/images 1000")
  69. sys.exit(1)
  70. searchDir = sys.argv[1]
  71. userID = sys.argv[2]
  72. (tokenApp,tokenMonth, tokenDay, tokenYear, tokenTime) = (0,3,4,7,5)
  73. results = decryptFileAndProcess( latestUserFile(searchDir,userID))
  74. print("Results for user", userID)
  75. print(results)