HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/394/lib/googlecloudsdk/command_lib/emulators/firestore_util.py
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# 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.
"""Utility functions for gcloud firestore emulator."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import os
from googlecloudsdk.command_lib.emulators import util
from googlecloudsdk.core import execution_utils
from googlecloudsdk.core import log
from googlecloudsdk.core.util import platforms


def ArgsForFirestoreEmulator(emulator_args):
  """Constructs an argument list for calling the Firestore emulator.

  Args:
    emulator_args: args for the emulator.

  Returns:
    An argument list to execute the Firestore emulator.
  """
  current_os = platforms.OperatingSystem.Current()
  if current_os is platforms.OperatingSystem.WINDOWS:
    cmd = 'cloud_firestore_emulator.cmd'
    exe = os.path.join(util.GetEmulatorRoot(CLOUD_FIRESTORE), cmd)
    return execution_utils.ArgsForCMDTool(exe, *emulator_args)
  else:
    cmd = 'cloud_firestore_emulator'
    exe = os.path.join(util.GetEmulatorRoot(CLOUD_FIRESTORE), cmd)
    return execution_utils.ArgsForExecutableTool(exe, *emulator_args)


FIRESTORE = 'firestore'
CLOUD_FIRESTORE = 'cloud-firestore'
FIRESTORE_TITLE = 'Google Cloud Firestore emulator'


def StartFirestoreEmulator(args, log_file=None):
  """Starts the firestore emulator with the given arguments.

  Args:
    args: Arguments passed to the start command.
    log_file: optional file argument to reroute process's output.

  Returns:
    process, The handle of the child process running the datastore emulator.
  """
  start_args = ['start']
  start_args.append('--host={0}'.format(args.host_port.host))
  start_args.append('--port={0}'.format(args.host_port.port))
  if args.rules:
    start_args.append('--rules={0}'.format(args.rules))
  if args.use_firestore_in_datastore_mode:
    start_args.append('--database-mode=datastore-mode')
  else:
    start_args.append('--database-mode={0}'.format(args.database_mode))
  if args.import_data:
    start_args.append('--import-data={0}'.format(args.import_data))
  if args.export_on_exit:
    start_args.append('--export-on-exit={0}'.format(args.export_on_exit))
  if args.licenses:
    start_args.append('--licenses')
  exec_args = ArgsForFirestoreEmulator(start_args)

  log.status.Print('Executing: {0}'.format(' '.join(exec_args)))
  return util.Exec(exec_args, log_file=log_file)


def ValidateStartArgs(args):  # pylint: disable=unused-argument
  pass


def GetHostPort():
  return util.GetHostPort(FIRESTORE)


class FirestoreEmulator(util.Emulator):
  """Represents the ability to start and route firestore emulator."""

  def Start(self, port):
    args = util.AttrDict({
        'host_port': {
            'host': 'localhost',
            'port': port,
        }
    })
    return StartFirestoreEmulator(args, self._GetLogNo())

  @property
  def prefixes(self):
    return ['google.firestore']

  @property
  def service_name(self):
    return FIRESTORE

  @property
  def emulator_title(self):
    return FIRESTORE_TITLE

  @property
  def emulator_component(self):
    return 'cloud-firestore-emulator'