__init__.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. This file is designed to retrieve information on a folder
  3. {files,size,hash}
  4. """
  5. import subprocess
  6. import sys
  7. import re
  8. import os
  9. import pandas as pd
  10. import io
  11. import datetime
  12. class Util :
  13. def size(self,stream):
  14. PATTERN = '(^.+)([A-Z]+$)'
  15. value,units = re.match('^(.+)([A-Z]+$)',stream).groups()
  16. value = float(value)
  17. if 'G' == units :
  18. units = 'GB'
  19. # value *= 1000
  20. elif 'K' == units:
  21. units = 'KB'
  22. # value /= 1000
  23. else :
  24. units = 'MB'
  25. # units = 'MB'
  26. return {"size":value,"units":units}
  27. def content(self,stream):
  28. return {"content":stream.split(' ')[0].strip()}
  29. def read(**args):
  30. """
  31. The path can also take in regular expressions
  32. """
  33. cmd = {"size":"du -sh :path","content":"find :path -type f -exec md5sum {} + | sort -z|md5sum"}
  34. r = {}
  35. util = Util()
  36. for key in cmd :
  37. _cmd = cmd[key]
  38. handler = subprocess.Popen(_cmd.replace(':path',args['path']),shell=True,stdout=subprocess.PIPE,encoding='utf-8')
  39. stream = handler.communicate()[0]
  40. if sys.version_info[0] > 2 :
  41. rows = str(stream).split('\n')
  42. else:
  43. rows = stream.split('\n')
  44. if key == 'size' :
  45. rows = rows[0]
  46. rows = util.size(rows.split('\t')[0])
  47. elif key == 'content' :
  48. #
  49. # There is a hash key that is generated and should be extracted
  50. rows = rows[0]
  51. rows = util.content(rows)
  52. r = dict(r, **rows)
  53. N = 0 if not os.path.exists(args['path']) else len( os.listdir(args['path']))
  54. r['path'] = args['path']
  55. r['files']= N
  56. r['name'] = args['path'].split(os.sep)[-1:][0]
  57. r['node'] = os.uname()[1]
  58. r['date'] = datetime.datetime.now().strftime('%m-%d-%Y')
  59. r['time'] = datetime.datetime.now().strftime('%H:%M:%S')
  60. return pd.DataFrame([r])
  61. pass