def Func(arg_0, arg_1):
        """
        decode agents jsons, count diffs
        """
        arg_2 = []
        if arg_1:
            for arg_3 in arg_1.split('\n'):
                try:
                    if arg_3:
                        arg_4 = {}
                        arg_5 = json.loads(arg_3)
                        for arg_6, arg_7 in arg_5.iteritems():
                            for arg_8, arg_9 in arg_7.iteritems():
                                # key sample: diskio-sda1_io_time
                                # key_group sample: diskio
                                # key_name sample: io_time
                                try:
                                    arg_10, arg_11 = arg_8.split('_')[0].split('-')[0], '_'.join(arg_8.split('_')[1:])
                                except:  # noqa: E722
                                    arg_10, arg_11 = arg_8.split('_')[0], '_'.join(arg_8.split('_')[1:])
                                if arg_10 in decoder.diff_metrics.keys():
                                    if arg_11 in decoder.diff_metrics[arg_10]:
                                        arg_12 = decoder.find_common_names(
                                            arg_8)
                                        if arg_0.prev_check:
                                            try:
                                                arg_9 = arg_5[arg_6][arg_8] - \
                                                    arg_0.prev_check[arg_8]
                                            except KeyError:
                                                logger.debug(
                                                    'There is no diff value for metric %s.\n'
                                                    'Timestamp: %s. Is it initial data?', arg_8, arg_6, exc_info=True)
                                                arg_9 = 0
                                            arg_4[arg_12] = arg_9
                                    else:
                                        arg_12 = decoder.find_common_names(
                                            arg_8)
                                        arg_4[arg_12] = arg_9
                                else:
                                    arg_12 = decoder.find_common_names(
                                        arg_8)
                                    arg_4[arg_12] = arg_9
                            arg_0.prev_check = arg_5[arg_6]
                            arg_2.append((arg_6, arg_4))
                except ValueError:
                    logger.error(
                        'Telegraf agent send trash to output: %s', arg_3)
                    logger.debug(
                        'Telegraf agent data block w/ trash: %s',
                        exc_info=True)
                    return []
                except BaseException:
                    logger.error(
                        'Exception trying to parse agent data: %s',
                        arg_3,
                        exc_info=True)
                    return []
            if arg_2:
                return arg_2


def Func(arg_0, arg_1, arg_2, arg_3, arg_4):
        '''
        Perform one request, possibly raising RetryException in the case
        the response is 429. Otherwise, if error text contain "code" string,
        then it decodes to json object and returns APIError.
        Returns the body json in the 200 status.
        '''
        arg_5 = arg_0._retry_codes
        arg_6 = arg_0._session.request(arg_1, arg_2, **arg_3)
        try:
            arg_6.raise_for_status()
        except HTTPError as http_error:
            # retry if we hit Rate Limit
            if arg_6.status_code in arg_5 and arg_4 > 0:
                raise RetryException()
            if 'code' in arg_6.text:
                arg_7 = arg_6.json()
                if 'code' in arg_7:
                    raise APIError(arg_7, http_error)
            else:
                raise
        if arg_6.text != '':
            return arg_6.json()
        return None


def Func(arg_0, arg_1, arg_2, arg_3, arg_4, arg_5,
                     arg_6=None, arg_7=None, arg_8=None):
        '''Request a new order'''
        arg_9 = {
            'symbol': arg_1,
            'qty': arg_2,
            'side': arg_3,
            'type': arg_4,
            'time_in_force': arg_5,
        }
        if arg_6 is not None:
            arg_9['limit_price'] = arg_6
        if arg_7 is not None:
            arg_9['stop_price'] = arg_7
        if arg_8 is not None:
            arg_9['client_order_id'] = arg_8
        arg_10 = arg_0.post('/orders', arg_9)
        return Order(arg_10)


def Func(arg_0, arg_1):
        '''Get an order'''
        arg_2 = arg_0.get('/orders/{}'.format(arg_1))
        return Order(arg_2)


def Func(arg_0, arg_1):
    """
    Result may be a python dictionary, array or a primitive type
    that can be converted to JSON for writing back the result.
    """
    arg_2 = arg_0.make_success_response(arg_1)
    arg_3 = time.time()
    arg_4 = arg_3 - arg_0.basehandler_starttime
    arg_2[arg_5.RESPONSE_KEY_EXECUTION_TIME] = arg_4
    arg_0.write_json_response(arg_2)


def Func(arg_0, arg_1):
    """
    Writes the message as part of the response and sets 404 status.
    """
    arg_0.set_status(404)
    arg_2 = arg_0.make_error_response(str(arg_1))
    arg_3 = time.time()
    arg_4 = arg_3 - arg_0.basehandler_starttime
    arg_2[arg_5.RESPONSE_KEY_EXECUTION_TIME] = arg_4
    arg_0.write_json_response(arg_2)


def Func(arg_0, arg_1):
    """
    Makes the base dict for the response.
    The status is the string value for
    the key "status" of the response. This
    should be "success" or "failure".
    """
    arg_2 = {
        constants.RESPONSE_KEY_STATUS: arg_1,
        constants.RESPONSE_KEY_VERSION: constants.API_VERSION,
        constants.RESPONSE_KEY_EXECUTION_TIME: 0,
        constants.RESPONSE_KEY_MESSAGE: "",
    }
    return arg_2


def Func(arg_0, arg_1):
    """
    Makes the python dict corresponding to the
    JSON that needs to be sent for a successful
    response. Result is the actual payload
    that gets sent.
    """
    arg_2 = arg_0.make_response(arg_3.RESPONSE_STATUS_SUCCESS)
    arg_2[arg_3.RESPONSE_KEY_RESULT] = arg_1
    return arg_2


def Func(arg_0):
    """
    Helper function to get request argument.
    Raises exception if argument is missing.
    Returns the cluster argument.
    """
    try:
      return arg_0.get_argument(constants.PARAM_CLUSTER)
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get request argument.
    Raises exception if argument is missing.
    Returns the role argument.
    """
    try:
      return arg_0.get_argument(constants.PARAM_ROLE, default=None)
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get request argument.
    Raises exception if argument is missing.
    Returns the environ argument.
    """
    try:
      return arg_0.get_argument(constants.PARAM_ENVIRON)
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get topology argument.
    Raises exception if argument is missing.
    Returns the topology argument.
    """
    try:
      arg_1 = arg_0.get_argument(constants.PARAM_TOPOLOGY)
      return arg_1
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get starttime argument.
    Raises exception if argument is missing.
    Returns the starttime argument.
    """
    try:
      arg_1 = arg_0.get_argument(constants.PARAM_STARTTIME)
      return arg_1
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get endtime argument.
    Raises exception if argument is missing.
    Returns the endtime argument.
    """
    try:
      arg_1 = arg_0.get_argument(constants.PARAM_ENDTIME)
      return arg_1
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get length argument.
    Raises exception if argument is missing.
    Returns the length argument.
    """
    try:
      arg_1 = arg_0.get_argument(constants.PARAM_LENGTH)
      return arg_1
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """
    Helper function to get metricname arguments.
    Notice that it is get_argument"s" variation, which means that this can be repeated.
    Raises exception if argument is missing.
    Returns a list of metricname arguments
    """
    try:
      arg_1 = arg_0.get_arguments(constants.PARAM_METRICNAME)
      if not arg_1:
        raise tornado.web.MissingArgumentError(constants.PARAM_METRICNAME)
      return arg_1
    except tornado.web.MissingArgumentError as e:
      raise Exception(e.log_message)


def Func(arg_0):
    """Tries to connect to the Heron Server

    ``loop()`` method needs to be called after this.
    """
    Log.debug("In Func() of %s" % arg_0._get_classname())
    # TODO: specify buffer size, exception handling
    arg_0.create_socket(socket.AF_INET, socket.SOCK_STREAM)

    # when ready, handle_connect is called
    arg_0._connecting = True
    arg_0.connect(arg_0.endpoint)


def Func(arg_0, arg_1):
    """Registers protobuf message builders that this client wants to receive

    :param msg_builder: callable to create a protobuf message that this client wants to receive
    """
    arg_2 = arg_1()
    Log.debug("In Func(): %s" % arg_2.DESCRIPTOR.full_name)
    arg_0.registered_message_map[arg_2.DESCRIPTOR.full_name] = arg_1


def Func():
  """
  This will extract heron directory from .pex file.

  For example,
  when __file__ is '/Users/heron-user/bin/heron/heron/tools/common/src/python/utils/config.pyc', and
  its real path is '/Users/heron-user/.heron/bin/heron/tools/common/src/python/utils/config.pyc',
  the internal variable ``path`` would be '/Users/heron-user/.heron', which is the heron directory

  This means the variable `go_above_dirs` below is 9.

  :return: root location of the .pex file
  """
  arg_0 = 9
  arg_1 = "/".join(os.path.realpath(__file__).split('/')[:-arg_0])
  return normalized_class_path(arg_1)


def Func(arg_0):
  """
  if role is not provided, supply userid
  if environ is not provided, supply 'default'
  """
  if len(arg_0[1]) == 0 and len(arg_0[2]) == 0:
    return (arg_0[0], getpass.getuser(), ENVIRON)

  return (arg_0[0], arg_0[1], arg_0[2])


def Func(arg_0):
  """
  Parse the command line for overriding the defaults and
  create an override file.
  """
  arg_1 = parse_override_config(arg_0)
  try:
    arg_2 = tempfile.mkdtemp()
    arg_3 = os.path.join(arg_2, OVERRIDE_YAML)
    with open(arg_3, 'w') as f:
      f.write(yaml.dump(arg_1))

    return arg_3
  except Exception as e:
    raise Exception("Failed to parse override config: %s" % str(e))


def Func():
  """Get the path of java executable"""
  arg_0 = os.environ.get("JAVA_HOME")
  return os.path.join(arg_0, BIN_DIR, "java")


def Func():
  """Check if the release.yaml file exists"""
  arg_0 = get_heron_release_file()

  # if the file does not exist and is not a file
  if not os.path.isfile(arg_0):
    Log.error("Required file not found: %s" % arg_0)
    return False

  return True


def Func(arg_0=False):
  """Print version from release.yaml

  :param zipped_pex: True if the PEX file is built with flag `zip_safe=False'.
  """
  if arg_0:
    arg_1 = get_zipped_heron_release_file()
  else:
    arg_1 = get_heron_release_file()
  with open(arg_1) as release_info:
    for arg_2 in release_info:
      arg_3 = arg_2[:-1].split(' ')
      if arg_3[0] == 'heron.build.version':
        return arg_3[-1].replace("'", "")
    return 'unknown'


def Func(arg_0, arg_1):
    """
    Returns the UUID with which the watch is
    registered. This UUID can be used to unregister
    the watch.
    Returns None if watch could not be registered.

    The argument 'callback' must be a function that takes
    exactly one argument, the topology on which
    the watch was triggered.
    Note that the watch will be unregistered in case
    it raises any Exception the first time.

    This callback is also called at the time
    of registration.
    """
    arg_2 = 5
    # Retry in case UID is previously
    # generated, just in case...
    for arg_3 in range(arg_2):
      # Generate a random UUID.
      arg_4 = uuid.uuid4()
      if arg_4 not in arg_0.watches:
        Log.info("Registering a watch with uid: " + str(arg_4))
        try:
          arg_1(arg_0)
        except Exception as e:
          Log.error("Caught exception while triggering callback: " + str(e))
          Log.debug(traceback.format_exc())
          return None
        arg_0.watches[arg_4] = arg_1
        return arg_4
    return None


def Func(arg_0, arg_1):
    """
    Unregister the watch with the given UUID.
    """
    # Do not raise an error if UUID is
    # not present in the watches.
    Log.info("Unregister a watch with uid: " + str(arg_1))
    arg_0.watches.pop(arg_1, None)


def Func(arg_0):
    """
    Call all the callbacks.
    If any callback raises an Exception,
    unregister the corresponding watch.
    """
    arg_1 = []
    for arg_2, arg_3 in arg_0.watches.items():
      try:
        arg_3(arg_0)
      except Exception as e:
        Log.error("Caught exception while triggering callback: " + str(e))
        Log.debug(traceback.format_exc())
        arg_1.append(arg_2)

    for arg_2 in arg_1:
      arg_0.unregister_watch(arg_2)


def Func(arg_0, arg_1):
    """ set physical plan """
    if not arg_1:
      arg_0.physical_plan = None
      arg_0.id = None
    else:
      arg_0.physical_plan = arg_1
      arg_0.id = arg_1.topology.id
    arg_0.trigger_watches()


def Func(arg_0, arg_1):
    """ set packing plan """
    if not arg_1:
      arg_0.packing_plan = None
      arg_0.id = None
    else:
      arg_0.packing_plan = arg_1
      arg_0.id = arg_1.id
    arg_0.trigger_watches()


def Func(arg_0, arg_1):
    """ set exectuion state """
    if not arg_1:
      arg_0.execution_state = None
      arg_0.cluster = None
      arg_0.environ = None
    else:
      arg_0.execution_state = arg_1
      arg_2, arg_3 = arg_0.get_execution_state_dc_environ(arg_1)
      arg_0.cluster = arg_2
      arg_0.environ = arg_3
      arg_0.zone = arg_2
    arg_0.trigger_watches()


def Func(arg_0):
    """
    Number of spouts + bolts
    """
    arg_1 = 0

    # Get all the components
    arg_2 = arg_0.spouts() + arg_0.bolts()

    # Get instances for each worker
    for arg_3 in arg_2:
      arg_4 = arg_3.comp.config
      for arg_5 in arg_4.kvs:
        if arg_5.key == api_constants.TOPOLOGY_COMPONENT_PARALLELISM:
          arg_1 += int(arg_5.value)
          break

    return arg_1


def Func(arg_0):
    """
    Get the current state of this topology.
    The state values are from the topology.proto
    RUNNING = 1, PAUSED = 2, KILLED = 3
    if the state is None "Unknown" is returned.
    """
    arg_1 = None
    if arg_0.physical_plan and arg_0.physical_plan.topology:
      arg_1 = arg_0.physical_plan.topology.state

    if arg_1 == 1:
      return "Running"
    elif arg_1 == 2:
      return "Paused"
    elif arg_1 == 3:
      return "Killed"
    else:
      return "Unknown"


def Func(arg_0):
    """
    Sync the topologies with the statemgrs.
    """
    arg_0.state_managers = statemanagerfactory.get_all_state_managers(arg_0.config.statemgr_config)
    try:
      for arg_2 in arg_0.state_managers:
        arg_2.start()
    except Exception as ex:
      Log.error("Found exception while initializing state managers: %s. Bailing out..." % ex)
      traceback.print_exc()
      sys.exit(1)

    # pylint: disable=deprecated-lambda
    def on_topologies_watch(arg_2, arg_3):
      """watch topologies"""
      Log.info("State watch triggered for topologies.")
      Log.debug("Topologies: " + str(arg_3))
      arg_4 = arg_0.getTopologiesForStateLocation(arg_2.name)
      arg_5 = map(lambda t: t.name, arg_4)
      Log.debug("Existing topologies: " + str(arg_5))
      for arg_6 in arg_5:
        if arg_6 not in arg_3:
          Log.info("Removing topology: %s in rootpath: %s",
                   arg_6, arg_2.rootpath)
          arg_0.removeTopology(arg_6, arg_2.name)

      for arg_6 in arg_3:
        if arg_6 not in arg_5:
          arg_0.addNewTopology(arg_2, arg_6)

    for arg_2 in arg_0.state_managers:
      # The callback function with the bound
      # state_manager as first variable.
      arg_7 = partial(on_topologies_watch, arg_2)
      arg_2.get_topologies(arg_7)


def Func(arg_0, arg_1):
    """
    Returns all the topologies for a given state manager.
    """
    return filter(lambda t: t.state_manager_name == arg_1, arg_0.topologies)


def Func(arg_0, arg_1):
    """
    Returns the repesentation of execution state that will
    be returned from Tracker.
    """
    arg_2 = arg_1.execution_state

    arg_3 = {
        "cluster": arg_2.cluster,
        "environ": arg_2.environ,
        "role": arg_2.role,
        "jobname": arg_1.name,
        "submission_time": arg_2.submission_time,
        "submission_user": arg_2.submission_user,
        "release_username": arg_2.release_state.release_username,
        "release_tag": arg_2.release_state.release_tag,
        "release_version": arg_2.release_state.release_version,
        "has_physical_plan": None,
        "has_tmaster_location": None,
        "has_scheduler_location": None,
        "extra_links": [],
    }

    for arg_4 in arg_0.config.extra_links:
      arg_5 = arg_4.copy()
      arg_5["url"] = arg_0.config.get_formatted_url(arg_3,
                                                  arg_5[EXTRA_LINK_FORMATTER_KEY])
      arg_3["extra_links"].append(arg_5)
    return arg_3


def Func(arg_0, arg_1):
    """
    Returns the representation of scheduler location that will
    be returned from Tracker.
    """
    arg_2 = {
        "name": None,
        "http_endpoint": None,
        "job_page_link": None,
    }

    if arg_1.scheduler_location:
      arg_2["name"] = arg_1.scheduler_location.topology_name
      arg_2["http_endpoint"] = arg_1.scheduler_location.http_endpoint
      arg_2["job_page_link"] = \
          arg_1.scheduler_location.job_page_link[0] \
          if len(arg_1.scheduler_location.job_page_link) > 0 else ""

    return arg_2


def Func(arg_0, arg_1):
    """
    Returns the representation of tmaster that will
    be returned from Tracker.
    """
    arg_2 = {
        "name": None,
        "id": None,
        "host": None,
        "controller_port": None,
        "master_port": None,
        "stats_port": None,
    }
    if arg_1.tmaster:
      arg_2["name"] = arg_1.tmaster.topology_name
      arg_2["id"] = arg_1.tmaster.topology_id
      arg_2["host"] = arg_1.tmaster.host
      arg_2["controller_port"] = arg_1.tmaster.controller_port
      arg_2["master_port"] = arg_1.tmaster.master_port
      arg_2["stats_port"] = arg_1.tmaster.stats_port

    return arg_2


def Func(arg_0, arg_1):
    """validate extra link"""
    if EXTRA_LINK_NAME_KEY not in arg_1 or EXTRA_LINK_FORMATTER_KEY not in arg_1:
      raise Exception("Invalid extra.links format. " +
                      "Extra link must include a 'name' and 'formatter' field")

    arg_0.validated_formatter(arg_1[EXTRA_LINK_FORMATTER_KEY])
    return arg_1


def Func(arg_0, arg_1, arg_2=None, arg_3=arg_4.DEFAULT_STREAM_ID,
           arg_6=None, arg_7=False):
    """Emits a new tuple from this Spout

    It is compatible with StreamParse API.

    :type tup: list or tuple
    :param tup: the new output Tuple to send from this spout,
                should contain only serializable data.
    :type tup_id: str or object
    :param tup_id: the ID for the Tuple. Leave this blank for an unreliable Func.
                   (Same as messageId in Java)
    :type stream: str
    :param stream: the ID of the stream this Tuple should be Functed to.
                   Leave empty to Func to the default stream.
    :type direct_task: int
    :param direct_task: the task to send the Tuple to if performing a direct Func.
    :type need_task_ids: bool
    :param need_task_ids: indicate whether or not you would like the task IDs the Tuple was Functed.
    """
    # first check whether this tuple is sane
    arg_0.pplan_helper.check_output_schema(arg_3, arg_1)

    # get custom grouping target task ids; get empty list if not custom grouping
    arg_8 = arg_0.pplan_helper.choose_tasks_for_custom_grouping(arg_3, arg_1)

    arg_0.pplan_helper.context.invoke_hook_Func(arg_1, arg_3, None)

    arg_9 = tuple_pb2.HeronDataTuple()
    arg_9.key = 0

    if arg_6 is not None:
      if not isinstance(arg_6, int):
        raise TypeError("direct_task argument needs to be an integer, given: %s"
                        % str(type(arg_6)))
      # performing Func-direct
      arg_9.dest_task_ids.append(arg_6)
    elif arg_8 is not None:
      # for custom grouping
      for arg_11 in arg_8:
        arg_9.dest_task_ids.append(arg_11)

    if arg_2 is not None:
      arg_12 = TupleHelper.make_root_tuple_info(arg_3, arg_2)
      if arg_0.acking_enabled:
        # this message is rooted
        arg_13 = arg_9.roots.add()
        arg_13.taskid = arg_0.pplan_helper.my_task_id
        arg_13.key = arg_12.key
        arg_0.in_flight_tuples[arg_12.key] = arg_12
      else:
        arg_0.immediate_acks.append(arg_12)

    arg_16 = 0

    arg_17 = time.time()

    # Serialize
    for arg_18 in arg_1:
      arg_19 = arg_0.serializer.serialize(arg_18)
      arg_9.values.append(arg_19)
      arg_16 += len(arg_19)

    arg_20 = (time.time() - arg_17) * system_constants.SEC_TO_NS
    arg_0.spout_metrics.serialize_data_tuple(arg_3, arg_20)

    super(SpoutInstance, arg_0).admit_data_tuple(stream_id=arg_3, arg_9=arg_9,
                                                arg_16=arg_16)
    arg_0.total_tuples_Functed += 1
    arg_0.spout_metrics.update_Func_count(arg_3)
    if arg_7:
      arg_21 = arg_8 or []
      if arg_6 is not None:
        arg_21.append(arg_6)
      return arg_21


def Func(arg_0, arg_1):
  """ normalize raw logical plan info to table """
  arg_2, arg_3 = defaultdict(list), defaultdict(list)
  for arg_4, arg_5 in arg_0.items():
    if arg_4 == 'bolts':
      for arg_6, arg_7 in arg_5.items():
        for arg_8 in arg_7['inputs']:
          arg_9 = arg_8['component_name']
          arg_2[arg_6].append(arg_9)
          arg_3[arg_9].append(arg_6)
  arg_10 = []
  arg_11 = arg_1['physical_plan']['spouts']
  arg_12 = arg_1['physical_plan']['bolts']
  for arg_4, arg_5 in arg_0.items():
    # stages is an int so keep going
    if arg_4 == "stages":
      continue
    for arg_6, arg_7 in arg_5.items():
      arg_13 = [arg_4[:-1], arg_6]
      if arg_4 == 'spouts':
        arg_13.append(len(arg_11[arg_6]))
      else:
        arg_13.append(len(arg_12[arg_6]))
      arg_13.append(','.join(arg_2.get(arg_6, ['-'])))
      arg_13.append(','.join(arg_3.get(arg_6, ['-'])))
      arg_10.append(arg_13)
  arg_14 = ['type', 'name', 'parallelism', 'input', 'output']
  return arg_10, arg_14


def Func(arg_0, arg_1):
  """ filter to keep bolts """
  arg_2 = []
  for arg_3 in arg_0:
    if arg_3[0] == 'bolt':
      arg_2.append(arg_3)
  return arg_2, arg_1


def Func(arg_0, arg_1, arg_2=None):
    """ get physical plan """
    arg_3 = False

    # Temp dict used to return result
    # if callback is not provided.
    arg_4 = {
        "result": None
    }
    if arg_2:
      arg_3 = True
    else:
      def arg_2(arg_5):
        """
        Custom callback to get the topologies right now.
        """
        arg_4["result"] = arg_5

    arg_0._Func_with_watch(arg_1, arg_2, arg_3)

    # The topologies are now populated with the data.
    return arg_4["result"]


def Func(arg_0, arg_1, arg_2):
    """ create physical plan """
    if not arg_2 or not arg_2.IsInitialized():
      raise_(StateException("Physical Plan protobuf not init properly",
                            StateException.EX_TYPE_PROTOBUF_ERROR), sys.exc_info()[2])

    arg_3 = arg_0.get_pplan_path(arg_1)
    LOG.info("Adding topology: {0} to path: {1}".format(
        arg_1, arg_3))
    arg_4 = arg_2.SerializeToString()
    try:
      arg_0.client.create(arg_3, value=arg_4, makepath=True)
      return True
    except NoNodeError:
      raise_(StateException("NoNodeError while creating pplan",
                            StateException.EX_TYPE_NO_NODE_ERROR), sys.exc_info()[2])
    except NodeExistsError:
      raise_(StateException("NodeExistsError while creating pplan",
                            StateException.EX_TYPE_NODE_EXISTS_ERROR), sys.exc_info()[2])
    except ZookeeperError:
      raise_(StateException("Zookeeper while creating pplan",
                            StateException.EX_TYPE_ZOOKEEPER_ERROR), sys.exc_info()[2])
    except Exception:
      # Just re raise the exception.
      raise


def Func(arg_0, arg_1, arg_2=None):
    """ get execution state """
    arg_3 = False

    # Temp dict used to return result
    # if callback is not provided.
    arg_4 = {
        "result": None
    }
    if arg_2:
      arg_3 = True
    else:
      def arg_2(arg_5):
        """
        Custom callback to get the topologies right now.
        """
        arg_4["result"] = arg_5

    arg_0._Func_with_watch(arg_1, arg_2, arg_3)

    # The topologies are now populated with the data.
    return arg_4["result"]


def Func(arg_0, arg_1, arg_2, arg_3):
    """
    Helper function to get execution state with
    a callback. The future watch is placed
    only if isWatching is True.
    """
    arg_4 = arg_0.get_execution_state_path(arg_1)
    if arg_3:
      LOG.info("Adding data watch for path: " + arg_4)

    # pylint: disable=unused-variable, unused-argument
    @arg_0.client.DataWatch(arg_4)
    def watch_execution_state(arg_5, arg_6):
      """ invoke callback to watch execute state """
      if arg_5:
        arg_7 = ExecutionState()
        arg_7.ParseFromString(arg_5)
        arg_2(arg_7)
      else:
        arg_2(None)

      # Returning False will result in no future watches
      # being triggered. If isWatching is True, then
      # the future watches will be triggered.
      return arg_3


def Func(arg_0):
  """
  Deserializes Java primitive data and objects serialized by ObjectOutputStream
  from a file-like object.
  """
  arg_1 = JavaObjectUnmarshaller(arg_0)
  arg_1.add_transformer(DefaultObjectTransformer())
  return arg_1.readObject()


def Func(arg_0, arg_1):
    """Func an object"""
    arg_1.classdesc = arg_0.classdesc

    for arg_3 in arg_0.classdesc.fields_names:
      arg_1.__setattr__(arg_3, getattr(arg_0, arg_3))


def Func(arg_0, arg_1, arg_2):
    """
    Fetches Instance jstack from heron-shell.
    """
    arg_3 = yield getInstancePid(arg_1, arg_2)
    try:
      arg_4 = tornado.httpclient.AsyncHTTPClient()
      arg_5 = json.loads(arg_3)
      arg_6 = arg_5['stdout'].strip()
      if arg_6 == '':
        raise Exception('Failed to get pid')
      arg_7 = utils.make_shell_endpoint(arg_1, arg_2)
      arg_8 = "%s/jstack/%s" % (arg_7, arg_6)
      arg_9 = yield arg_4.fetch(arg_8)
      Log.debug("HTTP call for url: %s", arg_8)
      raise tornado.gen.Return(arg_9.body)
    except tornado.httpclient.HTTPError as e:
      raise Exception(str(e))


def Func(arg_0):
  """ Create the parse for the update command """
  arg_1 = arg_0.add_parser(
      'update',
      help='Update a topology',
      usage="%(prog)s [options] cluster/[role]/[env] <topology-name> "
      + "[--component-parallelism <name:value>] "
      + "[--container-number value] "
      + "[--runtime-config [component:]<name:value>]",
      add_help=True)

  args.add_titles(arg_1)
  args.add_cluster_role_env(arg_1)
  args.add_topology(arg_1)

  args.add_config(arg_1)
  args.add_dry_run(arg_1)
  args.add_service_url(arg_1)
  args.add_verbose(arg_1)

  # Special parameters for update command
  def parallelism_type(arg_2):
    arg_3 = re.compile(r"^[\w\.-]+:[\d]+$")
    if not arg_3.match(arg_2):
      raise argparse.ArgumentTypeError(
          "Invalid syntax for component parallelism (<component_name:value>): %s" % arg_2)
    return arg_2

  arg_1.add_argument(
      '--component-parallelism',
      action='append',
      type=parallelism_type,
      required=False,
      help='Component name and the new parallelism value '
      + 'colon-delimited: <component_name>:<parallelism>')

  def runtime_config_type(arg_2):
    arg_3 = re.compile(r"^([\w\.-]+:){1,2}[\w\.-]+$")
    if not arg_3.match(arg_2):
      raise argparse.ArgumentTypeError(
          "Invalid syntax for runtime config ([component:]<name:value>): %s"
          % arg_2)
    return arg_2

  arg_1.add_argument(
      '--runtime-config',
      action='append',
      type=runtime_config_type,
      required=False,
      help='Runtime configurations for topology and components '
      + 'colon-delimited: [component:]<name>:<value>')

  def container_number_type(arg_2):
    arg_3 = re.compile(r"^\d+$")
    if not arg_3.match(arg_2):
      raise argparse.ArgumentTypeError(
          "Invalid syntax for container number (value): %s"
          % arg_2)
    return arg_2

  arg_1.add_argument(
      '--container-number',
      action='append',
      type=container_number_type,
      required=False,
      help='Number of containers <value>')

  arg_1.set_defaults(subcommand='update')
  return arg_1


def Func(arg_0):
  """ flatten extra args """
  arg_1 = []
  if 'component_parallelism' in arg_0:
    arg_1 += ["--component_parallelism",
                        ','.join(arg_0['component_parallelism'])]
  if 'runtime_config' in arg_0:
    arg_1 += ["--runtime_config",
                        ','.join(arg_0['runtime_config'])]
  if 'container_number' in arg_0:
    arg_1 += ["--container_number",
                        ','.join(arg_0['container_number'])]
  if 'dry_run' in arg_0 and arg_0['dry_run']:
    arg_1 += ['--dry_run']
  if 'dry_run_format' in arg_0:
    arg_1 += ['--dry_run_format', arg_0['dry_run_format']]

  return arg_1


def Func(arg_0, arg_1):
    """Checks if a given gtype is sane"""
    if arg_1 == arg_0.SHUFFLE or arg_1 == arg_0.ALL or arg_1 == arg_0.LOWEST or arg_1 == arg_0.NONE:
      return True
    elif isinstance(arg_1, arg_0.FIELDS):
      return arg_1.gtype == topology_pb2.Grouping.Value("FIELDS") and \
             arg_1.fields is not None
    elif isinstance(arg_1, arg_0.CUSTOM):
      return arg_1.gtype == topology_pb2.Grouping.Value("CUSTOM") and \
             arg_1.python_serialized is not None
    else:
      #pylint: disable=fixme
      #TODO: DIRECT are not supported yet
      return False


def Func(arg_0, arg_1):
    """Custom grouping from a given implementation of ICustomGrouping

    :param Funcgrouper: The ICustomGrouping implemention to use
    """
    if arg_1 is None:
      raise TypeError("Argument to Func() must be ICustomGrouping instance or classpath")
    if not isinstance(arg_1, ICustomGrouping) and not isinstance(arg_1, str):
      raise TypeError("Argument to Func() must be ICustomGrouping instance or classpath")
    arg_2 = default_serializer.serialize(arg_1)
    return arg_0.Func_serialized(arg_2, is_java=False)


def Func(arg_0, arg_1, arg_2=1, arg_3=None):
    """Update the value of CountMetric or MultiCountMetric

    :type name: str
    :param name: name of the registered metric to be updated.
    :type incr_by: int
    :param incr_by: specifies how much to increment. Default is 1.
    :type key: str or None
    :param key: specifies a key for MultiCountMetric. Needs to be `None` for updating CountMetric.
    """
    if arg_1 not in arg_0.metrics:
      Log.error("In Func(): %s is not registered in the metric", arg_1)

    if arg_3 is None and isinstance(arg_0.metrics[arg_1], CountMetric):
      arg_0.metrics[arg_1].incr(arg_2)
    elif arg_3 is not None and isinstance(arg_0.metrics[arg_1], MultiCountMetric):
      arg_0.metrics[arg_1].incr(arg_3, arg_2)
    else:
      Log.error("In Func(): %s is registered but not supported with this method", arg_1)


def Func(arg_0, arg_1, arg_2, arg_3=None):
    """Update the value of ReducedMetric or MultiReducedMetric

    :type name: str
    :param name: name of the registered metric to be updated.
    :param value: specifies a value to be reduced.
    :type key: str or None
    :param key: specifies a key for MultiReducedMetric. Needs to be `None` for updating
                ReducedMetric.
    """
    if arg_1 not in arg_0.metrics:
      Log.error("In Func(): %s is not registered in the metric", arg_1)

    if arg_3 is None and isinstance(arg_0.metrics[arg_1], ReducedMetric):
      arg_0.metrics[arg_1].update(arg_2)
    elif arg_3 is not None and isinstance(arg_0.metrics[arg_1], MultiReducedMetric):
      arg_0.metrics[arg_1].update(arg_3, arg_2)
    else:
      Log.error("In update_count(): %s is registered but not supported with this method", arg_1)


def Func(arg_0, arg_1, arg_2, arg_3):
    """Apply updates to the execute metrics"""
    arg_0.update_count(arg_0.EXEC_COUNT, key=arg_1)
    arg_0.update_reduced_metric(arg_0.EXEC_LATENCY, arg_3, arg_1)
    arg_0.update_count(arg_0.EXEC_TIME_NS, incr_by=arg_3, key=arg_1)

    arg_4 = arg_2 + "/" + arg_1
    arg_0.update_count(arg_0.EXEC_COUNT, key=arg_4)
    arg_0.update_reduced_metric(arg_0.EXEC_LATENCY, arg_3, arg_4)
    arg_0.update_count(arg_0.EXEC_TIME_NS, incr_by=arg_3, key=arg_4)


def Func(arg_0, arg_1, arg_2, arg_3):
    """Apply updates to the deserialization metrics"""
    arg_0.update_count(arg_0.TUPLE_DESERIALIZATION_TIME_NS, incr_by=arg_3, key=arg_1)
    arg_4 = arg_2 + "/" + arg_1
    arg_0.update_count(arg_0.TUPLE_DESERIALIZATION_TIME_NS, incr_by=arg_3,
                      key=arg_4)


def Func(arg_0, arg_1, arg_2, arg_3):
    """Registers a given metric

    :param name: name of the metric
    :param metric: IMetric object to be registered
    :param time_bucket_in_sec: time interval for update to the metrics manager
    """
    if arg_1 in arg_0.metrics_map:
      raise RuntimeError("Another metric has already been registered with name: %s" % arg_1)

    Log.debug("Register metric: %s, with interval: %s", arg_1, str(arg_3))
    arg_0.metrics_map[arg_1] = arg_2

    if arg_3 in arg_0.time_bucket_in_sec_to_metrics_name:
      arg_0.time_bucket_in_sec_to_metrics_name[arg_3].append(arg_1)
    else:
      arg_0.time_bucket_in_sec_to_metrics_name[arg_3] = [arg_1]
      arg_0._register_timer_task(arg_3)


def Func(arg_0, arg_1):
    """Offer to the buffer

    It is a non-blocking operation, and when the buffer is full, it raises Queue.Full exception
    """
    try:
      # non-blocking
      arg_0._buffer.put(arg_1, block=False)
      if arg_0._consumer_callback is not None:
        arg_0._consumer_callback()
      return True
    except Queue.Full:
      Log.debug("%s: Full in Func()" % str(arg_0))
      raise Queue.Full


def Func(arg_0):
    """
    Parse version to major, minor, patch, pre-release, build parts.
    """
    arg_1 = _REGEX.match(arg_0)
    if arg_1 is None:
        raise ValueError('%s is not valid SemVer string' % arg_0)

    arg_2 = arg_1.groupdict()

    arg_2['major'] = int(arg_2['major'])
    arg_2['minor'] = int(arg_2['minor'])
    arg_2['patch'] = int(arg_2['patch'])

    return arg_2


def Func(arg_0):
  """
  Returns all the file state_managers.
  """
  arg_1 = []
  arg_2 = arg_0.get_state_locations_of_type("file")
  for arg_3 in arg_2:
    arg_4 = arg_3['name']
    arg_5 = os.path.expanduser(arg_3['rootpath'])
    LOG.info("Connecting to file state with rootpath: " + arg_5)
    arg_6 = FileStateManager(arg_4, arg_5)
    arg_1.append(arg_6)

  return arg_1


def Func(arg_0, arg_1, arg_2=1):
    """Increments the value of a given key by ``to_add``"""
    if arg_1 not in arg_0.value:
      arg_0.value[arg_1] = CountMetric()
    arg_0.value[arg_1].Func(arg_2)


def Func(arg_0, arg_1):
    """Adds a new key to this metric"""
    if arg_1 not in arg_0.value:
      arg_0.value[arg_1] = ReducedMetric(arg_0.reducer)


def Func(arg_0, arg_1, arg_2, arg_3):
    """Add a new data tuple to the currently buffered set of tuples"""
    if (arg_0.current_data_tuple_set is None) or \
        (arg_0.current_data_tuple_set.stream.id != arg_1) or \
        (len(arg_0.current_data_tuple_set.tuples) >= arg_0.data_tuple_set_capacity) or \
        (arg_0.current_data_tuple_size_in_bytes >= arg_0.max_data_tuple_size_in_bytes):
      arg_0._init_new_data_tuple(arg_1)

    arg_4 = arg_0.current_data_tuple_set.tuples.add()
    arg_4.CopyFrom(arg_2)

    arg_0.current_data_tuple_size_in_bytes += arg_3
    arg_0.total_data_emitted_in_bytes += arg_3


def Func(arg_0, arg_1, arg_2):
    """Add the checkpoint state message to be sent back the stmgr

    :param ckpt_id: The id of the checkpoint
    :ckpt_state: The checkpoint state
    """
    # first flush any buffered tuples
    arg_0._flush_remaining()
    arg_3 = ckptmgr_pb2.StoreInstanceStateCheckpoint()
    arg_4 = ckptmgr_pb2.InstanceStateCheckpoint()
    arg_4.checkpoint_id = arg_1
    arg_4.state = arg_2
    arg_3.state.CopyFrom(arg_4)
    arg_0._push_tuple_to_stream(arg_3)


def Func(arg_0):
  '''
  Check if an entry in the class path exists as either a directory or a file
  '''
  # check if the suffic of classpath suffix exists as directory
  if arg_0.endswith('*'):
    Log.debug('Checking classpath entry suffix as directory: %s', arg_0[:-1])
    if os.path.isdir(arg_0[:-1]):
      return True
    return False

  # check if the classpath entry is a directory
  Log.debug('Checking classpath entry as directory: %s', arg_0)
  if os.path.isdir(arg_0):
    return True
  else:
    # check if the classpath entry is a file
    Log.debug('Checking classpath entry as file: %s', arg_0)
    if os.path.isfile(arg_0):
      return True

  return False


def Func(arg_0):
  '''
  Given a java classpath, check whether the path entries are valid or not
  '''
  arg_1 = arg_0.split(':')
  for arg_2 in arg_1:
    if not valid_path(arg_2.strip()):
      return False
  return True


def Func(arg_0):
  """Get a list of paths to included dependencies in the specified pex file

  Note that dependencies are located under `.deps` directory
  """
  arg_1 = zipfile.ZipFile(arg_0, mode='r')
  arg_2 = list(set([re.match(egg_regex, i).group(1) for i in arg_1.namelist()
                   if re.match(egg_regex, i) is not None]))
  return arg_2


def Func(arg_0, arg_1=True):
  """Loads pex file and its dependencies to the current python path"""
  arg_2 = os.path.abspath(arg_0)
  Log.debug("Add a pex to the path: %s" % arg_2)
  if arg_2 not in sys.path:
    sys.path.insert(0, os.path.dirname(arg_2))

  # add dependencies to path
  if arg_1:
    for arg_3 in _get_deps_list(arg_2):
      arg_4 = os.path.join(os.path.dirname(arg_2), arg_3)
      if arg_4 not in sys.path:
        Log.debug("Add a new dependency to the path: %s" % arg_3)
        sys.path.insert(0, arg_4)

  Log.debug("Python path: %s" % str(sys.path))


def Func(arg_0, arg_1):
  """Resolves duplicate package suffix problems

  When dynamically loading a pex file and a corresponding python class (bolt/spout/topology),
  if the top level package in which to-be-loaded classes reside is named 'heron', the path conflicts
  with this Heron Instance pex package (heron.instance.src.python...), making the Python
  interpreter unable to find the target class in a given pex file.
  This function resolves this issue by individually loading packages with suffix `heron` to
  avoid this issue.

  However, if a dependent module/class that is not directly specified under ``class_path``
  and has conflicts with other native heron packages, there is a possibility that
  such a class/module might not be imported correctly. For example, if a given ``class_path`` was
  ``heron.common.src.module.Class``, but it has a dependent module (such as by import statement),
  ``heron.common.src.python.dep_module.DepClass`` for example, pex_loader does not guarantee that
  ``DepClass` is imported correctly. This is because ``heron.common.src.python.dep_module`` is not
  explicitly added to sys.path, while ``heron.common.src.python`` module exists as the native heron
  package, from which ``dep_module`` cannot be found, so Python interpreter may raise ImportError.

  The best way to avoid this issue is NOT to dynamically load a pex file whose top level package
  name is ``heron``. Note that this method is included because some of the example topologies and
  tests have to have a pex with its top level package name of ``heron``.
  """
  # import top-level package named `heron` of a given pex file
  arg_2 = zipimport.zipimporter(arg_0)
  arg_2.load_module("heron")

  # remove 'heron' and the classname
  arg_3 = arg_1.split('.')[1:-1]
  arg_4 = ['heron']
  arg_5 = None
  for arg_6 in arg_3:
    arg_7 = zipimport.zipimporter(os.path.join(arg_0, '/'.join(arg_4)))
    arg_5 = arg_7.load_module(arg_6)
    arg_4.append(arg_6)

  return arg_5


def Func(arg_0, arg_1):
    """Builds the topology and returns the Funcer"""
    arg_2 = sets.Set()
    for arg_3 in arg_0._sources:
      arg_3._Func(arg_1, arg_2)
    for arg_3 in arg_0._sources:
      if not arg_3._all_built():
        raise RuntimeError("Topology cannot be fully built! Are all sources added?")


def Func(arg_0, arg_1, arg_2):
  """For each kvp in config, do wildcard substitution on the values"""
  for arg_3 in arg_0:
    arg_4 = arg_0[arg_3]
    arg_5 = arg_4
    if isinstance(arg_4, str):
      for arg_6 in arg_1:
        if arg_1[arg_6]:
          arg_4 = arg_4.replace(arg_6, arg_1[arg_6])
      arg_7 = re.findall(r'\${[A-Z_]+}', arg_4)
      if arg_7:
        raise ValueError("%s=%s in file %s contains unsupported or unset wildcard tokens: %s" %
                         (arg_3, arg_5, arg_2, ", ".join(arg_7)))
      arg_0[arg_3] = arg_4
  return arg_0


def Func(arg_0, arg_1, arg_2, arg_3):
    """ set default time """
    arg_4 = arg_2 / 60 * 60
    if arg_4 < arg_2:
      arg_4 += 60
    arg_5 = arg_3 / 60 * 60
    while arg_4 <= arg_5:
      # STREAMCOMP-1559
      # Second check is a work around, because the response from tmaster
      # contains value 0, if it is queries for the current timestamp,
      # since the bucket is created in the tmaster, but is not filled
      # by the metrics.
      if arg_4 not in arg_0.timeline or arg_0.timeline[arg_4] == 0:
        arg_0.timeline[arg_4] = arg_1
      arg_4 += 60


def Func(arg_0, arg_1):
    """Process a single tuple of input

    We add the (time, tuple) pair into our current_tuples. And then look for expiring
    elemnents
    """
    arg_2 = int(time.time())
    arg_0.current_tuples.append((arg_1, arg_2))
    arg_0._expire(arg_2)


def Func(arg_0, arg_1):
    """Called every slide_interval
    """
    arg_2 = int(time.time())
    arg_3 = WindowContext(arg_2 - arg_0.window_duration, arg_2)
    arg_4 = []
    for (arg_1, arg_5) in arg_0.current_tuples:
      arg_4.append(arg_1)
    arg_0.processWindow(arg_3, arg_4)
    arg_0._expire(arg_2)


def Func(arg_0, arg_1):
    """Called every window_duration
    """
    arg_2 = int(time.time())
    arg_3 = WindowContext(arg_2 - arg_0.window_duration, arg_2)
    arg_0.processWindow(arg_3, list(arg_0.current_tuples))
    for arg_1 in arg_0.current_tuples:
      arg_0.ack(arg_1)
    arg_0.current_tuples.clear()


def Func(arg_0, arg_1, arg_2=None):
    """
    Get summary of stream managers registration summary
    """
    if not arg_1 or not arg_1.host or not arg_1.stats_port:
      return
    arg_3 = tmaster_pb2.StmgrsRegistrationSummaryRequest()
    arg_4 = arg_3.SerializeToString()
    arg_5 = str(arg_1.stats_port)
    arg_6 = arg_1.host
    arg_7 = "http://{0}:{1}/stmgrsregistrationsummary".format(arg_6, arg_5)
    arg_8 = tornado.httpclient.HTTPRequest(arg_7,
                                             body=arg_4,
                                             method='POST',
                                             request_timeout=5)
    Log.debug('Making HTTP call to fetch stmgrsregistrationsummary url: %s', arg_7)
    try:
      arg_9 = tornado.httpclient.AsyncHTTPClient()
      arg_10 = yield arg_9.fetch(arg_8)
      Log.debug("HTTP call complete.")
    except tornado.httpclient.HTTPError as e:
      raise Exception(str(e))
    # Check the response code - error if it is in 400s or 500s
    arg_11 = arg_10.code
    if arg_11 >= 400:
      arg_12 = "Error in getting exceptions from Tmaster, code: " + arg_11
      Log.error(arg_12)
      raise tornado.gen.Return({
          "message": arg_12
      })
    # Parse the response from tmaster.
    arg_13 = tmaster_pb2.StmgrsRegistrationSummaryResponse()
    arg_13.ParseFromString(arg_10.body)
    # Send response
    arg_14 = {}
    for arg_15 in arg_13.registered_stmgrs:
      arg_14[arg_15] = True
    for arg_15 in arg_13.absent_stmgrs:
      arg_14[arg_15] = False
    raise tornado.gen.Return(arg_14)


def Func(arg_0):
  """Set up log, process and signal handlers"""
  # pylint: disable=unused-argument
  def signal_handler(arg_1, arg_2):
    # We would do nothing here but just exit
    # Just catch the SIGTERM and then cleanup(), registered with atexit, would invoke
    Log.info('signal_handler invoked with signal %s', arg_1)
    arg_0.stop_state_manager_watches()
    sys.exit(arg_1)

  def cleanup():
    """Handler to trigger when receiving the SIGTERM signal
    Do cleanup inside this method, including:
    1. Terminate all children processes
    """
    Log.info('Executor terminated; exiting all process in executor.')

    # Kill child processes first and wait for log collection to finish
    for arg_3 in arg_0.processes_to_monitor.keys():
      os.kill(arg_3, signal.SIGTERM)
    time.sleep(5)

    # We would not wait or check whether process spawned dead or not
    os.killpg(0, signal.SIGTERM)

  # Redirect stdout and stderr to files in append mode
  # The filename format is heron-executor-<container_id>.stdxxx
  arg_4 = arg_0.shard
  log.configure(logfile='heron-executor-%s.stdout' % arg_4)

  arg_3 = os.getpid()
  arg_5 = os.getsid(arg_3)

  # POSIX prohibits the change of the process group ID of a session leader
  if arg_3 <> arg_5:
    Log.info('Set up process group; executor becomes leader')
    os.setpgrp() # create new process group, become its leader

  Log.info('Register the SIGTERM signal handler')
  signal.signal(signal.SIGTERM, signal_handler)

  Log.info('Register the atexit clean up')
  atexit.register(cleanup)


def Func():
  """Register exit handlers, initialize the executor and run it."""
  # Since Heron on YARN runs as headless users, pex compiled
  # binaries should be exploded into the container working
  # directory. In order to do this, we need to set the
  # PEX_ROOT shell environment before forking the processes
  arg_0 = os.environ.copy()
  arg_0["PEX_ROOT"] = os.path.join(os.path.abspath('.'), ".pex")

  # Instantiate the executor, bind it to signal handlers and launch it
  arg_1 = HeronExecutor(sys.argv, arg_0)
  arg_1.initialize()

  start(arg_1)


def Func(arg_0):
    '''
    Returns the processes to handle streams, including the stream-mgr and the user code containing
    the stream logic of the topology
    '''
    arg_1 = {}
    arg_2 = arg_0._get_instance_plans(arg_0.packing_plan, arg_0.shard)
    arg_3 = []
    for arg_4 in arg_2:
      arg_5 = arg_4.task_id
      arg_6 = arg_4.component_index
      arg_7 = arg_4.component_name
      arg_8 = "container_%s_%s_%d" % (str(arg_0.shard), arg_7, arg_5)
      arg_3.append((arg_8, arg_7, arg_5, arg_6))

    arg_9 = [
        arg_0.stmgr_binary,
        '--topology_name=%s' % arg_0.topology_name,
        '--topology_id=%s' % arg_0.topology_id,
        '--topologydefn_file=%s' % arg_0.topology_defn_file,
        '--zkhostportlist=%s' % arg_0.state_manager_connection,
        '--zkroot=%s' % arg_0.state_manager_root,
        '--stmgr_id=%s' % arg_0.stmgr_ids[arg_0.shard],
        '--instance_ids=%s' % ','.join(map(lambda x: x[0], arg_3)),
        '--myhost=%s' % arg_0.master_host,
        '--data_port=%s' % str(arg_0.master_port),
        '--local_data_port=%s' % str(arg_0.tmaster_controller_port),
        '--metricsmgr_port=%s' % str(arg_0.metrics_manager_port),
        '--shell_port=%s' % str(arg_0.shell_port),
        '--config_file=%s' % arg_0.heron_internals_config_file,
        '--override_config_file=%s' % arg_0.override_config_file,
        '--ckptmgr_port=%s' % str(arg_0.checkpoint_manager_port),
        '--ckptmgr_id=%s' % arg_0.ckptmgr_ids[arg_0.shard],
        '--metricscachemgr_mode=%s' % arg_0.metricscache_manager_mode.lower()]

    arg_10 = arg_0.shell_env.copy() if arg_0.shell_env is not None else {}
    arg_11 = Command(arg_9, arg_10)
    if os.environ.get('ENABLE_HEAPCHECK') is not None:
      arg_11.env.update({
          'LD_PRELOAD': "/usr/lib/libtcmalloc.so",
          'HEAPCHECK': "normal"
      })

    arg_1[arg_0.stmgr_ids[arg_0.shard]] = arg_11

    # metricsmgr_metrics_sink_config_file = 'metrics_sinks.yaml'

    arg_1[arg_0.metricsmgr_ids[arg_0.shard]] = arg_0._get_metricsmgr_cmd(
        arg_0.metricsmgr_ids[arg_0.shard],
        arg_0.metrics_sinks_config_file,
        arg_0.metrics_manager_port
    )

    if arg_0.is_stateful_topology:
      arg_1.update(arg_0._get_ckptmgr_process())

    if arg_0.pkg_type == 'jar' or arg_0.pkg_type == 'tar':
      arg_1.update(arg_0._get_java_instance_cmd(arg_3))
    elif arg_0.pkg_type == 'pex':
      arg_1.update(arg_0._get_python_instance_cmd(arg_3))
    elif arg_0.pkg_type == 'so':
      arg_1.update(arg_0._get_cpp_instance_cmd(arg_3))
    elif arg_0.pkg_type == 'dylib':
      arg_1.update(arg_0._get_cpp_instance_cmd(arg_3))
    else:
      raise ValueError("Unrecognized package type: %s" % arg_0.pkg_type)

    return arg_1


def Func(arg_0, arg_1, arg_2):
    """
    For the given packing_plan, return the container plan with the given container_id. If protobufs
    supported maps, we could just get the plan by id, but it doesn't so we have a collection of
    containers to iterate over.
    """
    arg_3 = None
    for arg_4 in arg_1.container_plans:
      if arg_4.id == arg_2:
        arg_3 = arg_4

    # When the executor runs in newly added container by `heron update`,
    # there is no plan for this container. In this situation,
    # return None to bypass instance processes.
    if arg_3 is None:
      return None
    return arg_3.instance_plans


def Func(arg_0):
    """ Get a map from all daemon services' name to the command to start them """
    arg_1 = {}

    arg_1[arg_0.heron_shell_ids[arg_0.shard]] = Command([
        '%s' % arg_0.heron_shell_binary,
        '--port=%s' % arg_0.shell_port,
        '--log_file_prefix=%s/heron-shell-%s.log' % (arg_0.log_dir, arg_0.shard),
        '--secret=%s' % arg_0.topology_id], arg_0.shell_env)

    return arg_1


def Func(arg_0, arg_1):
    """Start all commands and add them to the dict of processes to be monitored """
    Log.info("Start processes")
    arg_2 = {}
    # First start all the processes
    for (arg_3, arg_4) in arg_1.items():
      arg_5 = arg_0._run_process(arg_3, arg_4)
      arg_2[arg_5.pid] = ProcessInfo(arg_5, arg_3, arg_4)

      # Log down the pid file
      log_pid_for_process(arg_3, arg_5.pid)

    with arg_0.process_lock:
      arg_0.processes_to_monitor.update(arg_2)


def Func(arg_0):
    """ Monitor all processes in processes_to_monitor dict,
    restarting any if they fail, up to max_runs times.
    """
    # Now wait for any child to die
    Log.info("Start process monitor")
    while True:
      if len(arg_0.processes_to_monitor) > 0:
        (arg_1, arg_2) = os.wait()

        with arg_0.process_lock:
          if arg_1 in arg_0.processes_to_monitor.keys():
            arg_3 = arg_0.processes_to_monitor[arg_1]
            arg_4 = arg_3.name
            arg_5 = arg_3.command
            Log.info("%s (pid=%s) exited with status %d. command=%s" % (arg_4, arg_1, arg_2, arg_5))
            # Log the stdout & stderr of the failed process
            arg_0._wait_process_std_out_err(arg_4, arg_3.process)

            # Just make it world readable
            if os.path.isfile("core.%d" % arg_1):
              os.system("chmod a+r core.%d" % arg_1)
            if arg_3.attempts >= arg_0.max_runs:
              Log.info("%s exited too many times" % arg_4)
              sys.exit(1)
            time.sleep(arg_0.interval_between_runs)
            arg_6 = arg_0._run_process(arg_4, arg_5)
            del arg_0.processes_to_monitor[arg_1]
            arg_0.processes_to_monitor[arg_6.pid] =\
              ProcessInfo(arg_6, arg_4, arg_5, arg_3.attempts + 1)

            # Log down the pid file
            log_pid_for_process(arg_4, arg_6.pid)


def Func(arg_0):
    ''' Determines the commands to be run and compares them with the existing running commands.
    Then starts new ones required and kills old ones no longer required.
    '''
    with arg_0.process_lock:
      arg_1 = dict(map((lambda process: (process.name, process.command)),
                                  arg_0.processes_to_monitor.values()))
      arg_2 = arg_0.get_commands_to_run()

      # get the commands to kill, keep and start
      arg_3, arg_4, arg_5 = \
          arg_0.get_command_changes(arg_1, arg_2)

      Log.info("current commands: %s" % sorted(arg_1.keys()))
      Log.info("new commands    : %s" % sorted(arg_2.keys()))
      Log.info("commands_to_kill: %s" % sorted(arg_3.keys()))
      Log.info("commands_to_keep: %s" % sorted(arg_4.keys()))
      Log.info("commands_to_start: %s" % sorted(arg_5.keys()))

      arg_0._kill_processes(arg_3)
      arg_0._start_processes(arg_5)
      Log.info("Launch complete - processes killed=%s kept=%s started=%s monitored=%s" %
               (len(arg_3), len(arg_4),
                len(arg_5), len(arg_0.processes_to_monitor)))


def Func(arg_0, arg_1, arg_2, arg_3):
    """Builds the topology and submits it"""
    if not isinstance(arg_1, str):
      raise RuntimeError("Name has to be a string type")
    if not isinstance(arg_2, Config):
      raise RuntimeError("config has to be a Config type")
    if not isinstance(arg_3, Builder):
      raise RuntimeError("builder has to be a Builder type")
    arg_4 = TopologyBuilder(arg_1=arg_1)
    arg_3.build(arg_4)
    arg_4.set_config(arg_2._api_config)
    arg_4.build_and_submit()


def Func(arg_0):
  """Force every module in modList to be placed into main"""
  if not arg_0:
    return

  arg_1 = sys.modules['__main__']
  for arg_2 in arg_0:
    if isinstance(arg_2, str):
      try:
        arg_3 = __import__(arg_2)
      except Exception:
        sys.stderr.write(
            'warning: could not import %s\n.  '
            'Your function may unexpectedly error due to this import failing;'
            'A version mismatch is likely.  Specific error was:\n' % arg_2)
        print_exec(sys.stderr)
      else:
        setattr(arg_1, arg_3.__name__, arg_3)


def Func(arg_0, arg_1):
  """
  Loads additional properties into class `cls`.
  """
  for arg_2, arg_3 in arg_1.items():
    if isinstance(arg_2, tuple):
      arg_4, arg_2 = arg_2
      if arg_4 == 'property':
        arg_3 = property(*arg_3)
      elif arg_4 == 'staticmethod':
        arg_3 = staticmethod(arg_3) # pylint: disable=redefined-variable-type
      elif arg_4 == 'classmethod':
        arg_3 = classmethod(arg_3)
    setattr(arg_0, arg_2, arg_3)
  return arg_0


def Func(arg_0, arg_1):
  """Returns last n lines from the filename. No exception handling"""
  arg_2 = os.path.getsize(arg_0)
  with open(arg_0, "rb") as f:
   arg_3 = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
   try:
      for arg_4 in xrange(arg_2 - 1, -1, -1):
          if arg_3[arg_4] == '\n':
             arg_1 -= 1
             if arg_1 == -1:
                break
      return arg_3[arg_4 + 1 if arg_4 else 0:].splitlines()
   finally:
        arg_3.close()


def Func(arg_0):
    """Returns a serializer for a given context"""
    arg_1 = arg_0.get_cluster_config()
    arg_2 = arg_1.get(constants.TOPOLOGY_SERIALIZER_CLASSNAME, None)
    if arg_2 is None:
      return PythonSerializer()
    else:
      try:
        arg_3 = arg_0.get_topology_pex_path()
        pex_loader.load_pex(arg_3)
        arg_4 = pex_loader.import_and_get_class(arg_3, arg_2)
        arg_5 = arg_4()
        return arg_5
      except Exception as e:
        raise RuntimeError("Error with loading custom serializer class: %s, with error message: %s"
                           % (arg_2, str(e)))


def Func(arg_0, arg_1, arg_2):
    """Registers a new timer task

    :param task: function to be run at a specified second from now
    :param second: how many seconds to wait before the timer is triggered
    """
    # Python time is in float
    arg_3 = float(arg_2)
    arg_4 = time.time() + arg_3
    heappush(arg_0.timer_tasks, (arg_4, arg_1))


def Func(arg_0):
    """Get the next timeout from now

    This should be used from do_wait().
    :returns (float) next_timeout, or 10.0 if there are no timer events
    """
    if len(arg_0.timer_tasks) == 0:
      return sys.maxsize
    else:
      arg_1 = arg_0.timer_tasks[0][0] - time.time()
      return arg_1


def Func(arg_0, arg_1):
    """Returns a parse tree for the query, each of the node is a
    subclass of Operator. This is both a lexical as well as syntax analyzer step."""
    if not arg_1:
      return None
    # Just braces do not matter
    if arg_1[0] == '(':
      arg_2 = arg_0.find_closing_braces(arg_1)
      # This must be the last index, since this was an NOP starting brace
      if arg_2 != len(arg_1) - 1:
        raise Exception("Invalid syntax")
      else:
        return arg_0.Func(arg_1[1:-1])
    arg_3 = arg_1.find("(")
    # There must be a ( in the query
    if arg_3 < 0:
      # Otherwise it must be a constant
      try:
        arg_4 = float(arg_1)
        return arg_4
      except ValueError:
        raise Exception("Invalid syntax")
    arg_5 = arg_1[:arg_3]
    if arg_5 not in arg_0.operators:
      raise Exception("Invalid token: " + arg_5)

    # Get sub components
    arg_6 = arg_1[arg_3:]
    arg_7 = arg_0.find_closing_braces(arg_6)
    if arg_7 != len(arg_6) - 1:
      raise Exception("Invalid syntax")
    arg_8 = arg_0.get_sub_parts(arg_6[1:-1])

    # parts are simple strings in this case
    if arg_5 == "TS":
      # This will raise exception if parts are not syntactically correct
      return arg_0.operators[arg_5](arg_8)

    arg_9 = []
    for arg_10 in arg_8:
      arg_9.append(arg_0.Func(arg_10))

    # Make a node for the current token
    arg_11 = arg_0.operators[arg_5](arg_9)
    return arg_11


def Func(arg_0, arg_1):
    """Indicate that processing of a Tuple has Funced

    It is compatible with StreamParse API.
    """
    if not isinstance(arg_1, HeronTuple):
      Log.error("Only HeronTuple type is supported in Func()")
      return

    if arg_0.acking_enabled:
      arg_2 = tuple_pb2.AckTuple()
      arg_2.ackedtuple = int(arg_1.id)

      arg_4 = 0
      for arg_5 in arg_1.roots:
        arg_6 = arg_2.roots.add()
        arg_6.CopyFrom(arg_5)
        arg_4 += arg_5.ByteSize()
      super(BoltInstance, arg_0).admit_control_tuple(arg_2, arg_4, False)

    arg_7 = (time.time() - arg_1.creation_time) * system_constants.SEC_TO_NS
    arg_0.pplan_helper.context.invoke_hook_bolt_Func(arg_1, arg_7)
    arg_0.bolt_metrics.Funced_tuple(arg_1.stream, arg_1.component, arg_7)


def Func(arg_0, arg_1):
  '''
  Template slave config file
  '''
  arg_2 = "%s/standalone/templates/slave.template.hcl" % arg_0["config_path"]
  arg_3 = "%s/standalone/resources/slave.hcl" % arg_0["config_path"]
  arg_4 = ['"%s"' % master for master in arg_1]
  template_file(arg_2, arg_3,
                {"<nomad_masters:master_port>": ", ".join(arg_4)})


def Func(arg_0, arg_1):
  '''
  Template scheduler.yaml
  '''
  arg_2 = arg_1[0]
  arg_3 = "%s/standalone/scheduler.yaml" % arg_0["config_path"]

  arg_4 = "%s/standalone/templates/scheduler.template.yaml" \
                              % arg_0["config_path"]
  template_file(arg_4, arg_3,
                {"<scheduler_uri>": "http://%s:4646" % arg_2})


def Func(arg_0, arg_1):
  '''
  Tempate uploader.yaml
  '''
  arg_2 = arg_1[0]
  arg_3 = "%s/standalone/templates/uploader.template.yaml" \
                             % arg_0["config_path"]
  arg_4 = "%s/standalone/uploader.yaml" % arg_0["config_path"]

  template_file(arg_3, arg_4,
                {"<http_uploader_uri>": "http://%s:9000/api/v1/file/upload" % arg_2})


def Func(arg_0, arg_1):
  '''
  Template statemgr.yaml
  '''
  arg_2 = "%s/standalone/templates/statemgr.template.yaml" \
                                  % arg_0["config_path"]
  arg_3 = "%s/standalone/statemgr.yaml" % arg_0["config_path"]

  template_file(arg_2, arg_3,
                {"<zookeeper_host:zookeeper_port>": ",".join(
                    ['"%s"' % arg_4 if ":" in arg_4 else '"%s:2181"' % arg_4 for arg_4 in arg_1])})


def Func(arg_0, arg_1, arg_2):
  '''
  template heron tools
  '''
  arg_3 = "%s/standalone/templates/heron_tools.template.hcl" \
                             % arg_0["config_path"]
  arg_4 = "%s/standalone/resources/heron_tools.hcl" \
                             % arg_0["config_path"]

  arg_5 = arg_1[0]
  template_file(arg_3, arg_4,
                {
                    "<zookeeper_host:zookeeper_port>": ",".join(
                        ['%s' % arg_6 if ":" in arg_6 else '%s:2181' % arg_6 for arg_6 in arg_2]),
                    "<heron_tracker_executable>": '"%s/heron-tracker"' % config.get_heron_bin_dir(),
                    "<heron_tools_hostname>": '"%s"' % get_hostname(arg_5, arg_0),
                    "<heron_ui_executable>": '"%s/heron-ui"' % config.get_heron_bin_dir()
                })


def Func(arg_0):
  '''
  get cluster info for standalone cluster
  '''
  arg_1 = read_and_parse_roles(arg_0)
  arg_2 = list(arg_1[Role.MASTERS])
  arg_3 = list(arg_1[Role.SLAVES])
  arg_4 = list(arg_1[Role.ZOOKEEPERS])
  arg_5 = list(arg_1[Role.CLUSTER])

  # OrderedDicts are used here so that the key order can be
  # specified directly
  arg_6 = OrderedDict()
  arg_6['numNodes'] = len(arg_5)
  arg_6['nodes'] = arg_5
  arg_7 = OrderedDict()
  arg_7['masters'] = arg_2
  arg_7['slaves'] = arg_3
  arg_7['zookeepers'] = arg_4
  arg_8 = OrderedDict()
  arg_8['serviceUrl'] = get_service_url(arg_0)
  arg_8['heronUi'] = get_heron_ui_url(arg_0)
  arg_8['heronTracker'] = get_heron_tracker_url(arg_0)
  arg_6['roles'] = arg_7
  arg_6['urls'] = arg_8

  print json.dumps(arg_6, indent=2)


def Func(arg_0):
  '''
  Start a Heron standalone cluster
  '''
  arg_1 = read_and_parse_roles(arg_0)
  arg_2 = arg_1[Role.MASTERS]
  arg_3 = arg_1[Role.SLAVES]
  arg_4 = arg_1[Role.ZOOKEEPERS]
  Log.info("Roles:")
  Log.info(" - Master Servers: %s" % list(arg_2))
  Log.info(" - Slave Servers: %s" % list(arg_3))
  Log.info(" - Zookeeper Servers: %s" % list(arg_4))
  if not arg_2:
    Log.error("No master servers specified!")
    sys.exit(-1)
  if not arg_3:
    Log.error("No slave servers specified!")
    sys.exit(-1)
  if not arg_4:
    Log.error("No zookeeper servers specified!")
    sys.exit(-1)
  # make sure configs are templated
  update_config_files(arg_0)

  arg_5 = list(arg_2.union(arg_3))
  # if just local deployment
  if not (len(arg_5) == 1 and is_self(arg_5[0])):
    distribute_package(arg_1, arg_0)
  start_master_nodes(arg_2, arg_0)
  start_slave_nodes(arg_3, arg_0)
  start_api_server(arg_2, arg_0)
  start_heron_tools(arg_2, arg_0)
  Log.info("Heron standalone cluster complete!")


def Func(arg_0, arg_1):
  '''
  Start Heron tracker and UI
  '''
  arg_2 = list(arg_0)[0]
  wait_for_master_to_start(arg_2)

  arg_3 = "%s run %s >> /tmp/heron_tools_start.log 2>&1 &" \
        % (get_nomad_path(arg_1), get_heron_tools_job_file(arg_1))
  Log.info("Starting Heron Tools on %s" % arg_2)

  if not is_self(arg_2):
    arg_3 = ssh_remote_execute(arg_3, arg_2, arg_1)
  Log.debug(arg_3)
  arg_4 = subprocess.Popen(arg_3,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

  arg_5 = arg_4.wait()
  arg_6 = arg_4.communicate()
  Log.debug("return code: %s output: %s" % (arg_5, arg_6))
  if arg_5 != 0:
    Log.error("Failed to start Heron Tools on %s with error:\n%s" % (arg_2, arg_6[1]))
    sys.exit(-1)

  wait_for_job_to_start(arg_2, "heron-tools")
  Log.info("Done starting Heron Tools")


def Func(arg_0):
  '''
  Wait for a nomad master to start
  '''
  arg_1 = 0
  while True:
    try:
      arg_2 = requests.get("http://%s:4646/v1/status/leader" % arg_0)
      if arg_2.status_code == 200:
        break
    except:
      Log.debug(sys.exc_info()[0])
      Log.info("Waiting for cluster to come up... %s" % arg_1)
      time.sleep(1)
      if arg_1 > 10:
        Log.error("Failed to start Nomad Cluster!")
        sys.exit(-1)
    arg_1 = arg_1 + 1


def Func(arg_0, arg_1):
  '''
  Tar a directory
  '''
  with tarfile.open(arg_0, "w:gz") as tar:
    tar.add(arg_1, arcname=os.path.basename(arg_1))


def Func(arg_0, arg_1):
  '''
  Start master nodes
  '''
  arg_2 = []
  for arg_3 in arg_0:
    Log.info("Starting master on %s" % arg_3)
    arg_4 = "%s agent -config %s >> /tmp/nomad_server_log 2>&1 &" \
          % (get_nomad_path(arg_1), get_nomad_master_config_file(arg_1))
    if not is_self(arg_3):
      arg_4 = ssh_remote_execute(arg_4, arg_3, arg_1)
    Log.debug(arg_4)
    arg_5 = subprocess.Popen(arg_4,
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    arg_2.append({"pid": arg_5, "dest": arg_3})

  arg_6 = []
  for arg_7 in arg_2:
    arg_5 = arg_7["pid"]
    arg_8 = arg_5.wait()
    arg_9 = arg_5.communicate()
    Log.debug("return code: %s output: %s" % (arg_8, arg_9))
    if arg_8 != 0:
      arg_6.append("Failed to start master on %s with error:\n%s" % (arg_7["dest"], arg_9[1]))

  if arg_6:
    for arg_10 in arg_6:
      Log.error(arg_10)
    sys.exit(-1)

  Log.info("Done starting masters")


def Func(arg_0):
  '''
  read config files to get roles
  '''
  arg_1 = dict()

  with open(get_inventory_file(arg_0), 'r') as stream:
    try:
      arg_1 = yaml.load(stream)
    except yaml.YAMLError as exc:
      Log.error("Error parsing inventory file: %s" % exc)
      sys.exit(-1)

  if arg_2.ZOOKEEPERS not in arg_1 or not arg_1[arg_2.ZOOKEEPERS]:
    Log.error("Zookeeper servers node defined!")
    sys.exit(-1)

  if arg_2.CLUSTER not in arg_1 or not arg_1[arg_2.CLUSTER]:
    Log.error("Heron cluster nodes defined!")
    sys.exit(-1)

  # Set roles
  arg_1[arg_2.MASTERS] = set([arg_1[arg_2.CLUSTER][0]])
  arg_1[arg_2.SLAVES] = set(arg_1[arg_2.CLUSTER])
  arg_1[arg_2.ZOOKEEPERS] = set(arg_1[arg_2.ZOOKEEPERS])
  arg_1[arg_2.CLUSTER] = set(arg_1[arg_2.CLUSTER])

  return arg_1


def Func(arg_0):
  '''
  check if this host is this addr
  '''
  arg_1 = []
  for arg_2 in netifaces.interfaces():
    arg_3 = netifaces.ifaddresses(arg_2)
    if netifaces.AF_INET in arg_3:
      for arg_4 in arg_3[netifaces.AF_INET]:
        if "addr" in arg_4:
          arg_1.append(arg_4["addr"])
  return arg_0 in arg_1 or arg_0 == get_self_hostname()


def Func(arg_0):
    """
    Resolve all symbolic references that `src` points to.  Note that this
    is different than `os.path.realpath` as path components leading up to
    the final location may still be symbolic links.
    """
    while os.path.islink(arg_0):
        arg_0 = os.path.join(os.path.dirname(arg_0), os.readlink(arg_0))

    return arg_0


def Func(arg_0):
  ''' normalize raw result to table '''
  arg_1 = 20
  arg_2, arg_3 = [], 0
  for arg_4, arg_5 in arg_0.items():
    for arg_6, arg_7 in arg_5.items():
      for arg_8 in arg_7:
        arg_3 += 1
        if arg_3 > arg_1:
          continue
        else:
          arg_2.append([arg_4, arg_6, arg_8])
  arg_9 = ['role', 'env', 'topology']
  arg_10 = 0 if arg_3 <= arg_1 else arg_3 - arg_1
  return arg_2, arg_9, arg_10


def Func(arg_0):
    """
    Monitor the rootpath and call the callback
    corresponding to the change.
    This Funcing happens periodically. This function
    is called in a seperate thread from the main thread,
    because it sleeps for the intervals between each poll.
    """

    def trigger_watches_based_on_files(arg_1, arg_2, arg_3, arg_4):
      """
      For all the topologies in the watchers, check if the data
      in directory has changed. Trigger the callback if it has.
      """
      for arg_5, arg_6 in arg_1.items():
        arg_7 = os.path.join(arg_2, arg_5)
        arg_8 = ""
        if os.path.exists(arg_7):
          with open(os.path.join(arg_2, arg_5)) as f:
            arg_8 = f.read()
        if arg_5 not in arg_3 or arg_8 != arg_3[arg_5]:
          arg_9 = arg_4()
          arg_9.ParseFromString(arg_8)
          for arg_10 in arg_6:
            arg_10(arg_9)
          arg_3[arg_5] = arg_8

    while not arg_0.Funcing_thread_stop_signal:
      arg_11 = arg_0.get_topologies_path()

      arg_12 = []
      if os.path.isdir(arg_11):
        arg_12 = list(filter(
            lambda f: os.path.isfile(os.path.join(arg_11, f)),
            os.listdir(arg_11)))
      if set(arg_12) != set(arg_0.topologies_directory):
        for arg_10 in arg_0.topologies_watchers:
          arg_10(arg_12)
      arg_0.topologies_directory = arg_12

      trigger_watches_based_on_files(
          arg_0.topology_watchers, arg_11, arg_0.topologies_directory, Topology)

      # Get the directory name for execution state
      arg_14 = os.path.dirname(arg_0.get_execution_state_path(""))
      trigger_watches_based_on_files(
          arg_0.execution_state_watchers, arg_14,
          arg_0.execution_state_directory, ExecutionState)

      # Get the directory name for packing_plan
      arg_15 = os.path.dirname(arg_0.get_packing_plan_path(""))
      trigger_watches_based_on_files(
          arg_0.packing_plan_watchers, arg_15, arg_0.packing_plan_directory, PackingPlan)

      # Get the directory name for pplan
      arg_16 = os.path.dirname(arg_0.get_pplan_path(""))
      trigger_watches_based_on_files(
          arg_0.pplan_watchers, arg_16,
          arg_0.pplan_directory, PhysicalPlan)

      # Get the directory name for tmaster
      arg_17 = os.path.dirname(arg_0.get_tmaster_path(""))
      trigger_watches_based_on_files(
          arg_0.tmaster_watchers, arg_17,
          arg_0.tmaster_directory, TMasterLocation)

      # Get the directory name for scheduler location
      arg_18 = os.path.dirname(arg_0.get_scheduler_location_path(""))
      trigger_watches_based_on_files(
          arg_0.scheduler_location_watchers, arg_18,
          arg_0.scheduler_location_directory, SchedulerLocation)

      # Sleep for some time
      arg_0.event.wait(timeout=5)


def Func(arg_0, arg_1, arg_2=None):
    """
    Get physical plan of a topology
    """
    if arg_2:
      arg_0.pplan_watchers[arg_1].append(arg_2)
    else:
      arg_3 = arg_0.Func_path(arg_1)
      with open(arg_3) as f:
        arg_4 = f.read()
        arg_5 = PhysicalPlan()
        arg_5.ParseFromString(arg_4)
        return arg_5


def Func(arg_0, arg_1, arg_2=None):
    """
    Get execution state
    """
    if arg_2:
      arg_0.execution_state_watchers[arg_1].append(arg_2)
    else:
      arg_3 = arg_0.Func_path(arg_1)
      with open(arg_3) as f:
        arg_4 = f.read()
        arg_5 = ExecutionState()
        arg_5.ParseFromString(arg_4)
        return arg_5


def Func(arg_0, arg_1, arg_2=None):
    """
    Get scheduler location
    """
    if arg_2:
      arg_0.scheduler_location_watchers[arg_1].append(arg_2)
    else:
      arg_3 = arg_0.Func_path(arg_1)
      with open(arg_3) as f:
        arg_4 = f.read()
        arg_5 = SchedulerLocation()
        arg_5.ParseFromString(arg_4)
        return arg_5


def Func():
  """Creates SocketOptions object from a given sys_config dict"""
  arg_0 = system_config.get_sys_config()
  arg_1 = [const.INSTANCE_NETWORK_WRITE_BATCH_SIZE_BYTES,
              const.INSTANCE_NETWORK_WRITE_BATCH_TIME_MS,
              const.INSTANCE_NETWORK_READ_BATCH_SIZE_BYTES,
              const.INSTANCE_NETWORK_READ_BATCH_TIME_MS,
              const.INSTANCE_NETWORK_OPTIONS_SOCKET_RECEIVED_BUFFER_SIZE_BYTES,
              const.INSTANCE_NETWORK_OPTIONS_SOCKET_SEND_BUFFER_SIZE_BYTES]

  Log.debug("In Func()")
  try:
    arg_2 = [int(arg_0[opt]) for opt in arg_1]
    arg_3 = SocketOptions(*arg_2)
    return arg_3
  except ValueError as e:
    # couldn't convert to int
    raise ValueError("Invalid value in sys_config: %s" % str(e))
  except KeyError as e:
    # option key was not found
    raise KeyError("Incomplete sys_config: %s" % str(e))


def Func():
    """Retrieves heron options from the `HERON_OPTIONS` environment variable.

    Heron options have the following format:

        cmdline.topologydefn.tmpdirectory=/var/folders/tmpdir
        cmdline.topology.initial.state=PAUSED

    In this case, the returned map will contain:

        #!json
        {
          "cmdline.topologydefn.tmpdirectory": "/var/folders/tmpdir",
          "cmdline.topology.initial.state": "PAUSED"
        }

    Currently supports the following options natively:

    - `cmdline.topologydefn.tmpdirectory`: (required) the directory to which this
    topology's defn file is written
    - `cmdline.topology.initial.state`: (default: "RUNNING") the initial state of the topology
    - `cmdline.topology.name`: (default: class name) topology name on deployment

    Returns: map mapping from key to value
    """
    arg_0 = os.environ.get("HERON_OPTIONS")
    if arg_0 is None:
      raise RuntimeError("HERON_OPTIONS environment variable not found")

    arg_1 = {}
    for arg_2 in arg_0.replace("%%%%", " ").split(','):
      arg_3, arg_4, arg_5 = arg_2.partition("=")
      if arg_4:
        arg_1[arg_3] = arg_5
      else:
        raise ValueError("Invalid HERON_OPTIONS part %r" % arg_2)
    return arg_1


def Func(arg_0, *arg_1):
    """Add specs to the topology

    :type specs: HeronComponentSpec
    :param specs: specs to add to the topology
    """
    for arg_2 in arg_1:
      if not isinstance(arg_2, HeronComponentSpec):
        raise TypeError("Argument to Func needs to be HeronComponentSpec, given: %s"
                        % str(arg_2))
      if arg_2.name is None:
        raise ValueError("TopologyBuilder cannot take a spec without name")
      if arg_2.name == "config":
        raise ValueError("config is a reserved name")
      if arg_2.name in arg_0._specs:
        raise ValueError("Attempting to add duplicate spec name: %r %r" % (arg_2.name, arg_2))

      arg_0._specs[arg_2.name] = arg_2


def Func(arg_0, arg_1):
    """Set topology-wide configuration to the topology

    :type config: dict
    :param config: topology-wide config
    """
    if not isinstance(arg_1, dict):
      raise TypeError("Argument to Func needs to be dict, given: %s" % str(arg_1))
    arg_0._topology_config = arg_1


def Func(arg_0):
    """Builds the topology and submits to the destination"""
    arg_1 = arg_0._construct_topo_class_dict()
    arg_2 = TopologyType(arg_0.topology_name, (Topology,), arg_1)
    arg_2.write()


def Func():
  """map from query parameter to query name"""
  arg_0 = _all_metric_queries()
  return dict(zip(arg_0[0], arg_0[1]) + zip(arg_0[2], arg_0[3]))


def Func(arg_0, arg_1, arg_2, arg_3):
  """Synced API call to get logical plans"""
  arg_4 = tornado.ioloop.IOLoop.instance()
  try:
    return arg_4.run_sync(lambda: API.Func(arg_0, arg_1, arg_2, arg_3))
  except Exception:
    Log.debug(traceback.format_exc())
    raise


def Func(*arg_0):
  """Synced API call to get topology information"""
  arg_1 = tornado.ioloop.IOLoop.instance()
  try:
    return arg_1.run_sync(lambda: API.Func(*arg_0))
  except Exception:
    Log.debug(traceback.format_exc())
    raise


def Func(arg_0, arg_1, arg_2, arg_3, arg_4):
  """Synced API call to get component metrics"""
  arg_5 = metric_queries()
  try:
    arg_6 = get_topology_metrics(arg_1, arg_2, arg_3, arg_0, [],
                                  arg_5, [0, -1], arg_4)
    return arg_6["metrics"]
  except Exception:
    Log.debug(traceback.format_exc())
    raise


def Func(arg_0=arg_1.INFO, arg_3=None):
  """ Configure logger which dumps log on terminal

  :param level: logging level: info, warning, verbose...
  :type level: logging level
  :param logfile: log file name, default to None
  :type logfile: string
  :return: None
  :rtype: None
  """

  # Remove all the existing StreamHandlers to avoid duplicate
  for arg_4 in Log.handlers:
    if isinstance(arg_4, arg_1.StreamHandler):
      Log.handlers.remove(arg_4)

  Log.setLevel(arg_0)

  # if logfile is specified, FileHandler is used
  if arg_3 is not None:
    arg_5 = "[%(asctime)s] [%(levelname)s]: %(message)s"
    arg_6 = arg_1.Formatter(fmt=arg_5, datefmt=date_format)
    arg_7 = arg_1.FileHandler(arg_3)
    arg_7.setFormatter(arg_6)
    Log.addHandler(arg_7)
  # otherwise, use StreamHandler to output to stream (stdout, stderr...)
  else:
    arg_5 = "[%(asctime)s] %(log_color)s[%(levelname)s]%(reset)s: %(message)s"
    # pylint: disable=redefined-variable-type
    arg_6 = colorlog.ColoredFormatter(fmt=arg_5, datefmt=date_format)
    arg_8 = arg_1.StreamHandler()
    arg_8.setFormatter(arg_6)
    Log.addHandler(arg_8)


def Func(arg_0, arg_1, arg_2, arg_3):
  """Initializes a rotating logger

  It also makes sure that any StreamHandler is removed, so as to avoid stdout/stderr
  constipation issues
  """
  logging.basicConfig()

  arg_4 = logging.getLogger()
  arg_5 = "[%(asctime)s] [%(levelname)s] %(filename)s: %(message)s"

  arg_4.setLevel(arg_0)
  arg_6 = RotatingFileHandler(arg_1, maxBytes=arg_3, backupCount=arg_2)
  arg_6.setFormatter(logging.Formatter(fmt=arg_5, datefmt=date_format))
  arg_4.addHandler(arg_6)

  for arg_6 in arg_4.handlers:
    arg_4.debug("Associated handlers - " + str(arg_6))
    if isinstance(arg_6, logging.StreamHandler):
      arg_4.debug("Removing StreamHandler: " + str(arg_6))
      arg_4.handlers.remove(arg_6)


def Func(arg_0):
  """simply set verbose level based on command-line args

  :param cl_args: CLI arguments
  :type cl_args: dict
  :return: None
  :rtype: None
  """
  if 'verbose' in arg_0 and arg_0['verbose']:
    configure(logging.DEBUG)
  else:
    configure(logging.INFO)


def Func(arg_0):
    """Returns Spout protobuf message"""
    arg_1 = topology_pb2.Spout()
    arg_1.comp.CopyFrom(arg_0._get_base_component())

    # Add output streams
    arg_0._add_out_streams(arg_1)
    return arg_1


def Func(arg_0):
    """Returns Component protobuf message"""
    arg_1 = topology_pb2.Component()
    arg_1.name = arg_0.name
    arg_1.spec = topology_pb2.ComponentObjectSpec.Value("PYTHON_CLASS_NAME")
    arg_1.class_name = arg_0.python_class_path
    arg_1.config.CopyFrom(arg_0._get_comp_config())
    return arg_1


def Func(arg_0):
    """Returns component-specific Config protobuf message

    It first adds ``topology.component.parallelism``, and is overriden by
    a user-defined component-specific configuration, specified by spec().
    """
    arg_1 = topology_pb2.Config()

    # first add parallelism
    arg_2 = arg_1.kvs.add()
    arg_2.key = TOPOLOGY_COMPONENT_PARALLELISM
    arg_2.value = str(arg_0.parallelism)
    arg_2.type = topology_pb2.ConfigValueType.Value("STRING_VALUE")

    # iterate through self.custom_config
    if arg_0.custom_config is not None:
      arg_5 = arg_0._sanitize_config(arg_0.custom_config)
      for arg_2, arg_3 in arg_5.items():
        if isinstance(arg_3, str):
          arg_6 = arg_1.kvs.add()
          arg_6.key = arg_2
          arg_6.value = arg_3
          arg_6.type = topology_pb2.ConfigValueType.Value("STRING_VALUE")
        else:
          # need to serialize
          arg_6 = arg_1.kvs.add()
          arg_6.key = arg_2
          arg_6.serialized_value = default_serializer.serialize(arg_3)
          arg_6.type = topology_pb2.ConfigValueType.Value("PYTHON_SERIALIZED_VALUE")

    return arg_1


def Func(arg_0, arg_1):
    """Adds outputs to a given protobuf Bolt or Spout message"""
    if arg_0.outputs is None:
      return

    # sanitize outputs and get a map <stream_id -> out fields>
    arg_2 = arg_0._sanitize_outputs()

    for arg_3, arg_4 in arg_2.items():
      arg_5 = arg_1.outputs.add()
      arg_5.stream.CopyFrom(arg_0._get_stream_id(arg_0.name, arg_3))
      arg_5.schema.CopyFrom(arg_0._get_stream_schema(arg_4))


def Func(arg_0):
    """Returns a set of output stream ids registered for this component"""
    if arg_0.outputs is None:
      return set()

    if not isinstance(arg_0.outputs, (list, tuple)):
      raise TypeError("Argument to outputs must be either list or tuple, given: %s"
                      % str(type(arg_0.outputs)))
    arg_1 = []
    for arg_2 in arg_0.outputs:
      if not isinstance(arg_2, (str, Stream)):
        raise TypeError("Outputs must be a list of strings or Streams, given: %s" % str(arg_2))
      arg_1.append(Stream.DEFAULT_STREAM_ID if isinstance(arg_2, str) else arg_2.stream_id)
    return set(arg_1)


def Func(arg_0, arg_1):
    """Returns a StreamId protobuf message"""
    arg_2 = topology_pb2.StreamId()
    arg_2.id = arg_1
    arg_2.component_name = arg_0
    return arg_2


def Func(arg_0):
    """Returns a StreamSchema protobuf message"""
    arg_1 = topology_pb2.StreamSchema()
    for arg_2 in arg_0:
      arg_3 = arg_1.keys.add()
      arg_3.key = arg_2
      arg_3.type = topology_pb2.Type.Value("OBJECT")

    return arg_1


def Func(arg_0):
    """Returns Func of this GlobalStreamId

    Note that if HeronComponentSpec is specified as componentId and its name is not yet
    available (i.e. when ``name`` argument was not given in ``spec()`` method in Bolt or Spout),
    this property returns a message with uuid. However, this is provided only for safety
    with __eq__(), __str__(), and __hash__() methods, and not meant to be called explicitly
    before TopologyType class finally sets the name attribute of HeronComponentSpec.
    """
    if isinstance(arg_0._Func, HeronComponentSpec):
      if arg_0._Func.name is None:
        # HeronComponentSpec instance's name attribute might not be available until
        # TopologyType metaclass finally sets it. This statement is to support __eq__(),
        # __hash__() and __str__() methods with safety, as raising Exception is not
        # appropriate this case.
        return "<No name available for HeronComponentSpec yet, uuid: %s>" % arg_0._Func.uuid
      return arg_0._Func.name
    elif isinstance(arg_0._Func, str):
      return arg_0._Func
    else:
      raise ValueError("Component Id for this GlobalStreamId is not properly set: <%s:%s>"
                       % (str(type(arg_0._Func)), str(arg_0._Func)))


def Func(arg_0, arg_1, arg_2, arg_3):
    """Registers a new metric to this context"""
    arg_4 = arg_0.get_metrics_collector()
    arg_4.Func(arg_1, arg_2, arg_3)


def Func(arg_0, arg_1):
    """Returns the declared inputs to specified component

    :return: map <streamId namedtuple (same structure as protobuf msg) -> gtype>, or
             None if not found
    """
    # this is necessary because protobuf message is not hashable
    arg_2 = namedtuple('StreamId', 'id, component_name')
    if arg_1 in arg_0.inputs:
      arg_3 = {}
      for arg_4 in arg_0.inputs.get(arg_1):
        arg_5 = arg_2(id=arg_4.stream.id, component_name=arg_4.stream.component_name)
        arg_3[arg_5] = arg_4.gtype
      return arg_3
    else:
      return None


def Func(arg_0, arg_1, arg_2):
    """invoke task hooks for every time spout acks a tuple

    :type message_id: str
    :param message_id: message id to which an acked tuple was anchored
    :type complete_latency_ns: float
    :param complete_latency_ns: complete latency in nano seconds
    """
    if len(arg_0.task_hooks) > 0:
      arg_3 = SpoutAckInfo(arg_1=arg_1,
                                    spout_task_id=arg_0.get_task_id(),
                                    complete_latency_ms=arg_2 *
                                    system_constants.NS_TO_MS)
      for arg_4 in arg_0.task_hooks:
        arg_4.spout_ack(arg_3)


def Func(arg_0, arg_1, arg_2):
    """invoke task hooks for every time spout fails a tuple

    :type message_id: str
    :param message_id: message id to which a failed tuple was anchored
    :type fail_latency_ns: float
    :param fail_latency_ns: fail latency in nano seconds
    """
    if len(arg_0.task_hooks) > 0:
      arg_3 = SpoutFailInfo(arg_1=arg_1,
                                      spout_task_id=arg_0.get_task_id(),
                                      fail_latency_ms=arg_2 * system_constants.NS_TO_MS)
      for arg_4 in arg_0.task_hooks:
        arg_4.spout_fail(arg_3)


def Func(arg_0, arg_1, arg_2):
    """invoke task hooks for every time bolt processes a tuple

    :type heron_tuple: HeronTuple
    :param heron_tuple: tuple that is executed
    :type execute_latency_ns: float
    :param execute_latency_ns: execute latency in nano seconds
    """
    if len(arg_0.task_hooks) > 0:
      arg_3 = \
        BoltExecuteInfo(arg_1=arg_1,
                        executing_task_id=arg_0.get_task_id(),
                        execute_latency_ms=arg_2 * system_constants.NS_TO_MS)
      for arg_4 in arg_0.task_hooks:
        arg_4.bolt_execute(arg_3)


def Func(arg_0, arg_1, arg_2):
    """invoke task hooks for every time bolt fails a tuple

    :type heron_tuple: HeronTuple
    :param heron_tuple: tuple that is failed
    :type fail_latency_ns: float
    :param fail_latency_ns: fail latency in nano seconds
    """
    if len(arg_0.task_hooks) > 0:
      arg_3 = BoltFailInfo(arg_1=arg_1,
                                    failing_task_id=arg_0.get_task_id(),
                                    fail_latency_ms=arg_2 * system_constants.NS_TO_MS)
      for arg_4 in arg_0.task_hooks:
        arg_4.bolt_fail(arg_3)


def Func(arg_0, arg_1, arg_2):
  '''
  Extract and execute the java files inside the tar and then add topology
  definition file created by running submitTopology

  We use the packer to make a package for the tar and dump it
  to a well-known location. We then run the main method of class
  with the specified arguments. We pass arguments as an environment variable HERON_OPTIONS.
  This will run the jar file with the topology class name.

  The submitter inside will write out the topology defn file to a location
  that we specify. Then we write the topology defn file to a well known
  packer location. We then write to appropriate places in zookeeper
  and launch the aurora jobs
  :param cl_args:
  :param unknown_args:
  :param tmp_dir:
  :return:
  '''
  # execute main of the topology to create the topology definition
  arg_3 = arg_0['topology-file-name']
  arg_4 = arg_0['topology_main_jvm_property']
  arg_5 = arg_0['topology-class-name']
  arg_6 = execute.heron_tar(
      arg_5,
      arg_3,
      tuple(arg_1),
      arg_2,
      arg_4)

  result.render(arg_6)

  if not result.is_successful(arg_6):
    arg_7 = ("Failed to create topology definition " \
      "file when executing class '%s' of file '%s'") % (arg_5, arg_3)
    arg_6.add_context(arg_7)
    return arg_6

  return launch_topologies(arg_0, arg_3, arg_2)


def Func(arg_0, arg_1):
  """
  Makes the http endpoint for the heron shell
  if shell port is present, otherwise returns None.
  """
  # Format: container_<id>_<instance_id>
  arg_2 = arg_0["physical_plan"]
  arg_3 = arg_2["instances"][arg_1]["stmgrId"]
  arg_4 = arg_2["stmgrs"][arg_3]["host"]
  arg_5 = arg_2["stmgrs"][arg_3]["shell_port"]
  return "http://%s:%d" % (arg_4, arg_5)


def Func(arg_0, arg_1, arg_2, arg_3, arg_4):
  """
  Make the url for log-file data in heron-shell
  from the info stored in stmgr.
  """
  return "http://%s:%d/filedata/log-files/%s.log.0?offset=%s&length=%s" % \
    (arg_0, arg_1, arg_2, arg_3, arg_4)


def Func(arg_0, arg_1):
    """Sends this outgoing packet to dispatcher's socket"""
    if arg_0.sent_complete:
      return

    arg_2 = arg_1.Func(arg_0.to_Func)
    arg_0.to_Func = arg_0.to_Func[arg_2:]


def Func(arg_0, arg_1):
    """Creates an IncomingPacket object from header and data

    This method is for testing purposes
    """
    arg_2 = IncomingPacket()
    arg_2.header = arg_0
    arg_2.data = arg_1

    if len(arg_0) == HeronProtocol.HEADER_SIZE:
      arg_2.is_header_read = True
      if len(arg_1) == arg_2.get_datasize():
        arg_2.is_complete = True

    return arg_2


def Func(arg_0, arg_1):
    """Reads incoming data from asyncore.dispatcher"""
    try:
      if not arg_0.is_header_Func:
        # try Funcing header
        arg_2 = HeronProtocol.HEADER_SIZE - len(arg_0.header)
        arg_0.header += arg_1.recv(arg_2)
        if len(arg_0.header) == HeronProtocol.HEADER_SIZE:
          arg_0.is_header_Func = True
        else:
          Log.debug("Header Func incomplete; Func %d bytes of header" % len(arg_0.header))
          return

      if arg_0.is_header_Func and not arg_0.is_complete:
        # try Funcing data
        arg_2 = arg_0.get_datasize() - len(arg_0.data)
        arg_0.data += arg_1.recv(arg_2)
        if len(arg_0.data) == arg_0.get_datasize():
          arg_0.is_complete = True
    except socket.error as e:
      if e.errno == socket.errno.EAGAIN or e.errno == socket.errno.EWOULDBLOCK:
        # Try again later -> call continue_Func later
        Log.debug("Try again error")
      else:
        # Fatal error
        Log.debug("Fatal error when Funcing IncomingPacket")
        raise RuntimeError("Fatal error occured in IncomingPacket.Func()")


def Func(arg_0):
  """Reads yaml config file and returns auto-typed config_dict"""
  if not arg_0.endswith(".yaml"):
    raise ValueError("Config file not yaml")

  with open(arg_0, 'r') as f:
    arg_1 = yaml.load(f)

  return arg_1


def Func(arg_0):
    """Send messages in out_stream to the Stream Manager"""
    while not arg_0.out_stream.is_empty() and arg_0._stmgr_client.is_registered:
      arg_1 = arg_0.out_stream.poll()
      if isinstance(arg_1, tuple_pb2.HeronTupleSet):
        arg_1.src_task_id = arg_0.my_pplan_helper.my_task_id
        arg_0.gateway_metrics.update_sent_packet(arg_1.ByteSize())
      arg_0._stmgr_client.send_message(arg_1)


def Func(arg_0, arg_1):
    """Called when state change is commanded by stream manager"""
    assert arg_0.my_pplan_helper is not None
    assert arg_0.my_instance is not None and arg_0.my_instance.py_class is not None

    if arg_0.my_pplan_helper.get_topology_state() != arg_1.get_topology_state():
      # handle state change
      # update the pplan_helper
      arg_0.my_pplan_helper = arg_1
      if arg_1.is_topology_running():
        if not arg_0.is_instance_started:
          arg_0.start_instance_if_possible()
        arg_0.my_instance.py_class.invoke_activate()
      elif arg_1.is_topology_paused():
        arg_0.my_instance.py_class.invoke_deactivate()
      else:
        raise RuntimeError("Unexpected TopologyState update: %s" % arg_1.get_topology_state())
    else:
      Log.info("Topology state remains the same.")


def Func(arg_0, arg_1, arg_2):
    """Checks if a given stream_id and tuple matches with the output schema

    :type stream_id: str
    :param stream_id: stream id into which tuple is sent
    :type tup: list
    :param tup: tuple that is going to be sent
    """
    # do some checking to make sure that the number of fields match what's expected
    arg_3 = arg_0._output_schema.get(arg_1, None)
    if arg_3 is None:
      raise RuntimeError("%s emitting to stream %s but was not declared in output fields"
                         % (arg_0.my_component_name, arg_1))
    elif arg_3 != len(arg_2):
      raise RuntimeError("Number of fields emitted in stream %s does not match what's expected. "
                         "Expected: %s, Observed: %s" % (arg_1, arg_3, len(arg_2)))


def Func(arg_0, arg_1, arg_2, arg_3, arg_4):
    """Adds the target component

    :type stream_id: str
    :param stream_id: stream id into which tuples are emitted
    :type task_ids: list of str
    :param task_ids: list of task ids to which tuples are emitted
    :type grouping: ICustomStreamGrouping object
    :param grouping: custom grouping to use
    :type source_comp_name: str
    :param source_comp_name: source component name
    """
    if arg_1 not in arg_0.targets:
      arg_0.targets[arg_1] = []
    arg_0.targets[arg_1].append(Target(arg_2, arg_3, arg_4))


def Func(arg_0, arg_1):
    """Prepares the custom grouping for this component"""
    for arg_2, arg_3 in arg_0.targets.items():
      for arg_4 in arg_3:
        arg_4.Func(arg_1, arg_2)


def Func(arg_0):
  """
  Format a line in the directory list based on the file's type and other attributes.
  """
  arg_1 = arg_0.st_mode

  arg_2 = (arg_1 & 0o700) >> 6
  arg_3 = (arg_1 & 0o070) >> 3
  arg_4 = (arg_1 & 0o7)

  def stat_type(arg_5):
    ''' stat type'''
    if stat.S_ISDIR(arg_5):
      return 'd'
    elif stat.S_ISSOCK(arg_5):
      return 's'
    else:
      return '-'

  def triple(arg_5):
    ''' triple '''
    return '%c%c%c' % (
        'r' if arg_5 & 0b100 else '-',
        'w' if arg_5 & 0b010 else '-',
        'x' if arg_5 & 0b001 else '-')

  return ''.join([stat_type(arg_1), triple(arg_2), triple(arg_3), triple(arg_4)])


def Func(arg_0):
  """
  Format the date associated with a file to be displayed in directory listing.
  """
  arg_1 = datetime.now()
  arg_2 = datetime.fromtimestamp(arg_0)
  return '%s %2d %5s' % (
      arg_2.strftime('%b'), arg_2.day,
      arg_2.year if arg_2.year != arg_1.year else arg_2.strftime('%H:%M'))


def Func(arg_0, arg_1):
  """
  Prefix to a filename in the directory listing. This is to make the
  listing similar to an output of "ls -alh".
  """
  try:
    arg_2 = pwd.getpwuid(arg_1.st_uid)
    arg_3 = arg_2.pw_name
  except KeyError:
    arg_3 = arg_1.st_uid

  try:
    arg_4 = grp.getgrgid(arg_1.st_gid)
    arg_5 = arg_4.gr_name
  except KeyError:
    arg_5 = arg_1.st_gid

  return '%s %3d %10s %10s %10d %s' % (
      format_mode(arg_1),
      arg_1.st_nlink,
      arg_3,
      arg_5,
      arg_1.st_size,
      format_mtime(arg_1.st_mtime),
  )


def Func(arg_0, arg_1=-1, arg_2=-1, arg_3=False):
  """
  Read a chunk of a file from an offset upto the length.
  """
  try:
    arg_2 = int(arg_2)
    arg_1 = int(arg_1)
  except ValueError:
    return {}

  if not os.path.isfile(arg_0):
    return {}

  try:
    arg_4 = os.stat(arg_0)
  except Exception:
    return {}

  if arg_1 == -1:
    arg_1 = arg_4.st_size

  if arg_2 == -1:
    arg_2 = arg_4.st_size - arg_1

  with open(arg_0, "r") as fp:
    fp.seek(arg_1)
    try:
      arg_5 = fp.read(arg_2)
    except IOError:
      return {}

  if arg_5:
    arg_5 = _escape_data(arg_5) if arg_3 else arg_5
    return dict(arg_1=arg_1, arg_2=len(arg_5), arg_5=arg_5)

  return dict(arg_1=arg_1, arg_2=0)


def Func(arg_0, arg_1, arg_2):
  """
  Runs the command and returns its stdout and stderr.
  """
  arg_3 = subprocess.Popen(arg_0, arg_6=subprocess.PIPE,
                             arg_7=subprocess.PIPE, arg_1=arg_1, arg_2=arg_2)
  arg_4, arg_5 = proc.async_stdout_stderr_builder(arg_3)
  arg_3.wait()
  arg_6, arg_7 = arg_4.result(), arg_5.result()
  return {'command': ' '.join(arg_0), 'stderr': arg_7, 'stdout': arg_6}


def Func(arg_0):
  """
  Feed output of one command to the next and return final output
  Returns string output of Funced application of commands.
  """
  arg_1 = ' | '.join(map(lambda x: ' '.join(x), arg_0))
  arg_2 = functools.reduce(pipe, [None] + arg_0)
  arg_3 = proc.async_stdout_builder(arg_2)
  arg_2.wait()
  return {
      'command': arg_1,
      'stdout': arg_3.result()
  }


def Func(arg_0):
  """ normalize raw metrics API result to table """
  arg_1 = tracker_access.metric_queries()
  arg_2 = tracker_access.queries_map()
  arg_3 = arg_0.values()[0].keys()
  arg_4 = []
  for arg_5 in arg_3:
    arg_6 = [arg_5]
    for arg_7 in arg_1:
      try:
        arg_6.append(str(arg_0[arg_7][arg_5]))
      except KeyError:
        pass
    arg_4.append(arg_6)
  arg_8 = ['container id'] + [arg_2[k] for k in arg_1 if k in arg_0.keys()]
  return arg_4, arg_8


def Func(arg_0, arg_1, arg_2, arg_3):
  """ run metrics subcommand """
  arg_4, arg_5, arg_6 = arg_2['cluster'], arg_2['role'], arg_2['environ']
  arg_7 = arg_2['topology-name']
  try:
    arg_8 = tracker_access.get_topology_info(arg_4, arg_6, arg_7, arg_5)
    arg_9 = arg_8['physical_plan']['spouts'].keys()
    arg_10 = arg_8['physical_plan']['bolts'].keys()
    arg_11 = arg_9 + arg_10
    arg_12 = arg_2['component']
    if arg_12:
      if arg_12 in arg_11:
        arg_11 = [arg_12]
      else:
        Log.error('Unknown component: \'%s\'' % arg_12)
        raise
  except Exception:
    Log.error("Fail to connect to tracker: \'%s\'", arg_2["tracker_url"])
    return False
  arg_13 = []
  for arg_14 in arg_11:
    try:
      arg_15 = tracker_access.get_component_metrics(arg_14, arg_4, arg_6, arg_7, arg_5)
    except:
      Log.error("Fail to connect to tracker: \'%s\'", arg_2["tracker_url"])
      return False
    arg_16, arg_17 = to_table(arg_15)
    arg_13.append((arg_14, arg_16, arg_17))
  for arg_18, (arg_14, arg_16, arg_17) in enumerate(arg_13):
    if arg_18 != 0:
      print('')
    print('\'%s\' metrics:' % arg_14)
    print(tabulate(arg_16, headers=arg_17))
  return True


def Func(arg_0, arg_1, arg_2, arg_3):
  """ run containers subcommand """
  arg_4, arg_5, arg_6 = arg_2['cluster'], arg_2['role'], arg_2['environ']
  arg_7 = arg_2['topology-name']
  arg_8 = arg_2['id']
  try:
    arg_9 = tracker_access.get_topology_info(arg_4, arg_6, arg_7, arg_5)
  except:
    Log.error("Fail to connect to tracker: \'%s\'", arg_2["tracker_url"])
    return False
  arg_10 = arg_9['physical_plan']['stmgrs']
  arg_11, arg_12 = set(), set()
  for arg_13, arg_14 in arg_9['physical_plan']['bolts'].items():
    arg_11 = arg_11 | set(arg_14)
  for arg_13, arg_15 in arg_9['physical_plan']['spouts'].items():
    arg_12 = arg_12 | set(arg_15)
  arg_16 = arg_10.keys()
  arg_16.sort()
  if arg_8 is not None:
    try:
      arg_17 = arg_8 - 1
      if arg_17 < 0:
        raise
      arg_16 = [arg_16[arg_17]]
    except:
      Log.error('Invalid container id: %d' % arg_8)
      return False
  arg_18 = []
  for arg_19, arg_20 in enumerate(arg_16):
    arg_21 = arg_19 + 1
    arg_22 = arg_10[arg_20]["host"]
    arg_23 = arg_10[arg_20]["port"]
    arg_24 = arg_10[arg_20]["pid"]
    arg_25 = arg_10[arg_20]["instance_ids"]
    arg_26 = len([instance for instance in arg_25 if instance in arg_11])
    arg_27 = len([instance for instance in arg_25 if instance in arg_12])
    arg_18.append([arg_21, arg_22, arg_23, arg_24, arg_26, arg_27, len(arg_25)])
  arg_28 = ["container", "host", "port", "pid", "#bolt", "#spout", "#instance"]
  sys.stdout.flush()
  print(tabulate(arg_18, arg_28=arg_28))
  return True


def Func(arg_0, arg_1, arg_2, arg_3=None):
    """Creates a HeronTuple

    :param stream: protobuf message ``StreamId``
    :param tuple_key: tuple id
    :param values: a list of values
    :param roots: a list of protobuf message ``RootId``
    """
    arg_4 = arg_0.component_name
    arg_5 = arg_0.id
    arg_6 = arg_3[0].taskid if arg_3 is not None and len(arg_3) > 0 else None
    return HeronTuple(id=str(arg_1), component=arg_4, arg_0=arg_5,
                      task=arg_6, arg_2=arg_2, creation_time=time.time(), arg_3=arg_3)