summaryrefslogtreecommitdiff
path: root/python/vyos/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r--python/vyos/util.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 3970b8bf1..87c2dcedc 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -16,6 +16,44 @@
import os
import re
import sys
+from subprocess import Popen, PIPE, STDOUT
+
+
+# debugging
+
+
+def debug(flag):
+ return flag if os.path.isfile(f'/tmp/vyos.{flag}.debug') else ''
+
+
+def debug_msg(message, section=''):
+ if section:
+ print(f'DEBUG/{section:<6} {message}')
+
+
+# commands
+
+
+def popen(command, section=''):
+ p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
+ tmp = p.communicate()[0].strip()
+ debug_msg(f"cmd '{command}'", section)
+ decoded = tmp.decode()
+ if decoded:
+ debug_msg(f"returned:\n{decoded}", section)
+ return decoded, p.returncode
+
+
+def cmd(command, section=''):
+ decoded, code = popen(command, section)
+ if code != 0:
+ # error code can be recovered with .errno
+ raise OSError(code, f'{command}\nreturned: {decoded}')
+ return decoded
+
+
+# file manipulation
+
def read_file(path):
""" Read a file to string """