No Description

tp-smart-process.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. maxTokenIdx = max(tokenApp, tokenMonth, tokenDay, tokenYear, tokenTime)
  25. isFirst = True
  26. for event in fileContent:
  27. # print("evenToken:", tokenTime)
  28. # print (event[tokenTime])
  29. if len(event) > maxTokenIdx:
  30. (hour, minutes, seconds) = event[tokenTime].split(':')
  31. (year, month, day) = (int(event[tokenYear]), monthDict[event[tokenMonth]],int(event[tokenDay]))
  32. currDateTime = datetime.datetime(year, month, day, int(hour), int(minutes), int(seconds))
  33. # print(event, currDateTime, event[tokenApp])
  34. if (not isFirst):
  35. timeDelta = currDateTime - prevDateTime
  36. # print(timeDelta, prevApp)
  37. if prevApp in appDict:
  38. appDict[prevApp] = appDict[prevApp] + timeDelta.seconds
  39. else:
  40. appDict[prevApp] = 0 + timeDelta.seconds
  41. else:
  42. isFirst = False
  43. prevDateTime = currDateTime
  44. prevApp = event[tokenApp]
  45. return appDict
  46. def decryptFileAndProcess(encryptedFName):
  47. key = "This is a secret"
  48. f = open(encryptedFName, 'rb')
  49. print("Decrypting the file: %s ..." % encryptedFName)
  50. cipher = AES.new(key)
  51. decrypted = cipher.decrypt(f.read())
  52. fileContent = decrypted.decode("utf-8").split("\n")
  53. # positions of tokens in each line
  54. # print("Is this a time? ", fileContent[0][tokenTime])
  55. # print("Is this a year month day? ", fileContent[0][tokenYear], fileContent[0][tokenMonth], fileContent[0][tokenDay])
  56. # print("If not, modify the token values....")
  57. # print(fileContent)
  58. # print([line for line in fileContent if len(line) > 10])
  59. return processAppTimes([line for line in fileContent if len(line) > 10])
  60. def latestUserFile(searchDir, userID):
  61. # search_dir = "/tmp/images/"
  62. print("Searching dir %s for latest file of userID: %s ...." % (searchDir, userID))
  63. files = [f for f in glob.glob(searchDir + "/" + userID + "-*EncrypFile*") if os.path.isfile(f)]
  64. return max(files, key = lambda x: os.path.getmtime(x))
  65. '''
  66. The main program....
  67. '''
  68. if len(sys.argv) != 3:
  69. print("Usage: " + sys.argv[0] + " (directory) (UserID)")
  70. print("Example: " + sys.argv[0] + " /tmp/images 1000")
  71. sys.exit(1)
  72. searchDir = sys.argv[1]
  73. userID = sys.argv[2]
  74. (tokenApp,tokenMonth, tokenDay, tokenYear, tokenTime) = (0,3,4,7,5)
  75. results = decryptFileAndProcess( latestUserFile(searchDir,userID))
  76. print("Results for user", userID)
  77. print(results)