From d2cf287152bdc70c8f4d609e4edba3aac923dabd Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Thu, 2 Apr 2020 21:17:47 +0200 Subject: vyos.util: add chown_file and chmod_x_file helpers --- python/vyos/util.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'python') diff --git a/python/vyos/util.py b/python/vyos/util.py index 67aa87a3a..56a3f0e61 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -33,6 +33,27 @@ def read_file(path): return data +def chown_file(path, user, group): + """ change file owner """ + from pwd import getpwnam + from grp import getgrnam + + if os.path.isfile(path): + uid = getpwnam(user).pw_uid + gid = getgrnam(group).gr_gid + os.chown(path, uid, gid) + + +def chmod_x_file(path): + """ make file executable """ + from stat import S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH + + if os.path.isfile(path): + bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | \ + S_IROTH | S_IXOTH + os.chmod(path, bitmask) + + def colon_separated_to_dict(data_string, uniquekeys=False): """ Converts a string containing newline-separated entries of colon-separated key-value pairs into a dict. -- cgit v1.2.3