예제 #1
0
 def get_file(self, ref):
     """
     Get the content of a file from the repo.
     """
     ref = ref.strip('"\'')
     ref = terms.loads(ref)
     return self.get_blob(ref['sha1'])[0]
예제 #2
0
 def rules(self):
     '''List the rules'''
     rules_dict = terms.loads(self.etb().get_rules())
     for file, rules in rules_dict.iteritems():
         print('Rules from file {0}:\n---------'.format(file))
         for rule in rules:
             print('{0}'.format(rule))
예제 #3
0
    def all_claims(self):
        """all_claims: Print all the claims established so far

        Prints a table listing all the claims established up to the present.
        This includes past sessions."""
        cs = terms.loads(self.etb().get_all_claims())
        self.print_claims(cs, 'established so far')
예제 #4
0
 def ping(self, from_id, from_port, to_id, payload=None):
     """
     Called by a remote node to notify that it is still alive.
     Some additional data may be piggybacked in the payload.
     """
     self.add_neighbor(from_id, from_port)
     node = self.neighbor(from_id)
     payload = terms.loads(payload) if payload else {}
     if node:
         assert isinstance(node, ETBNode), 'node not an ETBNode'
         node.touch()
         ip = self._get_incoming_ip()
         if ip is None:
             return
         node.add_host(ip)
         assert node.port == from_port, "port should not change"
         with node._rlock:
             if 'load' in payload:
                 node.load = int(payload['load'])
             if 'predicates' in payload:
                 if node.predicates != payload['predicates']:
                     # Find new ones
                     newpreds = set(payload['predicates']) - node.predicates
                     node.predicates.clear()
                     node.predicates = payload['predicates']
                     self.log.debug('Adding changes to predicates')
                     self.etb.update_predicates(newpreds)
             if 'subscriptions' in payload:
                 node.subscriptions.clear()
                 node.subscriptions.update(payload['subscriptions'])
             if 'to_hosts' in payload:
                 with self._rlock:
                     self._hosts.update(payload['to_hosts'])
     return to_id == self.id
예제 #5
0
 def interpret_goal_remotely(self, from_id, goals, query_id):
     """
     Goal interpretation through a tunnel.
     """
     goals = terms.loads(goals)
     self.log.debug('Remote query: %s for %s' % (goals, from_id))
     node = self.get_link(from_id)
     
     if node is None:
         self.log.error('Remote request unknown link node: %s' % from_id)
         return False
     
     self.log.debug('Remote node: %s', node)
     proxy = node.proxy
     if proxy is None:
         self.log.error('No proxy available for link node: %s' % from_id)
         return False
     self.etb.get_goals_dependencies(goals, proxy)
     def task_interpret(etb, query_id=query_id, goals=goals):
         query = self.etb.create_query((goals,))
         self.query_wait(query)
         answer = terms.dumps(self.etb.query_answers(query))
         def task(etb, query_id=query_id, answer=answer):
             proxy.answer_query(query_id, answer)
         etb.short_pool.schedule(task)
     self.etb.long_pool.schedule(task_interpret)
     return True
예제 #6
0
 def claims(self, q):
     '''claims($q): show claims established by query q'''
     cs = terms.loads(self.etb().query_claims(q))
     self.print_claims(cs, 'established by query %s' % q)
     if len(cs) == 1:
         return terms.dumps(cs[0])
     else:
         return [terms.dumps(c) for c in cs]
예제 #7
0
 def facts(self, all=False):
     '''List the facts'''
     facts_dict = terms.loads(self.etb().get_facts())
     print('facts_dict = {0}'.format(facts_dict))
     for file, facts in facts_dict.iteritems():
         print('Facts from file {0}:\n---------'.format(file))
         for fact in facts:
             print('{0}.'.format(fact))
예제 #8
0
 def answers(self, q):
     '''answers($q): print the answers to a query q'''
     output = self.etb().query_answers(q)
     if output:
         answers = terms.loads(output)
         print('\nAnswers for query %s' % q)
         print('==================================================')
         if answers:
             for s in answers:
                 print(s)
         else:
             print('(no answer)')
         print('')
         return self.translate_answers(output)
예제 #9
0
 def task(etb, neighbor=neighbor):
     """Fetch neighbors task"""
     proxy = neighbor.proxy
     if proxy is not None:
         neighbors = proxy.get_neighbors()
         # parse it as a list
         neighbors = terms.loads(neighbors)
         for nid, hosts, port, timestamp in neighbors:
             if time.time() - timestamp < etb.config.node_timeout:
                 # the neighbor has been seen recently by someone, it
                 # makes sense to add it (if it's not already known)
                 etb.networking.add_neighbor(nid, port)
             nbr = etb.networking.neighbor(nid)
             if nbr:
                 nbr.add_hosts(hosts)
                 # trust the remote node, and use its timestamp
                 nbr.timestamp = timestamp
예제 #10
0
 def add_link(self, nid, to_port, my_port, predicates):
     """Add this node as a new tunnel link."""
     new_node = False
     with self._rlock:
         if nid not in self._links:
             node = ETBNode(self.log, nid, to_port)
             node.my_port = my_port
             node.add_host('localhost')
             node.predicates = terms.loads(predicates) if predicates else {}
             self._links[nid] = node
             new_node = True
         else:
             node = self._links[nid]
             assert node is not None, 'Node is None in add_link'
     if new_node:
         self.log.debug('Connected to tunnel ETB node: %s' % nid)
         self.log.debug('node = %s' % node)
         poke_link(self.etb, node)
예제 #11
0
 def answer_query(self, query_id, answer):
     """
     Add an answer to the query. The answer must be a JSON-encoded
     substitution.
     """
     try:
         self.log.debug('Received JSON answer {0}'.format(answer))
         answer = terms.loads(answer)
         self.log.debug('Received answer to %s' % query_id)
         self.log.debug('answer_query: answer = %s' % answer)
         with self.etb._rlock:
             if query_id in self.etb.active_remote_queries:
                 (goal, nid, argspecs, node) = self.etb.active_remote_queries[query_id]
                 self.log.debug('answer_query: goal = {0}: {1}'.format(goal, type(goal)))
                 self.etb.process_interpreted_goal_answer(argspecs, goal, node, answer)
                 del self.etb.active_remote_queries[query_id]
     except:
         return False
     return True
예제 #12
0
 def add_proxy_link(self, nid, proxy_host, proxy_port, proxy_name, local_name, predicates):
     """Add this node as a new proxy link."""
     new_node = False
     with self._rlock:
         if nid not in self._links:
             node = ETBNode(self.log, nid, proxy_port)
             node.add_host(proxy_host)
             node.proxy_host = proxy_host
             node.remote_name = proxy_name
             node.local_name = local_name
             node.predicates = terms.loads(predicates) if predicates else {}
             self._links[nid] = node
             new_node = True
         else:
             node = self._links[nid]
             assert node is not None, 'Node is None in add_proxy_link'
     if new_node:
         self.log.debug('Connected to proxied ETB node: %s' % nid)
         self.log.debug('node = %s' % node)
         poke_link(self.etb, node)
예제 #13
0
    def get_contents_from_network(self, sha1, seen=None):
        self.log.debug('Being asked over the wire for %s' % sha1)
        contents = None
        execp = False
        #do I have it? 
        if self.etb.git.has_blob(sha1):
            contents = self.etb.git.get_blob(sha1)[0]
    
        if contents is not None and contents is not '':
            self.log.debug('I have %s!' % sha1)
            execp = False
        else:
            seen = terms.loads(seen) if seen else []
            contents, execp = self.get_contents_from_somewhere(sha1, seen)

        if contents is not None and contents is not '':
            b64_contents = base64.b64encode(contents)
        else:
            b64_contents = ''
        return b64_contents, execp
예제 #14
0
    def errors(self, q):
        '''errors($q): print error claims of query q

        If a query generates an error, e.g., a wrapper throws an exception,
        then a special error claim is created.  This command lists just the
        error claims, if any, for query q.
        '''
        output = self.etb().query_claims(q)
        if output:
            answers = terms.loads(output)
            answers = [
                a for a in answers if isinstance(a, terms.Literal)
                and a.first_symbol() == terms.mk_idconst('error')
            ]
            if len(answers) > 0:
                print('\nErrors:')
                print('======')
                for a in answers:
                    print(a)
            else:
                print('No errors')
예제 #15
0
    def interpret_goal(self, from_id, goals, query_id):
        """
        Given the goal (a JSON-encoded list of possibly interpret terms),
        schedule it to be interpreted by a tool wrapper and to send back the
        results to from_id.

        Returns True on success, False on failure.
        """
        goals = terms.loads(goals)
        self.log.debug('Asked by %s to interpret %s', from_id, goals)
        def task_interpret(etb, query_id=query_id, goals=goals):
            query = self.etb.create_query((goals,))
            self.query_wait(query)
            if False:
                filename = self.etb.engine.goal_deps_to_png(goals)
                os.rename(filename, "%s.png" % goals)
                self.log.debug("answering_png.png for %s dumped to %s" % (goals, os.getcwd()))
            ans = self.etb.query_answers(query)
            answer = terms.dumps(ans)
            self.log.debug('task_interpret: answer to query is: %s' % answer)
            def task(etb, query_id=query_id, answer=answer):
                if from_id in self._neighbors:
                    proxy = self.neighbor(from_id).proxy
                else:
                    print('setting proxy to None')
                    proxy = None
                if proxy is not None:
                    self.log.debug('Sending query answer to %s', from_id)
                    # I don't know why, but at some point the following
                    # started raising the exception printing proxy:
                    #   method "__unicode__" is not supported
                    #self.log.info('proxy for %s is %s' % (from_id, proxy))
                    self.log.debug('_neighbors = %s' % self._neighbors)
                    proxy.answer_query(query_id, answer)
            etb.short_pool.schedule(task)
        self.etb.long_pool.schedule(task_interpret)
        return True
예제 #16
0
 def all_claims(self):
     '''Loads the JSON result of the all_claims method'''
     claims = self.etb().all_claims()
     return sorted(terms.loads(claims))
예제 #17
0
 def query_claims(self, query):
     '''Loads the JSON result of the query_claims method'''
     claims = self.etb().query_claims(query)
     return terms.loads(claims)
예제 #18
0
 def find_claims(self, pattern, reasons=False):
     """find_claims: Find claims matching pattern"""
     cs = terms.loads(self.etb().find_claims(pattern, reasons))
     return cs
예제 #19
0
 def query_answers(self, query):
     '''Loads the JSON result of the query_answers method'''
     answers = self.etb().query_answers(query)
     if answers:
         return terms.loads(answers)
예제 #20
0
 def translate_answers(self, output):
     # print('translate_answers: output = %s of type %s' % (output, type(output))))
     substs = terms.loads(output)
     return substs