def print_leaves(leaves, name='Leaves', indent=''): ''' Pretty prints a list of clients (names only) @param leaves @type [concert_msgs.ConcertClient] ''' console.pretty_println(indent + "%s" % name, console.bold) console.pretty_print(indent + " Clients: ", console.cyan) console.pretty_println("%s " % [leaf.name for leaf in leaves], console.yellow)
def test_chatter_configuration(self): console.pretty_println("\n*************** Chatter Configuration ************\n", console.bold) nodes, concert_clients = setup_chatter_configuration() compatibility_tree = concert_orchestra.create_compatibility_tree(nodes, concert_clients) compatibility_tree.print_branches("\nCompatibility Tree\n", '') print("") pruned_branches = concert_orchestra.prune_compatibility_tree(compatibility_tree, verbosity=True) concert_orchestra.print_branches(pruned_branches, "\nPruned Compatibility Tree\n", ' ') for branch in pruned_branches: if branch.name() == 'dudette': self.assertEquals(1, len(branch.leaves)) self.assertEquals('dudette', branch.leaves[0].name) elif branch.name() == 'dude': self.assertEquals(2, len(branch.leaves))
def main(): global processes global roslaunch_pids signal.signal(signal.SIGINT, signal_handler) args = parse_arguments() if not which('konsole') and not which('gnome-terminal') and not which( 'x-terminal-emulator'): console.error( "Cannot find a suitable terminal [x-terminal-emulator, konsole, gnome-termional]" ) sys.exit(1) terminal = choose_terminal(args.gnome, args.konsole) if args.package == '': rocon_launcher = roslaunch.rlutil.resolve_launch_arguments( args.launcher)[0] else: rocon_launcher = roslaunch.rlutil.resolve_launch_arguments( [args.package] + args.launcher)[0] if args.screen: roslaunch_options = "--screen" else: roslaunch_options = "" launchers = parse_rocon_launcher(rocon_launcher, roslaunch_options) for launcher in launchers: console.pretty_println( "Launching [%s, %s] on port %s" % (launcher['package'], launcher['name'], launcher['port']), console.bold) if terminal == 'konsole': p = subprocess.Popen([ terminal, '--nofork', '--hold', '-e', "/bin/bash", "-c", "roslaunch %s --port %s %s %s" % (launcher['options'], launcher['port'], launcher['package'], launcher['name']) ], preexec_fn=preexec) elif terminal == 'gnome-terminal.wrapper' or terminal == 'gnome-terminal': # --disable-factory inherits the current environment, bit wierd. p = subprocess.Popen([ 'gnome-terminal', '--disable-factory', '-e', "/bin/bash", "-e", "roslaunch %s --port %s %s %s" % (launcher['options'], launcher['port'], launcher['package'], launcher['name']) ], preexec_fn=preexec) processes.append(p) signal.pause()
def signal_handler(sig, frame): global processes global roslaunch_pids for p in processes: roslaunch_pids.extend(get_roslaunch_pid(p.pid)) # kill roslaunch's for pid in roslaunch_pids: try: os.kill(pid, signal.SIGHUP) except OSError: continue for pid in roslaunch_pids: console.pretty_println("Terminating roslaunch [pid: %d]" % pid, console.bold) wait_pid(pid) #console.pretty_println("Terminated roslaunch [pid: %d]" % pid, console.bold) sleep(1) # now kill konsoles for p in processes: p.terminate()
def prune_resolvable_branches(compatibility_tree, verbosity): if verbosity: print("") compatibility_tree.print_branches("Pruning Resolvable Branches", " ") pruned_branches = [] remaining_branches = [] branches = compatibility_tree.branches removed_leaves = [] for branch in branches: if not branch.leaves: if not pruned_branches: # Only accept one change at a time pruned_branches.append(copy.deepcopy(branch)) else: remaining_branches.append(copy.deepcopy(branch)) elif len(branch.leaves) <= branch.minimum_leaves: if not pruned_branches: pruned_branches.append(copy.deepcopy(branch)) removed_leaves.extend(branch.leaves) else: remaining_branches.append(copy.deepcopy(branch)) else: remaining_branches.append(copy.deepcopy(branch)) removed_leaves = list(set(removed_leaves)) for branch in remaining_branches: branch.prune_leaves(removed_leaves) # are we guaranteed of clearing all of these? # if there was an update and there is work left to do, dig down if pruned_branches: if remaining_branches: if verbosity: console.pretty_println( " --> pruning leaves: %s" % ' '.join([leaf.name for leaf in removed_leaves]), console.green) console.pretty_println( " --> pruned automatically resolvable branches: %s\n" % ' '.join([branch.name() for branch in pruned_branches]), console.green) return pruned_branches, CompatibilityTree( branches=remaining_branches) else: return pruned_branches, None else: if verbosity: console.pretty_println(" --> no resolvable branches: \n", console.green) return [], None
def main(): global processes global roslaunch_pids signal.signal(signal.SIGINT, signal_handler) args = parse_arguments() terminal = None if not args.no_terminals: if not which('konsole') and not which('gnome-terminal') and not which( 'x-terminal-emulator'): console.error( "Cannot find a suitable terminal [x-terminal-emulator, konsole, gnome-termional]" ) sys.exit(1) terminal = choose_terminal(args.gnome, args.konsole) if args.package == '': rocon_launcher = roslaunch.rlutil.resolve_launch_arguments( args.launcher)[0] else: rocon_launcher = roslaunch.rlutil.resolve_launch_arguments( [args.package] + args.launcher)[0] if args.screen: roslaunch_options = "--screen" else: roslaunch_options = "" launchers = parse_rocon_launcher(rocon_launcher, roslaunch_options) temporary_launchers = [] for launcher in launchers: console.pretty_println( "Launching [%s, %s] on port %s" % (launcher['package'], launcher['name'], launcher['port']), console.bold) ########################## # Customise the launcher ########################## temp = tempfile.NamedTemporaryFile(mode='w+t', delete=False) print("Launching %s" % temp.name) launcher_filename = ros_utilities.find_resource( launcher['package'], launcher['name']) launch_text = '<launch>\n <include file="%s"/>\n' % launcher_filename if args.screen: launch_text += ' <param name="rocon/screen" value="true"/>\n' else: launch_text += ' <param name="rocon/screen" value="false"/>\n' launch_text += '</launch>\n' temp.write(launch_text) temp.close() # unlink it later temporary_launchers.append(temp) ########################## # Start the terminal ########################## if terminal == 'konsole': p = subprocess.Popen([ terminal, '--nofork', '--hold', '-e', "/bin/bash", "-c", "roslaunch %s --port %s %s" % (launcher['options'], launcher['port'], temp.name) ], preexec_fn=preexec) elif terminal == 'gnome-terminal.wrapper' or terminal == 'gnome-terminal': # --disable-factory inherits the current environment, bit wierd. p = subprocess.Popen([ 'gnome-terminal', '--disable-factory', '-e', "/bin/bash", "-e", "roslaunch %s --port %s %s" % (launcher['options'], launcher['port'], temp.name) ], preexec_fn=preexec) else: cmd = ["roslaunch"] if launcher['options']: cmd.append(launcher['options']) cmd.extend(["--port", launcher['port'], temp.name]) p = subprocess.Popen(cmd, preexec_fn=preexec) processes.append(p) signal.pause() # Have to unlink them here rather than in the for loop above, because the whole gnome-terminal # subprocess takes a while to kick in (in the background) and the unlinking may occur before # it actually runs the roslaunch that needs the file. for temporary_launcher in temporary_launchers: os.unlink(temporary_launcher.name)
def print_branches(self, name='Branches', indent=''): console.pretty_println(indent + "%s" % name, console.bold) for branch in self.branches: print(indent + " %s" % branch)
def prune_least_valuable_leaf(compatibility_tree, verbosity): ''' This should be only called on a tree with branches that have redundancy (i.e. more potential clients than required (specified by node.min). Does this by scanning the subtree looking for the least visible leaf (client) not including those with visibility one. It also grabs the branches that leaf is on, and there should multiple possible branches, and subsequently chooses to lock it down on the branch with the least redundancy, i.e. pruning it from the rest of the tree. This makes sure that there are plenty of possible options to cover the branches that match this leaf but are not chosen. @param subtree @type [CompatibilityBranch] ''' if verbosity: compatibility_tree.print_branches('Pruning Least Valuable Leaf', ' ') leaf_count = {} # client name - integer count leaves = {} for branch in compatibility_tree.branches: for leaf in branch.leaves: if leaf.name not in leaves: leaves[leaf.name] = leaf leaf_count[leaf.name] = leaf_count[ leaf.name] + 1 if leaf.name in leaf_count else 1 # remove any with count 1. for k, v in leaf_count.items(): if v == 1: del leaf_count[k] if not leaf_count: if verbosity: console.pretty_println( " --> no pruning left to do, unwinding", console.green) # Nothing more to do return None least_visible_count = min(leaf_count.values()) least_visible_leaf_names = [ name for name in leaf_count if leaf_count[name] == least_visible_count ] # let's just take the first one...should I error check here? least_visible_leaf = leaves[least_visible_leaf_names[0]] # branches associated with least_visible_leaf least_visible_branches = [] for branch in compatibility_tree.branches: if least_visible_leaf in branch.leaves: least_visible_branches.append(copy.deepcopy(branch)) # find most valuable branch - using a complicated formula here: # If any of the branches are over their max limits (free_slots < 0) then # make sure we only use that as a criteria (absolutely avoid the most over-maxxed # branches. Otherwise consider a nice balance between redundancy and free slots. # # I might have to add something in here that helps prevent it from actually # choosing something that will make the tree non-redundant. value = lambda b: b.free_slots() if b.free_slots() < 0 else ( 1.0 / b.redundancy()) * min([3, b.free_slots()]) most_valuable_branch = None for branch in least_visible_branches: if most_valuable_branch is None or value(branch) > value( most_valuable_branch): most_valuable_branch = branch pruned_compatibility_tree = copy.deepcopy(compatibility_tree) for branch in pruned_compatibility_tree.branches: if branch.name() != most_valuable_branch.name(): branch.prune_leaves([least_visible_leaf]) if verbosity: console.pretty_println( " --> pruning least visible leaf: %s" % least_visible_leaf.name, console.green) return pruned_compatibility_tree