<commit_before># Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib
import json
import logging
import os
import subprocess
import sys
import time
import urllib
import urllib2

def is_port_listening(port):
  import socket
  s = socket.socket()
  try:
    s.connect(('localhost', port))
  except socket.error:
    return False
  s.close()
  return True

class FrontendDaemonHost(object):
  def __init__(self, port, basedir):
    # If a daemon is running, try killing it via /kill
    if is_port_listening(port):
      for i in range(2):
        logging.warning("Existing daemon found. Asking it to exit")
        try:
          conn = httplib.HTTPConnection('localhost', port, True)
          conn.request('GET', '/exit')
        except:
          break
        res = conn.getresponse()
        if res.status != 200:
          break
        else:
          time.sleep(0.2)

    if is_port_listening(port):
      raise Exception("Daemon running")
    self._port = port
    
    self._daemon_proc = subprocess.Popen([
        sys.executable, "-m", "src.frontend_daemon", str(port), basedir])
    try:
      self._wait_for_daemon_start()
    except Exception, ex:
      import traceback
      traceback.print_exc()
      self.close()
      raise ex

  def _wait_for_daemon_start(self):
    ok = False
    for i in range(10):
      try:
        conn = httplib.HTTPConnection('localhost', self._port, True)
        conn.request('GET', '/ping')
      except:
        time.sleep(0.05)
        continue

      res = conn.getresponse()
      if res.status != 200:
        ok = False
        break
      if res.read() != 'OK':
        ok = False
        break
      ok = True
      break
    if not ok:
      raise Exception("Daemon did not come up")

    self._conn = None
    self._db_proxy = None    

  def close(self):
    try:
      pass
#      conn = httplib.HTTPConnection('localhost', self._port, True)
#      conn.request('GET', '/exit')
    except:
      pass
    t_beginning = time.time()
    while True:
      if self._daemon_proc.poll() is not None:
        break
      seconds_passed = time.time() - t_beginning
      if seconds_passed > 0.5:
        self._daemon_proc.terminate()
        self._daemon_proc.wait()
        break
      time.sleep(0.1)


  @property
  def host(self):
    return 'localhost'

  @property
  def port(self):
    return self._port

  @property
  def baseurl(self):
    return 'http://localhost:%i' % self._port

  def urlread(self, path):
    return urllib2.urlopen(urllib.basejoin(self.baseurl, path)).read()
<commit_msg>Bugfix for webkit-1.0-2 failing to load urls that use http://localhost/<commit_after># Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib
import json
import logging
import os
import socket
import subprocess
import sys
import time
import urllib
import urllib2

def is_port_listening(port):
  import socket
  s = socket.socket()
  try:
    s.connect(('localhost', port))
  except socket.error:
    return False
  s.close()
  return True

class FrontendDaemonHost(object):
  def __init__(self, port, basedir):
    # If a daemon is running, try killing it via /kill
    if is_port_listening(port):
      for i in range(2):
        logging.warning("Existing daemon found. Asking it to exit")
        try:
          conn = httplib.HTTPConnection('localhost', port, True)
          conn.request('GET', '/exit')
        except:
          break
        res = conn.getresponse()
        if res.status != 200:
          break
        else:
          time.sleep(0.2)

    if is_port_listening(port):
      raise Exception("Daemon running")
    self._port = port

    self._daemon_proc = subprocess.Popen([
        sys.executable, "-m", "src.frontend_daemon", str(port), basedir])
    try:
      self._wait_for_daemon_start()
    except Exception, ex:
      import traceback
      traceback.print_exc()
      self.close()
      raise ex

  def _wait_for_daemon_start(self):
    ok = False
    for i in range(10):
      try:
        conn = httplib.HTTPConnection('localhost', self._port, True)
        conn.request('GET', '/ping')
      except:
        time.sleep(0.05)
        continue

      res = conn.getresponse()
      if res.status != 200:
        ok = False
        break
      if res.read() != 'OK':
        ok = False
        break
      ok = True
      break
    if not ok:
      raise Exception("Daemon did not come up")

    self._conn = None
    self._db_proxy = None

  def close(self):
    try:
      pass
#      conn = httplib.HTTPConnection('localhost', self._port, True)
#      conn.request('GET', '/exit')
    except:
      pass
    t_beginning = time.time()
    while True:
      if self._daemon_proc.poll() is not None:
        break
      seconds_passed = time.time() - t_beginning
      if seconds_passed > 0.5:
        self._daemon_proc.terminate()
        self._daemon_proc.wait()
        break
      time.sleep(0.1)


  @property
  def host(self):
    # Report as an IP address because on older webkit builds that use soup, localhost
    # does not work as a URL.
    return socket.gethostbyname('localhost')

  @property
  def port(self):
    return self._port

  @property
  def baseurl(self):
    return 'http://%s:%i' % (self.host, self._port)

  def urlread(self, path):
    return urllib2.urlopen(urllib.basejoin(self.baseurl, path)).read()
{'fraction_non_alphanumeric': 0.10036010646625959, 'fraction_numerical': 0.009237513699702521, 'mean_word_length': 2.535141117874931, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '93559', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1920, 'n_words': 612}