docker-seafile-client/dsc/misc.py

49 lines
1.6 KiB
Python
Raw Permalink Normal View History

2019-09-06 18:41:43 +00:00
import os
import pwd
import subprocess
from dsc.const import DEFAULT_USERNAME
2019-09-06 18:41:43 +00:00
def setup_uid(uid: int, gid: int):
"""
Set GID and UID of default user so that seafile client creates files with
correct permissions.
If GID does not match, create a new group with the given GID.
Then update UID and GID of default user to match the given ones.
"""
2019-09-06 18:41:43 +00:00
user_pwinfo = pwd.getpwnam(DEFAULT_USERNAME)
create_group(gid)
2019-09-06 18:41:43 +00:00
if user_pwinfo.pw_uid != uid or user_pwinfo.pw_gid != gid:
subprocess.call(["usermod", "-o", "-u", str(uid), "-g", str(gid), DEFAULT_USERNAME])
def create_group(gid: int):
"""Search for a group with the given GID. If not found, create a new one."""
if not os.path.exists(f"/etc/group"):
raise RuntimeError(f"File /etc/group does not exist")
with open("/etc/group", "r") as f:
for line in f.readlines():
cur_gid = line.split(sep=":", maxsplit=3)[2]
if int(cur_gid) == gid:
return
subprocess.call(["groupadd", "-g", str(gid), DEFAULT_USERNAME + "-data"])
2019-09-06 18:41:43 +00:00
def create_dir(dir_path: str):
if not os.path.exists(dir_path):
os.mkdir(dir_path)
user_pwinfo = pwd.getpwnam(DEFAULT_USERNAME)
os.chown(dir_path, user_pwinfo.pw_uid, user_pwinfo.pw_gid)
else:
if not os.path.isdir(dir_path):
raise RuntimeError(f"Data dir {dir_path} is not a directory")
2023-09-14 23:22:02 +00:00
def hide_password(cmd: list, password: str) -> list:
cmd = cmd.copy()
for i, arg in enumerate(cmd):
2023-10-21 01:07:54 +00:00
if password in arg:
cmd[i] = arg.replace(password, "********")
2023-09-14 23:22:02 +00:00
return cmd