def test():
  import spyce
  f = open(sys.argv[1])
  spycecode = f.read()
  f.close()
  tokens = spyceTokenize(processMagic(CRLF2LF(spycecode)))
  for type, text, begin, end in tokens:
    print '%s (%s, %s): %s' % (type, begin, end, `text`)
  pythoncode, refs, libs = spyceCompile(spycecode, sys.argv[1], '', spyce.getServer())
  print pythoncode
Beispiel #2
0
def test():
    import spyce
    f = open(sys.argv[1])
    spycecode = f.read()
    f.close()
    tokens = spyceTokenize(processMagic(spycecode))
    for type, text, begin, end in tokens:
        print '%s (%s, %s): %s' % (type, begin, end, ` text `)
    pythoncode, refs, libs = spyceCompile(spycecode, sys.argv[1], '',
                                          spyce.getServer())
    print pythoncode
Beispiel #3
0
def spyceHTTPserver(port, root, config_file=None, daemon=None):
    os.environ[spyce.SPYCE_ENTRY] = 'www'
    spyceCmd.showVersion()
    print '# Starting web server'
    # test for threading support, if needed
    try:
        server = spyce.getServer(config_file=config_file,
                                 overide_www_port=port,
                                 overide_www_root=root)
    except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
        print e
        return
Beispiel #4
0
def spyceHTTPserver(port, root, config_file=None, daemon=None):
  os.environ[spyce.SPYCE_ENTRY] = 'www'
  spyceCmd.showVersion()
  print '# Starting web server'
  # test for threading support, if needed
  try:
    server = spyce.getServer(
      config_file=config_file, 
      overide_www_port=port, 
      overide_www_root=root)
  except (spyceException.spyceForbidden, spyceException.spyceNotFound), e:
    print e
    return
Beispiel #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()
Beispiel #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()