helper.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #This file has the helper functions for automation.
  2. from conf import Config
  3. import os,io,time
  4. import shutil
  5. from logger import log
  6. import sys
  7. #Initialization Class for Automation
  8. class Automate:
  9. #Initialize logging
  10. log = log()
  11. #Helper function to initialize configuration parameters
  12. def readConfig(self):
  13. self.__configuration__ = Config()
  14. status = self.__configuration__.read_config()
  15. if status == 1:
  16. log.logger.info("Configuration read success")
  17. else:
  18. log.logger.info("Configuration read not successful! Exiting automation script")
  19. sys.exit()
  20. #Helper function to get the files from a directory
  21. def GetFiles(self, paths):
  22. files = []
  23. for file in os.listdir(paths):
  24. files.append(file)
  25. return files
  26. #Helper function to copy files from a src dir to dest dir
  27. def copyFiles(self, src_folder,dest_folder,added):
  28. if src_folder == "" or dest_folder == "":
  29. log.logger.error('Source or Destination Folder does not exists')
  30. return 0
  31. if not os.listdir(src_folder):
  32. log.logger.error('No files found in src folder')
  33. return 0
  34. #for file in os.listdir(src_folder):
  35. for file in added:
  36. # print ("File being moved -----> " + file)
  37. full_file_name = os.path.join( src_folder, file )
  38. if os.path.isfile(full_file_name):
  39. try:
  40. shutil.copy( full_file_name, dest_folder )
  41. except shutil.Error as e:
  42. log.logger.error('Could not copy the files %s',e)
  43. return 0
  44. return 1
  45. #Helper function to rename the rendered trec file embedded HTML to index.html
  46. def renameFile(self,path,tochangefilename):
  47. fileName = os.listdir(path)
  48. for files in fileName:
  49. if(files == tochangefilename):
  50. os.rename(path+"\\"+tochangefilename, path+"\\"+"index.html")
  51. return 1
  52. return 0
  53. #Helper function to calculate wait time for rendering based on File Size
  54. def renderTime(self, file, renderingPerMB):
  55. st = os.stat(file)
  56. sizeBytes = st.st_size
  57. sizeBytes /= (1024 * 1024)
  58. renderTime = sizeBytes * renderingPerMB
  59. renderTime /= 1000
  60. return renderTime
  61. #Helper function Check is a duplicate exists and delete the duplicate folder
  62. def check_duplicate(self,filename,filePath):
  63. directories = os.listdir(filePath)
  64. for dir in directories:
  65. if dir == filename:
  66. shutil.rmtree(filePath+'\\'+filename)
  67. return 1
  68. return 0
  69. #Helper function Check if the file to render is copied completely before rendering
  70. def IsCopyFinished(self,fileName):
  71. timeout = 3600 # [seconds]
  72. timeout_start = time.time()
  73. if os.path.exists(fileName):
  74. while time.time() < timeout_start + timeout:
  75. try:
  76. with io.FileIO(fileName, "r+") as fileObj:
  77. log.logger.info("File loading completed.")
  78. return 1
  79. except IOError as ioe:
  80. pass
  81. log.logger.info("Issue with file.")
  82. return 0