예제 #1
0
 def __init__(self, spycewrap):
   self.str = ''
   type, error, _ = sys.exc_info()
   if type is type(SyntaxError):
     raise 'instantiate pythonSyntaxError only when SyntaxError raised: %s' % `type`
   if spycewrap.getCodeRefs().has_key(error.lineno):
     begin, end, text, filename = spycewrap.getCodeRefs()[error.lineno]
     if begin[0]==end[0]:
       linestr = str(begin[0])
     else:
       linestr = '%d-%d' % (begin[0], end[0])
     self.str = 'Python syntax error at %s:%s - %s\n  %s\n' % (filename, linestr, error.msg, text)
   else:
     self.str = spyceUtil.exceptionString()
예제 #2
0
 def __init__(self, spycewrap):
     self.str = ''
     type, error, _ = sys.exc_info()
     if type is type(SyntaxError):
         raise 'instantiate pythonSyntaxError only when SyntaxError raised: %s' % ` type `
     if spycewrap.getCodeRefs().has_key(error.lineno):
         begin, end, text, filename = spycewrap.getCodeRefs()[error.lineno]
         if begin[0] == end[0]:
             linestr = str(begin[0])
         else:
             linestr = '%d-%d' % (begin[0], end[0])
         self.str = 'Python syntax error at %s:%s - %s\n  %s\n' % (
             filename, linestr, error.msg, text)
     else:
         self.str = spyceUtil.exceptionString()
예제 #3
0
#


def spyceMain(apacheRequest):
    "Apache entry point."
    os.environ[spyce.SPYCE_ENTRY] = 'modpy'
    apacheRequest.add_common_vars()
    request = spyceModpyRequest(apacheRequest)
    response = spyceModpyResponse(apacheRequest)
    filename = apacheRequest.filename
    try:
        result = spyce.spyceFileHandler(request,
                                        response,
                                        filename,
                                        config_file=getApacheConfig(
                                            apacheRequest, 'SPYCE_CONFIG',
                                            None))
    except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
        response.clear()
        response.setContentType('text/plain')
        response.write(str(e) + '\n')
    except:
        response.clear()
        response.setContentType('text/plain')
        response.write(spyceUtil.exceptionString() + '\n')
    try:
        response.flush()
    except:
        pass
    return apache.OK
예제 #4
0
#

CONFIG_FILE = None


def spyceMain(apacheRequest):
    "Apache entry point."
    os.environ[spyce.SPYCE_ENTRY] = "modpy"
    apacheRequest.add_common_vars()
    request = spyceModpyRequest(apacheRequest)
    response = spyceModpyResponse(apacheRequest)
    filename = apacheRequest.filename
    global CONFIG_FILE
    if CONFIG_FILE == None:
        CONFIG_FILE = getApacheConfig(apacheRequest, "SPYCE_CONFIG", None)
    try:
        result = spyce.spyceFileHandler(request, response, filename, config_file=CONFIG_FILE)
    except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
        response.clear()
        response.setContentType("text/plain")
        response.write(str(e) + "\n")
    except:
        response.clear()
        response.setContentType("text/plain")
        response.write(spyceUtil.exceptionString() + "\n")
    try:
        response.flush()
    except:
        pass
    return apache.OK
예제 #5
0
def spyceMain(cgimode=0,
              cgiscript=None,
              stdout=sys.stdout,
              stdin=sys.stdin,
              stderr=sys.stderr,
              environ=os.environ):
    "Command-line and CGI entry point."
    # defaults
    compileonlyMode = 0
    outputFilename = None
    defaultOutputFilename = 0
    httpmode = 0
    httpport = None
    httproot = None
    daemon = None
    configFile = None
    # parse options
    if cgimode and cgiscript:
        args = [cgiscript]
    else:
        try:
            opts, args = getopt.getopt(sys.argv[1:], 'h?vco:Owq:ld:p:', [
                'help',
                'version',
                'compile',
                'output=',
                'web',
                'query=',
                'listen',
                'daemon=',
                'port=',
                'conf=',
            ])
        except getopt.error:
            if cgimode:
                stdout.write('Content-Type: text/plain\n\n')
            stdout.write('syntax: unknown switch used\n')
            stdout.write('Use -h option for help.\n')
            return -1
        for o, a in opts:
            if o in ("-v", "--version"):
                showVersion()
                return
            if o in ("-h", "--help", "-?"):
                showUsage()
                return
            if o in ("-c", "--compileonly"):
                compileonlyMode = 1
            if o in ("-o", "--output"):
                outputFilename = a
            if o in ("-O", ):
                defaultOutputFilename = 1
            if o in ("-w", "--web"):
                cgimode = 1
            if o in ("-q", "--query"):
                environ['QUERY_STRING'] = a
            if o in ("-l", "--listen"):
                httpmode = 1
            if o in ("-d", "--daemon"):
                daemon = a
            if o in ("-p", "--port"):
                try:
                    httpport = int(a)
                except:
                    stdout.write('syntax: port must be integer\n')
                    stdout.write('Use -h option for help.\n')
                    return -1
            if o in ("--conf", ):
                configFile = a

    # web server mode
    if httpmode:
        if len(args):
            httproot = args[0]
        import spyceWWW
        return spyceWWW.spyceHTTPserver(httpport,
                                        httproot,
                                        config_file=configFile,
                                        daemon=daemon)
    # some checks
    if not cgimode and not defaultOutputFilename and len(args) > 1:
        stdout.write('syntax: too many files to process\n')
        stdout.write('Use -h option for help.\n')
        return -1
    # file globbing
    if defaultOutputFilename:
        globbed = map(glob.glob, args)
        args = []
        for g in globbed:
            for f in g:
                args.append(f)
    if not len(args):
        if cgimode:
            stdout.write('Content-Type: text/plain\n\n')
        stdout.write('syntax: please specify a spyce file to process\n')
        stdout.write('Use -h option for help.\n')
        return -1
    # run spyce
    result = 0
    try:
        while len(args):
            result = 0
            script = args[0]
            del args[0]
            if cgimode:
                dir = os.path.dirname(script)
                if dir:
                    script = os.path.basename(script)
                    os.chdir(dir)
            try:
                output = stdout
                if defaultOutputFilename:
                    outputFilename = os.path.splitext(script)[0] + '.html'
                    stdout.write('Processing: %s\n' % script)
                    stdout.flush()
                if outputFilename:
                    output = None
                    output = open(outputFilename, 'w')
                if compileonlyMode:
                    s = spyce.getServer().spyce_cache['file', script]
                    output.write(s.getCode())
                    output.write('\n')
                else:
                    request = spyceCmdlineRequest(stdin, environ, script)
                    response = spyceCmdlineResponse(output, stderr, cgimode)
                    result = spyce.spyceFileHandler(request, response, script)
                    response.close()
            except KeyboardInterrupt:
                raise
            except SystemExit:
                pass
            except (spyceException.spyceForbidden,
                    spyceException.spyceNotFound), e:
                if cgimode:
                    stdout.write('Content-Type: text/plain\n\n')
                stdout.write(str(e) + '\n')
            except:
                if cgimode:
                    stdout.write('Content-Type: text/plain\n\n')
                stdout.write(spyceUtil.exceptionString() + '\n')
            if output:
                output.close()
예제 #6
0
def spyceMain(cgimode=0, cgiscript=None, 
    stdout=sys.stdout, stdin=sys.stdin, stderr=sys.stderr, environ=os.environ):
  "Command-line and CGI entry point."
  # defaults
  compileonlyMode = 0
  outputFilename = None
  defaultOutputFilename = 0
  httpmode = 0
  httpport = None
  httproot = None
  daemon = None
  configFile = None
  # parse options
  if cgimode and cgiscript:
    args = [cgiscript]
  else:
    try:
      opts, args = getopt.getopt(sys.argv[1:], 'h?vco:Owq:ld:p:',
        ['help', 'version', 'compile', 'output=', 'web', 
         'query=', 'listen', 'daemon=', 'port=', 'conf=',])
    except getopt.error: 
      if cgimode:
        stdout.write('Content-Type: text/plain\n\n')
      stdout.write('syntax: unknown switch used\n')
      stdout.write('Use -h option for help.\n')
      return -1
    for o, a in opts:
      if o in ("-v", "--version"):
        showVersion(); return
      if o in ("-h", "--help", "-?"):
        showUsage(); return
      if o in ("-c", "--compileonly"):
        compileonlyMode = 1
      if o in ("-o", "--output"):
        outputFilename = a
      if o in ("-O", ):
        defaultOutputFilename = 1
      if o in ("-w", "--web"):
        cgimode = 1
      if o in ("-q", "--query"):
        environ['QUERY_STRING'] = a
      if o in ("-l", "--listen"):
        httpmode = 1
      if o in ("-d", "--daemon"):
        daemon = a
      if o in ("-p", "--port"):
        try: httpport = int(a)
        except:
          stdout.write('syntax: port must be integer\n')
          stdout.write('Use -h option for help.\n')
          return -1
      if o in ("--conf", ):
        configFile = a

  # web server mode
  if httpmode:
    if len(args):
      httproot = args[0]
    import spyceWWW
    return spyceWWW.spyceHTTPserver(httpport, httproot, config_file=configFile, daemon=daemon)
  # some checks
  if not cgimode and not defaultOutputFilename and len(args)>1:
    stdout.write('syntax: too many files to process\n')
    stdout.write('Use -h option for help.\n')
    return -1
  # file globbing
  if defaultOutputFilename:
    globbed = map(glob.glob, args)
    args = []
    for g in globbed:
      for f in g:
        args.append(f)
  if not len(args):
    if cgimode:
      stdout.write('Content-Type: text/plain\n\n')
    stdout.write('syntax: please specify a spyce file to process\n')
    stdout.write('Use -h option for help.\n')
    return -1
  # run spyce
  result=0
  try:
    while len(args):
      result=0
      script = args[0]
      del args[0]
      if cgimode:
        dir = os.path.dirname(script)
        if dir: 
          script = os.path.basename(script)
          os.chdir(dir)
      try:
        output = stdout
        if defaultOutputFilename:
          outputFilename = os.path.splitext(script)[0]+'.html'
          stdout.write('Processing: %s\n'%script)
          stdout.flush()
        if outputFilename:
          output = None
          output = open(outputFilename, 'w')
        if compileonlyMode:
          s = spyce.getServer().spyce_cache['file', script]
          output.write(s.getCode())
          output.write('\n')
        else:
          request = spyceCmdlineRequest(stdin, environ, script)
          response = spyceCmdlineResponse(output, stderr, cgimode)
          result = spyce.spyceFileHandler(request, response, script)
          response.close()
      except KeyboardInterrupt: raise
      except SystemExit: pass
      except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
        if cgimode:
          stdout.write('Content-Type: text/plain\n\n')
        stdout.write(str(e)+'\n')
      except:
        if cgimode:
          stdout.write('Content-Type: text/plain\n\n')
        stdout.write(spyceUtil.exceptionString()+'\n')
      if output:
        output.close()
예제 #7
0
        if DEBUG_ERROR and theError:
          sys.stderr.write(`theError`+'\n')
        if thespyce:
          thespyce.spyceDestroy(theError)
          spycecode.returnWrapper(thespyce)
    except spyceException.spyceDone: pass
    except spyceException.spyceRedirect, e:
      return spyceFileHandler(request, response, e.filename)
    except KeyboardInterrupt: raise
    except (spyceException.spyceNotFound, spyceException.spyceForbidden, 
        spyceException.spyceSyntaxError, spyceException.pythonSyntaxError, 
        SyntaxError), e:
      return self.error(self, request, response, e)
    except SystemExit: pass
    except:
      errorString = spyceUtil.exceptionString()
      try:
        import cgi
        response.clear()
        response.write('<html><pre>\n')
        response.write('Unexpected exception: (please report!)\n')
        response.write(cgi.escape(errorString))
        response.write('\n</pre></html>\n')
        response.returncode = response.RETURN_OK
      except:
        sys.stderr.write(errorString+'\n')
    return response.returncode

class spyceRequest:
  """Underlying Spyce request object. All implementations (CGI, Apache...)
  should subclass and implement the methods marked 'not implemented'."""
예제 #8
0
            print '# Server Port: %d' % server.config.getSpyceWWWPort()
            httpd.documentRoot = os.path.abspath(
                server.config.getSpyceWWWRoot())
            print '# Server Root: ' + httpd.documentRoot
            httpd.mimeTable = buildMimeTable(server.config.getSpyceWWWMime())
            httpd.handler = buildHandlerTable(
                server.config.getSpyceWWWHandler(), myHTTPhandler)
        except:
            print 'Unable to start server on port %s' % server.config.getSpyceWWWPort(
            )
            return -1
        # daemonize
        if daemon:
            print '# Daemonizing process.'
            try:
                spyceCmd.daemonize(pidfile=daemon)
            except SystemExit:  # expected
                return 0
        # process requests
        print '# Ready.'
        while 1:
            try:
                httpd.handle_request()
            except KeyboardInterrupt:
                raise
            except:
                print 'Error: %s' % spyceUtil.exceptionString()
    except KeyboardInterrupt:
        print 'Break!'
    return 0
예제 #9
0
      httpd = sharedSocketServer(('',server.config.getSpyceWWWPort()), myHTTPhandler)
      print '# Server Port: %d' % server.config.getSpyceWWWPort()
      httpd.documentRoot = os.path.abspath(server.config.getSpyceWWWRoot())
      print '# Server Root: '+httpd.documentRoot
      httpd.mimeTable = buildMimeTable(server.config.getSpyceWWWMime())
      httpd.handler = buildHandlerTable(server.config.getSpyceWWWHandler(), myHTTPhandler)
    except:
      print 'Unable to start server on port %s' % server.config.getSpyceWWWPort()
      return -1
    # daemonize
    if daemon:
      print '# Daemonizing process.'
      try:
        spyceCmd.daemonize(pidfile=daemon)
      except SystemExit: # expected
        return 0
      global LOG
      LOG = 0
    # process requests
    print '# Ready.'
    while 1:
      try:
        httpd.handle_request()
      except KeyboardInterrupt: raise
      except:
        print 'Error: %s' % spyceUtil.exceptionString()
  except KeyboardInterrupt:
    print 'Break!'
  return 0

예제 #10
0
        except spyceException.spyceRuntimeException, theError: pass
      finally:
        if thespyce:
          thespyce.spyceDestroy(theError)
          spycecode.returnWrapper(thespyce)
    except spyceException.spyceDone: pass
    except spyceException.spyceRedirect, e:
      return spyceFileHandler(request, response, e.filename)
    except KeyboardInterrupt: raise
    except (spyceException.spyceNotFound, spyceException.spyceForbidden, 
        spyceException.spyceSyntaxError, spyceException.pythonSyntaxError, 
        SyntaxError), e:
      return self.error(self, request, response, e)
    except SystemExit: pass
    except:
      errorString = spyceUtil.exceptionString()
      try:
        import cgi
        response.clear()
        response.write('<html><pre>\n')
        response.write('Unexpected exception: (please report!)\n')
        response.write(cgi.escape(errorString))
        response.write('\n</pre></html>\n')
        response.returncode = response.RETURN_OK
      except:
        sys.stderr.write(errorString+'\n')
    return response.returncode

class spyceRequest:
  """Underlying Spyce request object. All implementations (CGI, Apache...)
  should subclass and implement the methods marked 'not implemented'."""