Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. import os
  2. from datetime import datetime
  3. import tabulate
  4. from ast import literal_eval
  5. import logging
  6. import hashlib
  7.  
  8. logger = logging.getLogger('File System')
  9.  
  10. allfilesystem_list = []
  11.  
  12. # print tabel naar scherm
  13. def show_list(listname):
  14. try:
  15. header = listname[0].keys()
  16. rows = [x.values() for x in listname]
  17. tablefilesystem = tabulate.tabulate(rows, header, tablefmt='rst')
  18. logger.info('creating table of allfilesystem_list')
  19. print(tablefilesystem)
  20. logger.info('printed table to the screen')
  21. return tablefilesystem
  22.  
  23. #voor als de invoer van het pad niet juist is
  24. except IndexError:
  25. logger.error('The input is incorrect')
  26. print('The input is incorrect, restart the program and try again (example: C:\\...)')
  27.  
  28. #Voor als het gekozen pad te groot is om te analyseren
  29. except MemoryError:
  30. logger.error('The path size is too big')
  31. print('The path size is too big, restart the program and try a different of the path')
  32.  
  33.  
  34. # schrijf tabel weg naar bestand
  35. def save_list(listname):
  36. try:
  37. f = open('Filesystem.txt', 'w', encoding="utf-8")
  38. logger.info('open file: Filesystem.txt')
  39. f.write(listname)
  40. logger.info('writing results to file')
  41. f.close()
  42. logger.info('close file: Filesystem.txt')
  43.  
  44. # locatie van de file
  45. print('\nThe results are saved into a file called Filesystem.txt on the location .\Filesystem.txt')
  46. except TypeError:
  47. print('')
  48.  
  49. hasher = hashlib.md5()
  50. with open('Filesystem.txt', 'rb') as afile:
  51. buf = afile.read()
  52. hasher.update(buf)
  53. hash1 = 'Filesystem.txt MD5 Hashwaarde: ' + hasher.hexdigest()
  54. logger.debug('Generating MD5 hash: ' + hasher.hexdigest())
  55.  
  56. hashersha = hashlib.sha256()
  57. with open('Filesystem.txt', 'rb') as afile:
  58. buf = afile.read()
  59. hashersha.update(buf)
  60. hash2 = 'Filesystem.txt SHA256 Hashwaarde: ' + hashersha.hexdigest()
  61. logger.debug('Generating SHA256 hash: ' + hashersha.hexdigest())
  62.  
  63. f = open('hashfile.txt', 'a', encoding="utf-8")
  64. logger.info('open file: hashfile.txt')
  65. f.write(hash1 + '\n' + hash2 + '\n')
  66. logger.info('writing md5 hash to file')
  67. f.close()
  68. logger.info('close file: hashfile.txt')
  69.  
  70.  
  71. #om filesystem langs te lopen
  72. def analysefilesystem(pathname):
  73.  
  74. for (dirpath, dirnames, filenames) in os.walk(pathname):
  75. for f in filenames:
  76. try:
  77. path = os.path.join(dirpath, f)
  78. file_stats = os.stat(path)
  79. lastaccess = file_stats.st_atime
  80. lastmodified = file_stats.st_mtime
  81. creationtime = file_stats.st_ctime
  82.  
  83. #om de datetime te veranderen naar een string
  84. last_access = datetime.fromtimestamp(lastaccess)
  85. last_access = last_access.strftime('%d/%m/%Y %H:%M:%S')
  86. last_modified = datetime.fromtimestamp(lastmodified)
  87. last_modified = last_modified.strftime('%d/%m/%Y %H:%M:%S')
  88. creation_time = datetime.fromtimestamp(creationtime)
  89. creation_time = creation_time.strftime('%d/%m/%Y %H:%M:%S')
  90. file_size = file_stats.st_size
  91. logger.debug('Analyzing file system at the location: ' + dirpath)
  92. logger.debug('Analyzing file stats: ' + f)
  93. logger.info('Append file stats to allfilesystem_list')
  94. allfilesystem_list.append({'File path': dirpath, 'File name': f, 'File size (bytes)': file_size, 'last access': last_access, 'Last modified': last_modified, 'Creation time': creation_time})
  95.  
  96. #voor als een file niet te vinden is, dat het programma dan die overslaat en verder gaat.
  97. except OSError:
  98. logger.warning('File not found: ' + f)
  99.  
  100. return allfilesystem_list
  101.  
  102. def filterfiles(listname, size1, size2, name1, name2, path1, path2):
  103. filteredlist = []
  104.  
  105. #filteroptie op File size
  106. if size1.upper() == 'Y':
  107. size2 = int(size2)
  108. sizelist = filter(lambda x: x['File size (bytes)'] == size2, listname)
  109. logger.info('filtering list')
  110. sizelist2 = list(sizelist)
  111. for item in sizelist2:
  112. sizestr = str(item)
  113. sizestr2 = sizestr.replace('[', '')
  114. sizestr3 = sizestr2.replace(']', '')
  115. if sizestr3 == '':
  116. logger.info('No results found on file size: ' + size1)
  117. else:
  118. sizelistdict = literal_eval(sizestr3)
  119. filteredlist.append(sizelistdict)
  120. logger.info('found results')
  121. logger.info('append filtered files to allfilesystem_list')
  122.  
  123. # filteroptie op File path
  124. if path1.upper() == 'Y':
  125. pathlist = filter(lambda x: x['File path'] == path2, listname)
  126. logger.info('filtering list')
  127. pathlist2 = list(pathlist)
  128. for item in pathlist2:
  129. pathstr = str(item)
  130. pathstr2 = pathstr.replace('[', '')
  131. pathstr3 = pathstr2.replace(']', '')
  132. if pathstr3 == '':
  133. logger.info('No results found on file path: ' + path1)
  134. else:
  135. pathlistdict = literal_eval(pathstr3)
  136. filteredlist.append(pathlistdict)
  137. logger.info('found results')
  138. logger.info('append filtered files to allfilesystem_list')
  139.  
  140. #filteroptie op File name
  141. if name1.upper() == 'Y':
  142. namelist = filter(lambda x: x['File name'] == name2, listname)
  143. logger.info('filtering list')
  144. namelist2 = list(namelist)
  145. for item in namelist2:
  146. namestr = str(item)
  147. namestr2 = namestr.replace('[', '')
  148. namestr3 = namestr2.replace(']', '')
  149. if namestr3 == '':
  150. logger.info('No results found on file name: ' + name1)
  151. else:
  152. namelistdict = literal_eval(namestr3)
  153. filteredlist.append(namelistdict)
  154. logger.info('found results')
  155. logger.info('append filtered files to allfilesystem_list')
  156.  
  157. if filteredlist == []:
  158. print('No results found')
  159. return filteredlist
  160.  
  161. def main(pathname, filter1, filtersize, sizef, filtername, namef, filterpath, pathf, save):
  162. logger.info('Started')
  163. filesystem_list = analysefilesystem(pathname)
  164.  
  165. if filter1.upper() == 'Y':
  166. filesystem_list = filterfiles(filesystem_list, filtersize, sizef, filtername, namef, filterpath, pathf)
  167.  
  168. tablelist = show_list(filesystem_list)
  169.  
  170. if save.upper() == 'Y':
  171. save_list(tablelist)
  172. logger.info('Finished')
  173.  
  174. if __name__ == '__main__':
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement