summaryrefslogtreecommitdiff
path: root/python/vyos
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-04-02 21:17:47 +0200
committerChristian Poessinger <christian@poessinger.com>2020-04-02 21:17:47 +0200
commitd2cf287152bdc70c8f4d609e4edba3aac923dabd (patch)
treedcafb8ca6e419dd9efdebbacef1a1610eecd475a /python/vyos
parentdf59d92fe83ac2e3b15145e437d0a5130680eaca (diff)
downloadvyos-1x-d2cf287152bdc70c8f4d609e4edba3aac923dabd.tar.gz
vyos-1x-d2cf287152bdc70c8f4d609e4edba3aac923dabd.zip
vyos.util: add chown_file and chmod_x_file helpers
Diffstat (limited to 'python/vyos')
-rw-r--r--python/vyos/util.py21
1 files changed, 21 insertions, 0 deletions
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.