Skip to content

Commit

Permalink
add helples
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed May 14, 2024
1 parent 5ac7e4f commit 4016b74
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions library/1.0.0/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from . import utils
import pathlib
import re
import os

RE_ID = re.compile(r"^[0-9]+$")
RE_MODE = re.compile(r"^(0o)?([0-7]{3})$")


def chown(path, uid, gid):
if not pathlib.Path(path).exists():
utils.throw_error(f"Path [{path}] does not exist")

if not RE_ID.match(uid):
utils.throw_error(f"User ID must be a number, but got [{uid}]")
if not RE_ID.match(gid):
utils.throw_error(f"Group ID must be a number, but got [{gid}]")

os.chown(path, int(uid), int(gid))
return ""


def chmod(path, mode):
if not pathlib.Path(path).exists():
utils.throw_error(f"Path [{path}] does not exist")

if not RE_MODE.match(mode):
utils.throw_error(
f"Mode must be in octal format, but got [{mode}]. Example: 0o755 or 755"
)

os.chmod(path, int(mode, 8))
return ""

0 comments on commit 4016b74

Please sign in to comment.