aboutsummaryrefslogtreecommitdiffstats
path: root/torouterui/util.py
diff options
context:
space:
mode:
authorficus <ficus@robocracy.org>2012-09-15 22:50:14 +0200
committerficus <ficus@robocracy.org>2012-09-15 22:50:14 +0200
commitced83335a5a338cb9c24956b96da526465d94df8 (patch)
treeb345cd21dfcbb9e660aceb2433c92bd50848938f /torouterui/util.py
parent57a1e70dee45f5cd9493d6d23f8fc7b6435d5388 (diff)
downloadtui-ced83335a5a338cb9c24956b96da526465d94df8.tar.gz
tui-ced83335a5a338cb9c24956b96da526465d94df8.zip
refactor to distributable format
Diffstat (limited to 'torouterui/util.py')
-rw-r--r--torouterui/util.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/torouterui/util.py b/torouterui/util.py
new file mode 100644
index 0000000..dc3b6c0
--- /dev/null
+++ b/torouterui/util.py
@@ -0,0 +1,28 @@
+"""
+Utility functions used by helper code to crudely grab the output of simple UNIX
+command line programs, plus a couple misc other functions.
+"""
+
+import os
+
+def cli_read(cmd):
+ p = os.popen(cmd)
+ return ''.join(p.readlines())
+
+def cli_read_lines(cmd):
+ p = os.popen(cmd)
+ return p.readlines()
+
+def fs_read(path):
+ with open(path, 'r') as f:
+ return ''.join(f.readlines())
+
+def prefix_to_ipv4_mask(prefixlen):
+ assert(prefixlen >= 0)
+ assert(prefixlen <= 32)
+ mask = (0xFFFFFFFF & (0xFFFFFFFF << (32 - prefixlen)))
+ a = (0xFF000000 & mask) >> 24
+ b = (0x00FF0000 & mask) >> 16
+ c = (0x0000FF00 & mask) >> 8
+ d = (0x000000FF & mask)
+ return '%d.%d.%d.%d' % (a, b, c, d)