Ejemplo n.º 1
0
  def __init__(self, gcd_sh, working_directory, project_id, deadline,
               start_options, quiet=False):
    """Constructs a local datastore.

    Clients should use LocalCloudDatastoreFactory to construct
    LocalCloudDatastore instances.

    Args:
      gcd_sh: path to gcd.sh
      working_directory: directory file where temporary files will be stored
      project_id: project ID
      deadline: number of seconds to wait for the datastore to start
      start_options: a list of additional command-line options to pass to the
          gcd.sh start command
      quiet: if true, silences the gcd tool output

    Raises:
      IOError: if the datastore failed to start within the deadline
    """
    self._project_id = project_id
    self._gcd_sh = gcd_sh
    self._http = httplib2.Http()
    self.__running = False

    self._tmp_dir = tempfile.mkdtemp(dir=working_directory)
    self._project_directory = os.path.join(self._tmp_dir, self._project_id)
    p = subprocess.Popen([gcd_sh,
                          'create',
                          '--project_id=%s' % self._project_id,
                          self._project_directory])
    if p.wait() != 0:
      raise IOError('could not create project in directory: %s'
                    % self._project_directory)

    # Start GCD and wait for it to start responding to requests.
    port = portpicker.PickUnusedPort()
    self._host = 'http://localhost:%d' % port
    cmd = [self._gcd_sh, 'start', '--port=%d' % port]
    cmd.extend(_DEFAULT_GCD_OPTIONS)
    if start_options:
      cmd.extend(start_options)
    cmd.append(self._project_directory)
    out = open(os.devnull, 'wb') if quiet else None
    subprocess.Popen(cmd, stdout=out, stderr=out)
    if not self._WaitForStartup(deadline):
      raise IOError('datastore did not respond within %ds' % deadline)
    endpoint = '%s/datastore/v1beta3/projects/%s' % (self._host,
                                                     self._project_id)
    self.__datastore = connection.Datastore(project_endpoint=endpoint)
    self.__running = True
Ejemplo n.º 2
0
    def __init__(self, emulator_cmd, working_directory, project_id, deadline,
                 start_options):
        """Constructs a DatastoreEmulator.

    Clients should use DatastoreEmulatorFactory to construct DatastoreEmulator
    instances.

    Args:
      emulator_cmd: path to cloud_datastore_emulator
      working_directory: directory file where temporary files will be stored
      project_id: project ID
      deadline: number of seconds to wait for the datastore to start
      start_options: a list of additional command-line options to pass to the
          emulator 'start' command

    Raises:
      IOError: if the emulator failed to start within the deadline
    """
        self._project_id = project_id
        self._emulator_cmd = emulator_cmd
        self._http = httplib2.Http()
        self.__running = False

        self._tmp_dir = tempfile.mkdtemp(dir=working_directory)
        self._project_directory = os.path.join(self._tmp_dir, self._project_id)
        p = subprocess.Popen([
            emulator_cmd, 'create',
            '--project_id=%s' % self._project_id, self._project_directory
        ])
        if p.wait() != 0:
            raise IOError('could not create project in directory: %s' %
                          self._project_directory)

        # Start the emulator and wait for it to start responding to requests.
        port = portpicker.PickUnusedPort()
        self._host = 'http://localhost:%d' % port
        cmd = [self._emulator_cmd, 'start', '--port=%d' % port]
        cmd.extend(_DEFAULT_EMULATOR_OPTIONS)
        if start_options:
            cmd.extend(start_options)
        cmd.append(self._project_directory)
        subprocess.Popen(cmd)
        if not self._WaitForStartup(deadline):
            raise IOError('emulator did not respond within %ds' % deadline)
        endpoint = '%s/v1/projects/%s' % (self._host, self._project_id)
        self.__datastore = connection.Datastore(project_endpoint=endpoint)
        self.__running = True