From c74b7b95fb5d65dcf0fee7eb5a5525ae884ab209 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Tue, 24 Mar 2015 22:10:54 +0100 Subject: [PATCH 1/2] update package structure, move non-executables to lib --- backend.py | 19 +++++++++---------- GlobalRRD.py => lib/GlobalRRD.py | 3 ++- NodeRRD.py => lib/NodeRRD.py | 3 ++- RRD.py => lib/RRD.py | 0 lib/__init__.py | 1 + alfred.py => lib/alfred.py | 0 batman.py => lib/batman.py | 0 graph.py => lib/graph.py | 6 ++++-- nodes.py => lib/nodes.py | 0 rrddb.py => lib/rrddb.py | 5 +++-- 10 files changed, 21 insertions(+), 16 deletions(-) rename GlobalRRD.py => lib/GlobalRRD.py (97%) rename NodeRRD.py => lib/NodeRRD.py (98%) rename RRD.py => lib/RRD.py (100%) create mode 100644 lib/__init__.py rename alfred.py => lib/alfred.py (100%) rename batman.py => lib/batman.py (100%) rename graph.py => lib/graph.py (98%) rename nodes.py => lib/nodes.py (100%) rename rrddb.py => lib/rrddb.py (96%) diff --git a/backend.py b/backend.py index 2d8cc9a..5b60d3e 100755 --- a/backend.py +++ b/backend.py @@ -6,20 +6,19 @@ https://github.com/ffnord/ffmap-backend import argparse import json import os -import networkx as nx from datetime import datetime + +import networkx as nx from networkx.readwrite import json_graph -import alfred -import nodes -import graph -from batman import Batman -from rrddb import RRD +from lib import alfred, graph, nodes +from lib.batman import Batman +from lib.rrddb import RRD def main(params): - nodes_fn = os.path.join(params['destination_directory'], 'nodes.json') - graph_fn = os.path.join(params['destination_directory'], 'graph.json') + nodes_fn = os.path.join(params['dest_dir'], 'nodes.json') + graph_fn = os.path.join(params['dest_dir'], 'graph.json') now = datetime.utcnow().replace(microsecond=0) @@ -77,7 +76,7 @@ def main(params): if params['rrd']: script_directory = os.path.dirname(os.path.realpath(__file__)) rrd = RRD(os.path.join(script_directory, 'nodedb'), - os.path.join(params['destination_directory'], 'nodes')) + os.path.join(params['dest_dir'], 'nodes')) rrd.update_database(nodedb['nodes']) rrd.update_images() @@ -92,7 +91,7 @@ if __name__ == '__main__': parser.add_argument('-m', '--mesh', action='append', default=['bat0'], help='batman mesh interface (defaults to bat0)') - parser.add_argument('-d', '--destination-directory', action='store', + parser.add_argument('-d', '--dest-dir', action='store', help='destination directory for generated files', required=True) parser.add_argument('--vpn', action='append', metavar='MAC', diff --git a/GlobalRRD.py b/lib/GlobalRRD.py similarity index 97% rename from GlobalRRD.py rename to lib/GlobalRRD.py index 9c09549..47235f2 100644 --- a/GlobalRRD.py +++ b/lib/GlobalRRD.py @@ -1,6 +1,7 @@ import os import subprocess -from RRD import RRD, DS, RRA + +from lib.RRD import DS, RRA, RRD class GlobalRRD(RRD): diff --git a/NodeRRD.py b/lib/NodeRRD.py similarity index 98% rename from NodeRRD.py rename to lib/NodeRRD.py index a4ec092..37bc6f9 100644 --- a/NodeRRD.py +++ b/lib/NodeRRD.py @@ -1,6 +1,7 @@ import os import subprocess -from RRD import RRD, DS, RRA + +from lib.RRD import DS, RRA, RRD class NodeRRD(RRD): diff --git a/RRD.py b/lib/RRD.py similarity index 100% rename from RRD.py rename to lib/RRD.py diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..64bd3f3 --- /dev/null +++ b/lib/__init__.py @@ -0,0 +1 @@ +__author__ = 'hexa' diff --git a/alfred.py b/lib/alfred.py similarity index 100% rename from alfred.py rename to lib/alfred.py diff --git a/batman.py b/lib/batman.py similarity index 100% rename from batman.py rename to lib/batman.py diff --git a/graph.py b/lib/graph.py similarity index 98% rename from graph.py rename to lib/graph.py index 460e327..d5163ff 100644 --- a/graph.py +++ b/lib/graph.py @@ -1,7 +1,9 @@ -import networkx as nx from functools import reduce from itertools import chain -from nodes import build_mac_table + +import networkx as nx + +from lib.nodes import build_mac_table def import_vis_data(graph, nodes, vis_data): diff --git a/nodes.py b/lib/nodes.py similarity index 100% rename from nodes.py rename to lib/nodes.py diff --git a/rrddb.py b/lib/rrddb.py similarity index 96% rename from rrddb.py rename to lib/rrddb.py index b023e6b..bcd33b3 100644 --- a/rrddb.py +++ b/lib/rrddb.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 import time import os -from GlobalRRD import GlobalRRD -from NodeRRD import NodeRRD + +from lib.GlobalRRD import GlobalRRD +from lib.NodeRRD import NodeRRD class RRD(object): From 6fba8ad21b50cacbb77e27ad6e981135db9e576d Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Tue, 24 Mar 2015 22:48:00 +0100 Subject: [PATCH 2/2] add alfred socket support (--alfred-sock) --- backend.py | 8 +++++++- lib/alfred.py | 37 ++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/backend.py b/backend.py index 5b60d3e..3eaeaf1 100755 --- a/backend.py +++ b/backend.py @@ -11,7 +11,8 @@ from datetime import datetime import networkx as nx from networkx.readwrite import json_graph -from lib import alfred, graph, nodes +from lib import graph, nodes +from lib.alfred import Alfred from lib.batman import Batman from lib.rrddb import RRD @@ -34,6 +35,8 @@ def main(params): for node_id, node in nodedb['nodes'].items(): node['flags']['online'] = False + alfred = Alfred(unix_sockpath=params['alfred_sock']) + nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(), now, assume_online=True) @@ -91,6 +94,9 @@ if __name__ == '__main__': parser.add_argument('-m', '--mesh', action='append', default=['bat0'], help='batman mesh interface (defaults to bat0)') + parser.add_argument('-s', '--alfred-sock', + default=None, + help='alfred unix socket path') parser.add_argument('-d', '--dest-dir', action='store', help='destination directory for generated files', required=True) diff --git a/lib/alfred.py b/lib/alfred.py index 0467316..1b9b220 100644 --- a/lib/alfred.py +++ b/lib/alfred.py @@ -1,20 +1,35 @@ import subprocess import json +import os -def _fetch(data_type): - output = subprocess.check_output( - ["alfred-json", "-z", "-f", "json", "-r", str(data_type)]) - return json.loads(output.decode("utf-8")).values() +class Alfred(object): + """ + Bindings for the alfred-json utility + """ + def __init__(self, unix_sockpath=None): + if unix_sockpath: + if os.path.exists(unix_sockpath): + self.unix_sock = unix_sockpath + else: + raise RuntimeError('alfred: invalid unix socket path given') + def _fetch(self, data_type): + cmd = ['alfred-json', + '-z', + '-f', 'json', + '-r', data_type] + if self.unix_sock: + cmd.extend(['-s', self.unix_sock]) -def nodeinfo(): - return _fetch(158) + output = subprocess.check_output(cmd) + return json.loads(output.decode("utf-8")).values() + def nodeinfo(self): + return self._fetch(158) -def statistics(): - return _fetch(159) + def statistics(self): + return self._fetch(159) - -def vis(): - return _fetch(160) + def vis(self): + return self._fetch(160)