2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 def match_tags(box, all_tags, exact_tags):
20 """ returns True if box has required tags """
22 texact = set(exact_tags)
24 if texact and box.connections == texact:
27 if tall and tall.issubset(box.connections):
32 def find_boxes(box, all_tags = None, exact_tags = None, max_depth = 1):
33 """ Look for the connected boxes with the required tags, doing breath-first
34 search, until max_depth ( max_depth = None will traverse the entire graph ).
36 if not all_tags and not exact_tags:
37 msg = "No matching criteria for resources."
38 raise RuntimeError(msg)
41 # enqueue (depth, box)
52 (depth, a) = queue.pop()
53 if match_tags(a, all_tags, exact_tags):
56 if not max_depth or depth <= max_depth:
58 for b in sorted(a.connections):
59 if b not in traversed: