Jam/filecheck.py

47 lines
2.0 KiB
Python
Raw Normal View History

2021-10-20 04:55:20 +00:00
import os.path
# check that a file can be edited
def check_edit(file):
# check that the file is not a directory
if os.path.isdir(file):
return(-1,"error: '"+file+"' is a directory")
if os.path.isfile(file):
# check permissions
if not os.access(file,os.R_OK):
return(-2,"error: permission denied: cannot read file '"+file+"'")
else:
# check write to directory
if len(os.path.dirname(file))>0:
if not os.access(os.path.dirname(file),os.W_OK):
return(-3,"error: permission denied: cannot write to parent directory '"+os.path.dirname(file)+"'")
else:
if not os.access(os.getcwd(),os.W_OK):
return(-4,"error: permission denied: cannot write to parent directory '"+os.getcwd()+"'")
return(0,"")
# check that a file can be written
def check_write(file,overwrite):
# check that the file is not a directory
if os.path.isdir(file):
return(-1,"error: '"+file+"' is a directory")
# check that file does not already exist
if not overwrite and os.path.isfile(file):
return(-2,"error: '"+file+"' already exists")
# check that the containing directory exists
if len(os.path.dirname(file))>0 and not os.path.isdir(os.path.dirname(file)):
return(-3,"error: could not find directory '"+os.path.dirname(file)+"'")
# check permissions
if os.path.isfile(file):
if not os.access(file,os.W_OK):
return(-4,"error: permission denied: cannot write to file '"+file+"'")
else:
# check write to directory
if len(os.path.dirname(file))>0:
if not os.access(os.path.dirname(file),os.W_OK):
return(-5,"error: permission denied: cannot write to parent directory '"+os.path.dirname(file)+"'")
else:
if not os.access(os.getcwd(),os.W_OK):
return(-6,"error: permission denied: cannot write to parent directory '"+os.getcwd()+"'")
return(0,"")