예제 #1
0
    def get(self, path):
        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
            lang = langQS
        elif langCookie:
            lang = langCookie
        else:
            lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
            response = memcache.get(memcacheKey)
            if response:
                logging.info('304 ' + fullPath)

        if response is None:
            try:
                if path.endswith('/') or path == '':
                    response = devsiteIndex.getPage(path, lang)
                else:
                    response = devsitePage.getPage(path, lang)

                if response is None:
                    # No file found, check for redirect
                    redirectTo = devsiteHelper.checkForRedirect(
                        fullPath, lang, USE_MEMCACHE)
                    if redirectTo:
                        logging.info('301 ' + redirectTo)
                        self.redirect(redirectTo, permanent=True)
                        return

                    # No redirect found, send the 404 page.
                    response = render('gae/404.tpl', {'requestPath': fullPath})
                    logging.error('404 ' + fullPath)
                    self.response.set_status(404)
                else:
                    logging.info('200 ' + fullPath)
                    if USE_MEMCACHE:
                        memcache.set(memcacheKey, response)
            except Exception as ex:
                context = {'content': ex, 'requestPath': fullPath}
                response = render('gae/500.tpl', context)
                logging.exception('500 ' + fullPath)
                self.response.set_status(500)

        self.response.out.write(response)
예제 #2
0
    def get(self, path):
        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
          lang = langQS
        elif langCookie:
          lang = langCookie
        else:
          lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
          response = memcache.get(memcacheKey)
          if response:
            logging.info('304 ' + fullPath)

        if response is None:
          try:
            if path.endswith('/') or path == '':
              response = devsiteIndex.getPage(path, lang)
            else:
              response = devsitePage.getPage(path, lang)
          
            if response is None:
              # No file found, check for redirect
              redirectTo = devsiteHelper.checkForRedirect(fullPath, lang, USE_MEMCACHE)
              if redirectTo:
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return

              # No redirect found, send the 404 page.
              response = render('gae/404.tpl', {'requestPath': fullPath})
              logging.error('404 ' + fullPath)
              self.response.set_status(404)
            else:
              logging.info('200 ' + fullPath)
              if USE_MEMCACHE:
                memcache.set(memcacheKey, response)
          except Exception as ex:
            context = {'content': ex, 'requestPath': fullPath}
            response = render('gae/500.tpl', context)
            logging.exception('500 ' + fullPath)
            self.response.set_status(500)

        self.response.out.write(response)
    def get(self, path):

        self.response.headers.add('x-frame-options', 'SAMEORIGIN')

        if path.endswith('.html') or path.endswith('.md'):
          redirectTo = '/web/' + os.path.splitext(path)[0]
          self.redirect(redirectTo, permanent=True)
          return

        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
          lang = langQS
        elif langCookie:
          lang = langCookie
        else:
          lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = self.request.host + fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
          response = memcache.get(memcacheKey)
          if response:
            logging.info('304 ' + fullPath)

        if response is None:
          try:
            if os.path.isdir(os.path.join(SOURCE_PATH, 'en', path)):
              # Make sure the directory ends with a /, as required by devsite
              if len(path) > 0 and not path.endswith('/'):
                redirectTo = '/web/' +  path + '/'
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return
              response = devsiteIndex.getPage(path, lang)
              if (response is None) and (path.startswith('showcase') or
                  path.startswith('shows') or path.startswith('updates')):
                response = devsiteIndex.getDirIndex(path)
            else:
              response = devsitePage.getPage(path, lang)

            if response is None:
              # No file found, check for redirect
              redirectTo = devsiteHelper.checkForRedirect(fullPath, lang, USE_MEMCACHE)
              if redirectTo:
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return

              # No redirect found, send the 404 page.
              response = render('gae/404.tpl', {'requestPath': fullPath})
              logging.error('404 ' + fullPath)
              self.response.set_status(404)
            else:
              logging.info('200 ' + fullPath)
              if USE_MEMCACHE:
                memcache.set(memcacheKey, response)
          except Exception as ex:
            context = {'content': ex, 'requestPath': fullPath}
            response = render('gae/500.tpl', context)
            logging.exception('500 ' + fullPath)
            self.response.set_status(500)

        self.response.out.write(response)
file_count_expected = len(files)

for source_file in files:
    file_count_total += 1
    first_forward = source_file.index('/')
    lang = source_file[0:first_forward]
    kind = 'md'
    if source_file.endswith('_index.yaml'):
        kind = 'yaml'
    source_file = source_file[first_forward + 1:]
    source_file = source_file[0:source_file.rindex('.')]
    dest_file = os.path.join(build_dir, lang, source_file + '.html')

    response = None
    if (kind == 'yaml'):
        response = devsiteIndex.getPage(source_file.replace('_index', ''),
                                        lang)
        dest_file = dest_file.replace('_index', 'index')
    else:
        response = devsitePage.getPage(source_file, lang)

    if response:
        try:
            dest_dir = os.path.dirname(dest_file)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            with io.open(dest_file, 'w', encoding='utf8') as rendered_file:
                rendered_file.write(response)
                rendered_file.close()
            msg = 'OK ' + dest_file + ' (' + str(file_count_total) + ' of '
            msg += str(file_count_expected) + ')'
            print(msg)
예제 #5
0
    def get(self, path):

        self.response.headers.add('x-frame-options', 'SAMEORIGIN')

        if path.endswith('.html') or path.endswith('.md'):
          redirectTo = '/web/' + os.path.splitext(path)[0]
          self.redirect(redirectTo, permanent=True)
          return

        response = None
        langQS = self.request.get('hl', None)
        langCookie = self.request.cookies.get('hl')
        if langQS:
          lang = langQS
        elif langCookie:
          lang = langCookie
        else:
          lang = DEFAULT_LANG
        self.response.set_cookie('hl', lang, max_age=3600, path='/')

        fullPath = self.request.path
        memcacheKey = self.request.host + fullPath + '?hl=' + lang
        logging.info('GET ' + memcacheKey)

        if USE_MEMCACHE:
          response = memcache.get(memcacheKey)
          if response:
            logging.info('304 ' + fullPath)

        if response is None:
          try:
            if os.path.isdir(os.path.join(SOURCE_PATH, 'en', path)):
              # Make sure the directory ends with a /, as required by devsite
              if len(path) > 0 and not path.endswith('/'):
                redirectTo = '/web/' +  path + '/'
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return
              response = devsiteIndex.getPage(path, lang)
              if (response is None) and (path.startswith('showcase') or
                  path.startswith('shows') or path.startswith('updates')):
                response = devsiteIndex.getDirIndex(path)
            else:
              response = devsitePage.getPage(path, lang)

            if response is None:
              # No file found, check for redirect
              redirectTo = devsiteHelper.checkForRedirect(fullPath, lang, USE_MEMCACHE)
              if redirectTo:
                logging.info('301 ' + redirectTo)
                self.redirect(redirectTo, permanent=True)
                return

              # No redirect found, send the 404 page.
              response = render('gae/404.tpl', {'requestPath': fullPath})
              logging.error('404 ' + fullPath)
              self.response.set_status(404)
            else:
              logging.info('200 ' + fullPath)
              if USE_MEMCACHE:
                memcache.set(memcacheKey, response)
          except Exception as ex:
            context = {'content': ex, 'requestPath': fullPath}
            response = render('gae/500.tpl', context)
            logging.exception('500 ' + fullPath)
            self.response.set_status(500)

        self.response.out.write(response)
file_count_expected = len(files)

for source_file in files:
  file_count_total += 1
  first_forward = source_file.index('/')
  lang = source_file[0:first_forward]
  kind = 'md'
  if source_file.endswith('_index.yaml'):
    kind = 'yaml'
  source_file = source_file[first_forward + 1:]
  source_file = source_file[0:source_file.rindex('.')]
  dest_file = os.path.join(build_dir, lang, source_file + '.html')

  response = None
  if (kind == 'yaml'):
    response = devsiteIndex.getPage(source_file.replace('_index', ''), lang)
    dest_file = dest_file.replace('_index', 'index')
  else:
    response = devsitePage.getPage(source_file, lang)

  if response:
    try:
      dest_dir = os.path.dirname(dest_file)
      if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)
      with io.open(dest_file, 'w', encoding='utf8') as rendered_file:
        rendered_file.write(response)
        rendered_file.close()
      msg = 'OK ' + dest_file + ' (' + str(file_count_total) + ' of '
      msg += str(file_count_expected) + ')'
      print(msg)