|
@ -10,11 +10,12 @@ from ee.core.logging import Log |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EEFileUtils(): |
|
|
class EEFileUtils(): |
|
|
"""Method to operate on files""" |
|
|
"""Utilities to operate on files""" |
|
|
def __init__(): |
|
|
def __init__(): |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
def remove(self, filelist): |
|
|
def remove(self, filelist): |
|
|
|
|
|
"""remove files from given path""" |
|
|
for file in filelist: |
|
|
for file in filelist: |
|
|
if os.path.isfile(file): |
|
|
if os.path.isfile(file): |
|
|
Log.info(self, "Removing {0:65}".format(file), end=' ') |
|
|
Log.info(self, "Removing {0:65}".format(file), end=' ') |
|
@ -33,6 +34,10 @@ class EEFileUtils(): |
|
|
Log.error(self, 'Unable to Remove file ') |
|
|
Log.error(self, 'Unable to Remove file ') |
|
|
|
|
|
|
|
|
def create_symlink(self, paths, errormsg=''): |
|
|
def create_symlink(self, paths, errormsg=''): |
|
|
|
|
|
""" |
|
|
|
|
|
Create symbolic links provided in list with first as source |
|
|
|
|
|
and second as destination |
|
|
|
|
|
""" |
|
|
src = paths[0] |
|
|
src = paths[0] |
|
|
dst = paths[1] |
|
|
dst = paths[1] |
|
|
if not os.path.islink(dst): |
|
|
if not os.path.islink(dst): |
|
@ -45,6 +50,9 @@ class EEFileUtils(): |
|
|
Log.debug(self, "Destination: {0} exists".format(dst)) |
|
|
Log.debug(self, "Destination: {0} exists".format(dst)) |
|
|
|
|
|
|
|
|
def remove_symlink(self, filepath): |
|
|
def remove_symlink(self, filepath): |
|
|
|
|
|
""" |
|
|
|
|
|
Removes symbolic link for the path provided with filepath |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
os.unlink(filepath) |
|
|
os.unlink(filepath) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
@ -52,6 +60,11 @@ class EEFileUtils(): |
|
|
Log.error(self, "Unable to reomove symbolic link ...\n") |
|
|
Log.error(self, "Unable to reomove symbolic link ...\n") |
|
|
|
|
|
|
|
|
def copyfile(self, src, dest): |
|
|
def copyfile(self, src, dest): |
|
|
|
|
|
""" |
|
|
|
|
|
Copies files: |
|
|
|
|
|
src : source path |
|
|
|
|
|
dest : destination path |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
shutil.copy2(src, dest) |
|
|
shutil.copy2(src, dest) |
|
|
except shutil.Error as e: |
|
|
except shutil.Error as e: |
|
@ -64,6 +77,12 @@ class EEFileUtils(): |
|
|
.fromat(src, dest)) |
|
|
.fromat(src, dest)) |
|
|
|
|
|
|
|
|
def searchreplace(self, fnm, sstr, rstr): |
|
|
def searchreplace(self, fnm, sstr, rstr): |
|
|
|
|
|
""" |
|
|
|
|
|
Search replace strings in file |
|
|
|
|
|
fnm : filename |
|
|
|
|
|
sstr: search string |
|
|
|
|
|
rstr: replace string |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
for line in fileinput.input(fnm, inplace=True): |
|
|
for line in fileinput.input(fnm, inplace=True): |
|
|
print(line.replace(sstr, rstr), end='') |
|
|
print(line.replace(sstr, rstr), end='') |
|
@ -74,6 +93,11 @@ class EEFileUtils(): |
|
|
.format(fnm, sstr, rstr)) |
|
|
.format(fnm, sstr, rstr)) |
|
|
|
|
|
|
|
|
def mvfile(self, src, dst): |
|
|
def mvfile(self, src, dst): |
|
|
|
|
|
""" |
|
|
|
|
|
Moves file from source path to destination path |
|
|
|
|
|
src : source path |
|
|
|
|
|
dst : Destination path |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
Log.debug(self, "Moving file from {0} to {1}".format(src, dst)) |
|
|
Log.debug(self, "Moving file from {0} to {1}".format(src, dst)) |
|
|
shutil.move(src, dst) |
|
|
shutil.move(src, dst) |
|
@ -83,6 +107,10 @@ class EEFileUtils(): |
|
|
.format(src, dst)) |
|
|
.format(src, dst)) |
|
|
|
|
|
|
|
|
def chdir(self, path): |
|
|
def chdir(self, path): |
|
|
|
|
|
""" |
|
|
|
|
|
Change Directory to path specified |
|
|
|
|
|
Path : path for destination directory |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
os.chdir(path) |
|
|
os.chdir(path) |
|
|
except OSError as e: |
|
|
except OSError as e: |
|
@ -90,6 +118,14 @@ class EEFileUtils(): |
|
|
Log.error(self, 'Unable to Change Directory {0}'.format(path)) |
|
|
Log.error(self, 'Unable to Change Directory {0}'.format(path)) |
|
|
|
|
|
|
|
|
def chown(self, path, user, group, recursive=False): |
|
|
def chown(self, path, user, group, recursive=False): |
|
|
|
|
|
""" |
|
|
|
|
|
Change Owner for files |
|
|
|
|
|
change owner for file with path specified |
|
|
|
|
|
user: username of owner |
|
|
|
|
|
group: group of owner |
|
|
|
|
|
recursive: if recursive is True change owner for all |
|
|
|
|
|
files in directory |
|
|
|
|
|
""" |
|
|
userid = pwd.getpwnam(user)[2] |
|
|
userid = pwd.getpwnam(user)[2] |
|
|
groupid = pwd.getpwnam(user)[3] |
|
|
groupid = pwd.getpwnam(user)[3] |
|
|
try: |
|
|
try: |
|
@ -111,6 +147,12 @@ class EEFileUtils(): |
|
|
Log.error(self, "Unable to change owner : {0} ".format(path)) |
|
|
Log.error(self, "Unable to change owner : {0} ".format(path)) |
|
|
|
|
|
|
|
|
def chmod(self, path, perm, recursive=False): |
|
|
def chmod(self, path, perm, recursive=False): |
|
|
|
|
|
""" |
|
|
|
|
|
Changes Permission for files |
|
|
|
|
|
path : file path permission to be changed |
|
|
|
|
|
perm : permissions to be given |
|
|
|
|
|
recursive: change permission recursively for all files |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
if recursive: |
|
|
if recursive: |
|
|
for root, dirs, files in os.walk(path): |
|
|
for root, dirs, files in os.walk(path): |
|
@ -125,6 +167,11 @@ class EEFileUtils(): |
|
|
Log.error(self, "Unable to change owner : {0}".format(path)) |
|
|
Log.error(self, "Unable to change owner : {0}".format(path)) |
|
|
|
|
|
|
|
|
def mkdir(self, path): |
|
|
def mkdir(self, path): |
|
|
|
|
|
""" |
|
|
|
|
|
create directories. |
|
|
|
|
|
path : path for directory to be created |
|
|
|
|
|
Similar to `mkdir -p` |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
os.makedirs(path) |
|
|
os.makedirs(path) |
|
|
except OSError as e: |
|
|
except OSError as e: |
|
@ -132,6 +179,9 @@ class EEFileUtils(): |
|
|
Log.error(self, "Unable to create directory {0} ".format(path)) |
|
|
Log.error(self, "Unable to create directory {0} ".format(path)) |
|
|
|
|
|
|
|
|
def isexist(self, path): |
|
|
def isexist(self, path): |
|
|
|
|
|
""" |
|
|
|
|
|
Check if file exist on given path |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
if os.path.exists(path): |
|
|
if os.path.exists(path): |
|
|
return (True) |
|
|
return (True) |
|
@ -142,6 +192,9 @@ class EEFileUtils(): |
|
|
Log.error(self, "Unable to check path {0}".format(path)) |
|
|
Log.error(self, "Unable to check path {0}".format(path)) |
|
|
|
|
|
|
|
|
def grep(self, fnm, sstr): |
|
|
def grep(self, fnm, sstr): |
|
|
|
|
|
""" |
|
|
|
|
|
Searches for string in file and returns the matched line. |
|
|
|
|
|
""" |
|
|
try: |
|
|
try: |
|
|
for line in open(fnm): |
|
|
for line in open(fnm): |
|
|
if sstr in line: |
|
|
if sstr in line: |
|
@ -152,6 +205,9 @@ class EEFileUtils(): |
|
|
.format(sstr, fnm)) |
|
|
.format(sstr, fnm)) |
|
|
|
|
|
|
|
|
def rm(self, path): |
|
|
def rm(self, path): |
|
|
|
|
|
""" |
|
|
|
|
|
Remove files |
|
|
|
|
|
""" |
|
|
if EEFileUtils.isexist(self, path): |
|
|
if EEFileUtils.isexist(self, path): |
|
|
try: |
|
|
try: |
|
|
if os.path.isdir(path): |
|
|
if os.path.isdir(path): |
|
|