diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-12-27 11:43:27 +0100 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2020-12-28 19:42:50 +0100 |
commit | eeb78e842423319169b036d16601e73227dbffdd (patch) | |
tree | 282dac56ecfccf381fa92ad9027df6779d16ae2f /python/vyos/util.py | |
parent | 1efd20ab21e75e421487d563fc794a7f97361a3e (diff) | |
download | vyos-1x-eeb78e842423319169b036d16601e73227dbffdd.tar.gz vyos-1x-eeb78e842423319169b036d16601e73227dbffdd.zip |
webproxy: T563: squidguard: support default ruleset
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r-- | python/vyos/util.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index fc6915687..494c8155e 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -215,6 +215,30 @@ def read_file(fname, defaultonfailure=None): return defaultonfailure raise e +def write_file(fname, data, defaultonfailure=None, user=None, group=None): + """ + Write content of data to given fname, should defaultonfailure be not None, + it is returned on failure to read. + + If directory of file is not present, it is auto-created. + """ + dirname = os.path.dirname(fname) + if not os.path.isdir(dirname): + os.makedirs(dirname, mode=0o755, exist_ok=False) + chown(dirname, user, group) + + try: + """ Write a file to string """ + bytes = 0 + with open(fname, 'w') as f: + bytes = f.write(data) + chown(fname, user, group) + return bytes + except Exception as e: + if defaultonfailure is not None: + return defaultonfailure + raise e + def read_json(fname, defaultonfailure=None): """ |