Commit d81034da authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Added check for read/write/delete permissions at tempdir.


Former-commit-id: 51a8bf08
parent e3ce0b42
......@@ -671,7 +671,7 @@ class AtmCorr(object):
try:
from .cloud_masking import Cloud_Mask_Creator
CMC = Cloud_Mask_Creator(self.inObjs[0], algorithm=algorithm)
CMC = Cloud_Mask_Creator(self.inObjs[0], algorithm=algorithm, tempdir_root=CFG.job.path_tempdir)
CMC.calc_cloud_mask()
cm_geoarray = CMC.cloud_mask_geoarray
cm_array = CMC.cloud_mask_array
......
......@@ -30,7 +30,7 @@ __author__ = 'Daniel Scheffler'
class _FMASK_Runner(object):
"""The FMASK runner base class (not to be called directly)."""
def __init__(self, path_providerArchive, satellite, extract_archive):
def __init__(self, path_providerArchive, satellite, extract_archive, tempdir_root):
# TODO provide options of fmask_usgsLandsatStacked argparser
# private attributes
self._gdal_path_archive = None
......@@ -53,10 +53,9 @@ class _FMASK_Runner(object):
self.saturationmask_legend = {}
# create temporary directory
tempdir_rootPath = '/dev/shm/GeoMultiSens'
if not os.path.isdir(tempdir_rootPath):
os.makedirs(tempdir_rootPath)
self.tempdir = tempfile.mkdtemp(dir=tempdir_rootPath,
if tempdir_root and not os.path.isdir(tempdir_root):
os.makedirs(tempdir_root)
self.tempdir = tempfile.mkdtemp(dir=tempdir_root,
prefix='FMASK__%s__' % os.path.basename(self.path_archive))
# create subdirectory for FMASK internal intermediate files
......@@ -153,7 +152,8 @@ class _FMASK_Runner(object):
class FMASK_Runner_Landsat(_FMASK_Runner):
def __init__(self, path_providerArchive, satellite, TOARef=None, opticalDNs=None, thermalDNs=None):
def __init__(self, path_providerArchive, satellite, TOARef=None, opticalDNs=None, thermalDNs=None,
tempdir_root=None):
"""FMASK wrapper class for Landsat 4-8.
:param path_providerArchive: file path of the provider .tar.gz archive
......@@ -162,6 +162,7 @@ class FMASK_Runner_Landsat(_FMASK_Runner):
scaled from 0 to 10000 (optional -> generated from archive if not given)
:param opticalDNs: file path or GeoArray instance of optical data (digital numbers)
:param thermalDNs: file path or GeoArray instance of thermal data (digital numbers)
:param tempdir_root: directory to write intermediate data (auto-determined if not given)
"""
# private attributes
......@@ -176,7 +177,8 @@ class FMASK_Runner_Landsat(_FMASK_Runner):
'Landsat-8': dict(optical='L*_B[1-7,9].TIF', thermal='L*_B1[0,1].TIF', meta='*_MTL.txt')
}[satellite]
super(FMASK_Runner_Landsat, self).__init__(path_providerArchive, satellite, extract_archive=False)
super(FMASK_Runner_Landsat, self).__init__(path_providerArchive, satellite, extract_archive=False,
tempdir_root=tempdir_root)
# populate optional attributes
if TOARef is not None:
......@@ -300,7 +302,7 @@ class FMASK_Runner_Landsat(_FMASK_Runner):
class FMASK_Runner_Sentinel2(_FMASK_Runner):
def __init__(self, path_providerArchive, satellite, scene_ID=None, granule_ID='', target_res=20, TOARef=None,
extract_archive=False):
extract_archive=False, tempdir_root=None):
"""FMASK wrapper class for Sentinel-2.
:param path_providerArchive: file path of the provider .zip archive
......@@ -312,6 +314,7 @@ class FMASK_Runner_Sentinel2(_FMASK_Runner):
scaled from 0 to 10000 (optional -> read from archive if not given)
:param extract_archive: whether to extract the archive to disk or to read from the archive directly
(default: False); NOTE: There is no remarkable speed difference.
:param tempdir_root: directory to write intermediate data (auto-determined if not given)
"""
self._granule_ID = granule_ID
......@@ -333,7 +336,8 @@ class FMASK_Runner_Sentinel2(_FMASK_Runner):
metaNEWStyle='*MTD_TL.xml'),
}[satellite]
super(FMASK_Runner_Sentinel2, self).__init__(path_providerArchive, satellite, extract_archive)
super(FMASK_Runner_Sentinel2, self).__init__(path_providerArchive, satellite, extract_archive,
tempdir_root=tempdir_root)
# populate optional attributes
if TOARef is not None:
......@@ -443,7 +447,7 @@ class FMASK_Runner_Sentinel2(_FMASK_Runner):
class Cloud_Mask_Creator(object):
def __init__(self, GMS_object, algorithm, target_res=None):
def __init__(self, GMS_object, algorithm, target_res=None, tempdir_root=None):
"""A class for creating cloud masks.
:param GMS_object: instance or subclass instance of model.gms_object.GMS_object
......@@ -455,6 +459,7 @@ class Cloud_Mask_Creator(object):
self.GMS_obj = GMS_object
self.algorithm = algorithm
self.tgt_res = target_res if target_res else self.GMS_obj.ac_options['cld_mask']['target_resolution']
self.tempdir_root = tempdir_root
self.cloud_mask_geoarray = None
self.cloud_mask_array = None
......@@ -482,7 +487,7 @@ class Cloud_Mask_Creator(object):
else:
FMR = FMASK_Runner_Sentinel2(self.GMS_obj.path_archive, self.GMS_obj.satellite,
scene_ID=self.GMS_obj.scene_ID,
target_res=self.tgt_res)
target_res=self.tgt_res, tempdir_root=self.tempdir_root)
self.cloud_mask_geoarray = FMR.calc_cloudMask()
self.cloud_mask_array = self.cloud_mask_geoarray[:]
......
......@@ -102,7 +102,7 @@ class GEOPROCESSING(object):
self.shortname, self.extension = os.path.splitext(self.filename)
# ****OBJECT ATTRIBUTES***************************************************
self.workspace = "/dev/shm/GeoMultiSens/GEOPROCESSING_temp/" if workspace is None else workspace
self.workspace = os.path.join(CFG.job.path_tempdir, 'GEOPROCESSING_temp') if workspace is None else workspace
if v:
self.logger.debug("\n--")
self.logger.debug("\ttemporary geoprocessing workspace", self.workspace)
......
......@@ -85,10 +85,16 @@ class GMSEnvironment(object):
pass
def check_read_write_permissions(self):
# folders = []
@staticmethod
def check_read_write_permissions():
if not os.access(CFG.job.path_tempdir, os.R_OK):
raise PermissionError('No read-permissions at %s.' % CFG.job.path_tempdir)
pass
if not os.access(CFG.job.path_tempdir, os.W_OK):
raise PermissionError('No write-permissions at %s.' % CFG.job.path_tempdir)
if not os.access(CFG.job.path_tempdir, os.X_OK):
raise PermissionError('No delete-permissions at %s.' % CFG.job.path_tempdir)
@staticmethod
def check_paths(): # TODO to be completed
......
......@@ -1344,8 +1344,8 @@ class GMS_object(Dataset):
raise NotImplementedError
# delete tempfiles created by HLP_F.get_tempfile()
files2delete = glob.glob(os.path.join(self.ExtractedFolder, 'GeoMultiSens_*'))
files2delete += glob.glob(os.path.join('/dev/shm/', 'GeoMultiSens_*'))
files2delete = glob.glob(os.path.join(self.ExtractedFolder, '*')) # all files within ExtractedFolder
files2delete += glob.glob(os.path.join(CFG.job.path_tempdir + 'GeoMultiSens_*'))
for f in files2delete:
HLP_F.silentremove(f)
......
......@@ -89,6 +89,7 @@ class process_controller(object):
# check environment
self.GMSEnv = ENV.GMSEnvironment(self.logger)
self.GMSEnv.check_dependencies()
self.GMSEnv.check_read_write_permissions()
# check if process_controller is executed by debugger
# isdebugging = 1 if True in [frame[1].endswith("pydevd.py") for frame in inspect.stack()] else False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment