Skip to content

Commit

Permalink
fix identation
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed May 16, 2024
1 parent 155888b commit fdbd304
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions library/1.0.0/permissions.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
from . import utils
import textwrap
import re

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


def check_path(path):
out = f"""
if not pathlib.Path("{path}").exists():
raise Exception(f"Path [{path}] does not exist")
"""
return textwrap.dedent(out)
return f"""
if not pathlib.Path("{path}").exists():
raise Exception(f"Path [{path}] does not exist")
"""


def check_empty(path, force=False):
out = f"""
if any(pathlib.Path("{path}").iterdir()):
if not {force}:
raise Exception(
f"Path [{path}] is not empty, skipping... Use [force=True] to override"
)
"""
return textwrap.dedent(out)
return f"""
if any(pathlib.Path("{path}").iterdir()):
if not {force}:
raise Exception(
f"Path [{path}] is not empty, skipping... Use [force=True] to override"
)
"""


def chown(path, uid, gid, force=False):
Expand All @@ -31,12 +28,13 @@ def chown(path, uid, gid, force=False):
if not RE_ID.match(str(gid)):
utils.throw_error(f"Group ID must be a number, but got [{gid}]")

out = f"""
{check_path(path)}
{check_empty(path, force)}
os.chown("{path}", {int(uid)}, {int(gid)})
"""
return textwrap.dedent(out)
return f"""
{check_path(path)}
{check_empty(path, force)}
print(f"Changing owner of [{path}] to [{uid}:{gid}]")
os.chown("{path}", {int(uid)}, {int(gid)})
print("New owner:", (os.stat("{path}").st_uid, os.stat("{path}").st_gid))
"""


def chmod(path, mode, force=False):
Expand All @@ -45,9 +43,10 @@ def chmod(path, mode, force=False):
f"Mode must be in octal format, but got [{mode}]. Example: 0o755 or 755"
)

out = f"""
{check_path(path)}
{check_empty(path, force)}
os.chmod("{path}", {int(mode, 8)})
"""
return textwrap.dedent(out)
return f"""
{check_path(path)}
{check_empty(path, force)}
print(f"Changing mode of [{path}] to [{mode}]")
os.chmod("{path}", {int(mode, 8)})
print("New mode:", (os.stat("{path}").st_mode).to_octal())
"""

0 comments on commit fdbd304

Please sign in to comment.