Skip to content

Commit 219642f

Browse files
add set_permissions eu-cdse/openeo-cdse-infra#609
1 parent 8f28a2c commit 219642f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

openeogeotrellis/ml/modelloader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from openeo_driver.utils import generate_unique_id
1616

1717
from openeogeotrellis.configparams import ConfigParams
18-
from openeogeotrellis.utils import s3_client, add_permissions
18+
from openeogeotrellis.utils import s3_client, set_permissions
1919
from .geopysparkmlmodel import GeopysparkMlModel, ModelArchitecture
2020
from .geopysparkcatboostmodel import GeopySparkCatBoostModel
2121
from .geopysparkrandomforestmodel import GeopySparkRandomForestModel
@@ -181,9 +181,9 @@ def _create_model_dir(gps_batch_jobs: 'GpsBatchJobs', use_s3: bool = False) -> s
181181

182182
if not ml_models_dir_exists:
183183
# Everyone can access the ml_models directory: `drwxrwxrwx user group`
184-
add_permissions(ml_models_dir, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
184+
set_permissions(ml_models_dir, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
185185
# Only the user has full access to this directory: `drwx------. user group`
186-
add_permissions(result_dir, mode=stat.S_IRWXU, user=None)
186+
set_permissions(result_dir, mode=stat.S_IRWXU, user=None)
187187
return str(result_dir)
188188

189189
except Exception as e:
@@ -283,4 +283,4 @@ def _is_valid_url(url: str) -> bool:
283283
parsed = urlparse(url)
284284
return parsed.scheme in ["http", "https"] and parsed.netloc
285285
except Exception:
286-
return False
286+
return False

openeogeotrellis/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,25 @@ def add_permissions_with_failsafe(path: Path, mode: int, user=None, group=None):
451451
p.chmod(current_permission_bits | mode)
452452

453453

454+
def set_permissions(path: Path, mode: int, user=None, group=None):
455+
"""
456+
Set permissions to a file or directory, and optionally change its ownership.
457+
"""
458+
if str(path).lower().startswith("s3:/"):
459+
logger.warning(f"set_permissions called on S3 path {path!r}, which is not supported.")
460+
return
461+
if not path.exists():
462+
raise FileNotFoundError
463+
os.chmod(path, mode)
464+
if user is not None or group is not None:
465+
try:
466+
shutil.chown(path, user=user, group=group)
467+
except LookupError as e:
468+
logger.warning(f"Could not change user/group of {path} to {user}/{group}.")
469+
except PermissionError as e:
470+
logger.warning(f"Could not change user/group of {path} to {user}/{group}, no permissions.")
471+
472+
454473
def ensure_executor_logging(f) -> Callable:
455474
def setup_context_aware_logging(user_id: Optional[str], request_id: str):
456475
job_id = get_job_id()

0 commit comments

Comments
 (0)