/ee/core/fileutils.py

https://gitlab.com/mattyhead/easyengine
Python | 268 lines | 183 code | 16 blank | 69 comment | 54 complexity | cd9ff7b86c79de02dc4045b2f3461523 MD5 | raw file
  1. """EasyEngine file utils core classes."""
  2. import shutil
  3. import os
  4. import sys
  5. import glob
  6. import shutil
  7. import pwd
  8. import fileinput
  9. from ee.core.logging import Log
  10. class EEFileUtils():
  11. """Utilities to operate on files"""
  12. def __init__():
  13. pass
  14. def remove(self, filelist):
  15. """remove files from given path"""
  16. for file in filelist:
  17. if os.path.isfile(file):
  18. Log.info(self, "Removing {0:65}".format(file), end=' ')
  19. os.remove(file)
  20. Log.info(self, "{0}".format("[" + Log.ENDC + "Done" +
  21. Log.OKBLUE + "]"))
  22. Log.debug(self, 'file Removed')
  23. if os.path.isdir(file):
  24. try:
  25. Log.info(self, "Removing {0:65}".format(file), end=' ')
  26. shutil.rmtree(file)
  27. Log.info(self, "{0}".format("[" + Log.ENDC + "Done" +
  28. Log.OKBLUE + "]"))
  29. except shutil.Error as e:
  30. Log.debug(self, "{err}".format(err=str(e.reason)))
  31. Log.error(self, 'Unable to Remove file ')
  32. def create_symlink(self, paths, errormsg=''):
  33. """
  34. Create symbolic links provided in list with first as source
  35. and second as destination
  36. """
  37. src = paths[0]
  38. dst = paths[1]
  39. if not os.path.islink(dst):
  40. try:
  41. Log.debug(self, "Creating Symbolic link, Source:{0}, Dest:{1}"
  42. .format(src, dst))
  43. os.symlink(src, dst)
  44. except Exception as e:
  45. Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
  46. Log.error(self, "Unable to create symbolic link ...\n ")
  47. else:
  48. Log.debug(self, "Destination: {0} exists".format(dst))
  49. def remove_symlink(self, filepath):
  50. """
  51. Removes symbolic link for the path provided with filepath
  52. """
  53. try:
  54. Log.debug(self, "Removing symbolic link: {0}".format(filepath))
  55. os.unlink(filepath)
  56. except Exception as e:
  57. Log.debug(self, "{0}".format(e))
  58. Log.error(self, "Unable to reomove symbolic link ...\n")
  59. def copyfiles(self, src, dest):
  60. """
  61. Copies files:
  62. src : source path
  63. dest : destination path
  64. Recursively copy an entire directory tree rooted at src.
  65. The destination directory, named by dst, must not already exist;
  66. it will be created as well as missing parent directories.
  67. """
  68. try:
  69. Log.debug(self, "Copying files, Source:{0}, Dest:{1}"
  70. .format(src, dest))
  71. shutil.copytree(src, dest)
  72. except shutil.Error as e:
  73. Log.debug(self, "{0}".format(e))
  74. Log.error(self, 'Unable to copy files from {0} to {1}'
  75. .format(src, dest))
  76. except IOError as e:
  77. Log.debug(self, "{0}".format(e.strerror))
  78. Log.error(self, "Unable to copy files from {0} to {1}"
  79. .format(src, dest))
  80. def copyfile(self, src, dest):
  81. """
  82. Copy file:
  83. src : source path
  84. dest : destination path
  85. """
  86. try:
  87. Log.debug(self, "Copying file, Source:{0}, Dest:{1}"
  88. .format(src, dest))
  89. shutil.copy2(src, dest)
  90. except shutil.Error as e:
  91. Log.debug(self, "{0}".format(e))
  92. Log.error(self, 'Unable to copy file from {0} to {1}'
  93. .format(src, dest))
  94. except IOError as e:
  95. Log.debug(self, "{0}".format(e.strerror))
  96. Log.error(self, "Unable to copy file from {0} to {1}"
  97. .format(src, dest))
  98. def searchreplace(self, fnm, sstr, rstr):
  99. """
  100. Search replace strings in file
  101. fnm : filename
  102. sstr: search string
  103. rstr: replace string
  104. """
  105. try:
  106. Log.debug(self, "Doning search and replace, File:{0},"
  107. "Source string:{1}, Dest String:{2}"
  108. .format(fnm, sstr, rstr))
  109. for line in fileinput.input(fnm, inplace=True):
  110. print(line.replace(sstr, rstr), end='')
  111. fileinput.close()
  112. except Exception as e:
  113. Log.debug(self, "{0}".format(e))
  114. Log.error(self, "Unable to search {0} and replace {1} {2}"
  115. .format(fnm, sstr, rstr))
  116. def mvfile(self, src, dst):
  117. """
  118. Moves file from source path to destination path
  119. src : source path
  120. dst : Destination path
  121. """
  122. try:
  123. Log.debug(self, "Moving file from {0} to {1}".format(src, dst))
  124. shutil.move(src, dst)
  125. except Exception as e:
  126. Log.debug(self, "{err}".format(err=e))
  127. Log.error(self, 'Unable to move file from {0} to {1}'
  128. .format(src, dst))
  129. def chdir(self, path):
  130. """
  131. Change Directory to path specified
  132. Path : path for destination directory
  133. """
  134. try:
  135. Log.debug(self, "Changing directory to {0}"
  136. .format(path))
  137. os.chdir(path)
  138. except OSError as e:
  139. Log.debug(self, "{err}".format(err=e.strerror))
  140. Log.error(self, 'Unable to Change Directory {0}'.format(path))
  141. def chown(self, path, user, group, recursive=False):
  142. """
  143. Change Owner for files
  144. change owner for file with path specified
  145. user: username of owner
  146. group: group of owner
  147. recursive: if recursive is True change owner for all
  148. files in directory
  149. """
  150. userid = pwd.getpwnam(user)[2]
  151. groupid = pwd.getpwnam(user)[3]
  152. try:
  153. Log.debug(self, "Changing ownership of {0}, Userid:{1},Groupid:{2}"
  154. .format(path, userid, groupid))
  155. # Change inside files/directory permissions only if recursive flag
  156. # is set
  157. if recursive:
  158. for root, dirs, files in os.walk(path):
  159. for d in dirs:
  160. os.chown(os.path.join(root, d), userid,
  161. groupid)
  162. for f in files:
  163. os.chown(os.path.join(root, f), userid,
  164. groupid)
  165. os.chown(path, userid, groupid)
  166. except shutil.Error as e:
  167. Log.debug(self, "{0}".format(e))
  168. Log.error(self, "Unable to change owner : {0}".format(path))
  169. except Exception as e:
  170. Log.debug(self, "{0}".format(e))
  171. Log.error(self, "Unable to change owner : {0} ".format(path))
  172. def chmod(self, path, perm, recursive=False):
  173. """
  174. Changes Permission for files
  175. path : file path permission to be changed
  176. perm : permissions to be given
  177. recursive: change permission recursively for all files
  178. """
  179. try:
  180. Log.debug(self, "Changing permission of {0}, Perm:{1}"
  181. .format(path, perm))
  182. if recursive:
  183. for root, dirs, files in os.walk(path):
  184. for d in dirs:
  185. os.chmod(os.path.join(root, d), perm)
  186. for f in files:
  187. os.chmod(os.path.join(root, f), perm)
  188. else:
  189. os.chmod(path, perm)
  190. except OSError as e:
  191. Log.debug(self, "{0}".format(e.strerror))
  192. Log.error(self, "Unable to change owner : {0}".format(path))
  193. def mkdir(self, path):
  194. """
  195. create directories.
  196. path : path for directory to be created
  197. Similar to `mkdir -p`
  198. """
  199. try:
  200. Log.debug(self, "Creating directories: {0}"
  201. .format(path))
  202. os.makedirs(path)
  203. except OSError as e:
  204. Log.debug(self, "{0}".format(e.strerror))
  205. Log.error(self, "Unable to create directory {0} ".format(path))
  206. def isexist(self, path):
  207. """
  208. Check if file exist on given path
  209. """
  210. try:
  211. if os.path.exists(path):
  212. return (True)
  213. else:
  214. return (False)
  215. except OSError as e:
  216. Log.debug(self, "{0}".format(e.strerror))
  217. Log.error(self, "Unable to check path {0}".format(path))
  218. def grep(self, fnm, sstr):
  219. """
  220. Searches for string in file and returns the matched line.
  221. """
  222. try:
  223. Log.debug(self, "Finding string {0} to file {1}"
  224. .format(sstr, fnm))
  225. for line in open(fnm, encoding='utf-8'):
  226. if sstr in line:
  227. return line
  228. return False
  229. except OSError as e:
  230. Log.debug(self, "{0}".format(e.strerror))
  231. Log.error(self, "Unable to Search string {0} in {1}"
  232. .format(sstr, fnm))
  233. def rm(self, path):
  234. """
  235. Remove files
  236. """
  237. Log.debug(self, "Removing {0}".format(path))
  238. if EEFileUtils.isexist(self, path):
  239. try:
  240. if os.path.isdir(path):
  241. shutil.rmtree(path)
  242. else:
  243. os.remove(path)
  244. except shutil.Error as e:
  245. Log.debug(self, "{0}".format(e))
  246. Log.error(self, "Unable to remove directory : {0} "
  247. .format(path))
  248. except OSError as e:
  249. Log.debug(self, "{0}".format(e))
  250. Log.error(self, "Unable to remove file : {0} "
  251. .format(path))