#!/usr/bin/env python # -*- coding: utf-8 -*- import os from social_computing import crawl_social_graph import cgi #import cgitb; cgitb.enable() class output_file(file): def write(self, string): super(output_file, self).write(string) self.flush() def onexit(): # RUNNING_LOG_PROXY = open(RUNNING_LOG.name) RUNNING_LOG2 = open(WRITABLE_DIRECTORY + "running_log.html", 'a') RUNNING_LOG.seek(0) RUNNING_LOG2.write(RUNNING_LOG.read()) # RUNNING_LOG_PROXY.close() RUNNING_LOG2.close() RESULT_LOG2 = open(WRITABLE_DIRECTORY + "result_log.txt", 'w') RESULT_LOG.seek(0) RESULT_LOG2.write(RESULT_LOG.read()) # RUNNING_LOG_PROXY.close() RESULT_LOG2.close() POINTER_LOG = open(WRITABLE_DIRECTORY + "pointer.txt", 'a') POINTER_LOG.write(WRITABLE_DIRECTORY + "running_log.html" + '\n') POINTER_LOG.write(WRITABLE_DIRECTORY + "result_log.txt" + '\n') POINTER_LOG.close() # def close(self): # self.file.close() # RUNNIN_LOG.close = close # RESULT_LOG.close = close RUNNING_LOG.close() RESULT_LOG.close() import tempfile #a place where the web server has the write access WRITABLE_DIRECTORY = "/afs/csail.mit.edu/group/dig/www/data/2008/webdav/tms_web/" #this stores pointers to the temporary files, used in fetch_result.cgi POINTER_LOG = open(WRITABLE_DIRECTORY + "pointer.txt", 'a') RUNNING_LOG = tempfile.NamedTemporaryFile(mode='a', bufsize=0) RESULT_LOG = tempfile.NamedTemporaryFile(mode='w', bufsize=0) #RUNNING_LOG = output_file("running_log.html", 'a') #RESULT_LOG = output_file("result_log.txt", 'w') POINTER_LOG.write(RUNNING_LOG.name + '\n') POINTER_LOG.write(RESULT_LOG.name + '\n') POINTER_LOG.close() os.system("chmod 644 %s %s" % (RUNNING_LOG.name, RESULT_LOG.name)) fields = cgi.FieldStorage() seed = fields['seed'].value instruction_text = fields['instructions'].value from Knowledge_Closure.util import send_initial_document send_initial_document(str(os.getpid())) import sys ## Redirect stdout and stderr to the right place sys.stderr = RUNNING_LOG sys.stdout = RESULT_LOG def except_hook(type, value, traceback): sys.__excepthook__(type, value, traceback) onexit() sys.excepthook = except_hook #if an error occurs after this point, #copy the error back RUNNING_LOG.write("seed: " + seed + '\n') RUNNING_LOG.write("instructions: \n" + instruction_text + '\n') from swap import notation3 from swap.myStore import formula, symbol ## parsing N3 from text asIfFrom = "file:///afs/csail.mit.edu/u/k/kennyluck/public_html/SVN-DIG/Papers/SocialComp/code/same_interest.n3" F = formula() p = notation3.SinkParser(F.store, F, thisDoc=asIfFrom) p.startDoc() p.feed(instruction_text) p.endDoc() ## main crawl_social_graph(symbol(seed), F) ## At this point, the crawling unblievably ends. The temp file will get erased # so copy the file to other place onexit()