messageboard-2022-08-31-1241.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









690569066907690869096910691169126913691469156916691769186919692069216922692369246925 69266927692869296930693169326933693469356936693769386939694069416942694369446945   6946694769486949695069516952695369546955 69566957695869596960696169626963696469656966696769686969697069716972697369746975








69816982698369846985698669876988698969906991699269936994699569966997699869997000 7001700270037004700570067007700870097010701170127013701470157016701770187019702070217022








70657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105








721172127213721472157216721772187219722072217222722372247225722672277228722972307231 72327233723472357236723772387239724072417242724372447245724672477248724972507251











                            <----SKIPPED LINES---->






def MakeVersionCopy(python_prefix):
  """Copies current instance of python file into repository."""
  file_extension = '.py'

  live_name = python_prefix + '.py'
  live_path = os.path.join(CODE_REPOSITORY, live_name)

  epoch = os.path.getmtime(live_path)
  last_modified_suffix = EpochDisplayTime(
      epoch, format_string='-%Y-%m-%d-%H%M')
  version_name = python_prefix + last_modified_suffix + file_extension
  version_path = os.path.join(VERSION_REPOSITORY, version_name)

  if not os.path.exists(version_path):
    shutil.copyfile(live_path, version_path)
  return version_name


def DumpMemorySnapsnot(configuration, iteration, main_start_time):

  """Dump the memory snapshot to disk for later analysis.

  This dumps the memory snapshot to disk with a file name defined by the
  timestamp and iteration, in the directory MEMORY_DIRECTORY, for later
  analysis by tracemalloc load class; this can be used to do a memory
  leak detection by looking at a single snapshot, or potentially by doing
  a compare_to to look at the deltas from an earlier snapshot.

  If the 'memory' configuration is greater than 0, then snapshotting is
  enabled and we dump the configuration every time iteration is evenly
  divisible by 1000 times the 'memory' setting (because the setting
  as described on the settings.php page is in thousands of iterations).

  Args:
    configuration: dictionary of configuration attributes.
    iteration: counter that indicates how many times through the main
      loop we are.
    main_start_time: time stamp of when the program first started, to
      be recorded in file names, so that associated memory dumps
      can be more easily grouped, analyzed, and purged together.



  """
  iteration_divisor = configuration.get('memory', 0)
  if not iteration_divisor:
    return
  if not iteration % (iteration_divisor * 1000):
    main_time_stamp = EpochDisplayTime(
        main_start_time, format_string='%m-%d-%H%M%S')
    this_time_stamp = EpochDisplayTime(
        time.time(), format_string='%Y-%m-%d-%H%M')
    file_name = '%s-%s-%d.dump' % (main_time_stamp, this_time_stamp, iteration)

    full_path_name = os.path.join(MEMORY_DIRECTORY, file_name)
    snapshot = tracemalloc.take_snapshot()
    snapshot.dump(full_path_name)


def main():
  """Traffic cop between radio, configuration, and messageboard.

  This is the main logic, checking for new flights, augmenting the radio
  signal with additional web-scraped data, and generating messages in a
  form presentable to the messageboard.
  """
  VersionControl()

  # Since this clears log files, it should occur first before we start logging
  if '-s' in sys.argv:
    global SIMULATION_COUNTER
    SimulationSetup()

  last_heartbeat_time = HeartbeatRestart()




                            <----SKIPPED LINES---->





  # Redirect any errors to a log file instead of the screen, and add a datestamp
  if not SIMULATION:
    sys.stderr = open(STDERR_FILE, 'a')
    Log('', STDERR_FILE)

  init_timing.append((time.time(), 1))
  Log('Starting up process %d' % os.getpid())
  already_running_ids = FindRunningParents()
  if already_running_ids:
    for pid in already_running_ids:
      Log('Sending termination signal to %d' % pid)
      os.kill(pid, signal.SIGTERM)
  init_timing.append((time.time(), 2))

  SetPinMode()

  configuration = ReadAndParseSettings(CONFIG_FILE)
  Log('Read CONFIG_FILE at %s: %s' % (CONFIG_FILE, str(configuration)))


  if configuration.get('memory', 0):
    tracemalloc.start(25)  # keep at least 25 frames

  startup_time = time.time()
  json_desc_dict = {}

  init_timing.append((time.time(), 3))
  flights = UnpickleObjectFromFile(
      PICKLE_FLIGHTS, True, max_days=MAX_INSIGHT_HORIZON_DAYS, heartbeat=True)
  # Clear the loaded flight of any cached data, identified by keys
  # with a specific suffix, since code fixes may change the values for
  # some of those cached elements
  init_timing.append((time.time(), 4))
  for flight in flights:
    for key in list(flight.keys()):
      if key.endswith(CACHED_ELEMENT_PREFIX):
        flight.pop(key)
  init_timing.append((time.time(), 5))

  screen_history = UnpickleObjectFromFile(PICKLE_SCREENS, True, max_days=2)

  # If we're displaying just a single insight message, we want it to be




                            <----SKIPPED LINES---->




  # time is long, we don't wait another x seconds after processing completes
  next_loop_time = time.time() + LOOP_DELAY_SECONDS

  # These files are read only if the version on disk has been modified more
  # recently than the last time it was read
  last_dump_json_timestamp = 0

  init_timing.append((time.time(), 7))
  WaitUntilKillComplete(already_running_ids)
  init_timing.append((time.time(), 8))

  personal_message = None  # Unknown what personal message is displayed

  temp_last_logged = 0  # Keeps track of when temperature was last logged

  LogTimes(init_timing)
  reboot = False

  iteration = 0  # counter that tracks how many times thru while loop
  start_time = time.time()
  DumpMemorySnapsnot(configuration, iteration, start_time)

  Log('Finishing initialization of %d; starting radio polling loop' %
      os.getpid())
  while ((not SIMULATION or SIMULATION_COUNTER < len(DUMP_JSONS))
         and not SHUTDOWN_SIGNAL):

    last_heartbeat_time = Heartbeat(last_heartbeat_time)

    new_configuration = ReadAndParseSettings(CONFIG_FILE)
    UpdateRollingLogSize(new_configuration)
    CheckForNewFilterCriteria(
        configuration, new_configuration, message_queue, flights)
    configuration = new_configuration

    ResetLogs(configuration)  # clear the logs if requested
    UpdateRollingLogSize(configuration)

    # if this is a SIMULATION, then process every diff dump. But if it
    # isn't a simulation, then only read & do related processing for the
    # next dump if the last-modified timestamp indicates the file has been




                            <----SKIPPED LINES---->




    # check time & if appropriate, display next message from queue
    next_message_time = ManageMessageQueue(
        message_queue, next_message_time, configuration, screen_history)

    reboot = CheckRebootNeeded(
        startup_time, message_queue, json_desc_dict, configuration)

    temp_last_logged = CheckTemperature(configuration, temp_last_logged)

    if not SIMULATION:
      time.sleep(max(0, next_loop_time - time.time()))
      next_loop_time = time.time() + LOOP_DELAY_SECONDS
    else:
      SIMULATION_COUNTER += 1
      if simulation_slowdown:
        SimulationSlowdownNearFlight(flights, persistent_nearby_aircraft)

    # now that we've completed the loop, lets potentially dump the
    # memory snapshot
    iteration += 1  # this completes the iteration-th time thru the loop
    DumpMemorySnapsnot(configuration, iteration, start_time)


  if SIMULATION:
    SimulationEnd(message_queue, flights, screen_history)
  PerformGracefulShutdown(shutdown, reboot)


if __name__ == "__main__":
  #interrupt, as in ctrl-c
  signal.signal(signal.SIGINT, InterruptShutdownFromSignal)

  #terminate, when another instance found or via kill
  signal.signal(signal.SIGTERM, InterruptShutdownFromSignal)

  if '-i' in sys.argv:
    BootstrapInsightList()
  else:
    main_settings = ReadAndParseSettings(CONFIG_FILE)
    if 'code_profiling_enabled' in main_settings:
      import cProfile
      cProfile.run(




                            <----SKIPPED LINES---->





01234567890123456789012345678901234567890123456789012345678901234567890123456789









6905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980








6986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028








70717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111








721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258











                            <----SKIPPED LINES---->






def MakeVersionCopy(python_prefix):
  """Copies current instance of python file into repository."""
  file_extension = '.py'

  live_name = python_prefix + '.py'
  live_path = os.path.join(CODE_REPOSITORY, live_name)

  epoch = os.path.getmtime(live_path)
  last_modified_suffix = EpochDisplayTime(
      epoch, format_string='-%Y-%m-%d-%H%M')
  version_name = python_prefix + last_modified_suffix + file_extension
  version_path = os.path.join(VERSION_REPOSITORY, version_name)

  if not os.path.exists(version_path):
    shutil.copyfile(live_path, version_path)
  return version_name


def DumpMemorySnapsnot(
    configuration, iteration, main_start_time, initial_frame_count):
  """Dump the memory snapshot to disk for later analysis.

  This dumps the memory snapshot to disk with a file name defined by the
  timestamp and iteration, in the directory MEMORY_DIRECTORY, for later
  analysis by tracemalloc load class; this can be used to do a memory
  leak detection by looking at a single snapshot, or potentially by doing
  a compare_to to look at the deltas from an earlier snapshot.

  If the 'memory' configuration is greater than 0, then snapshotting is
  enabled and we dump the configuration every time iteration is evenly
  divisible by 1000 times the 'memory' setting (because the setting
  as described on the settings.php page is in thousands of iterations).

  Args:
    configuration: dictionary of configuration attributes.
    iteration: counter that indicates how many times through the main
      loop we are.
    main_start_time: time stamp of when the program first started, to
      be recorded in file names, so that associated memory dumps
      can be more easily grouped, analyzed, and purged together.
    initial_frame_count: frame count used to initialize the tracemalloc;
      note that anything greater than 1 has implications for how
      comparisons can be done.
  """
  iteration_divisor = configuration.get('memory', 0)
  if not iteration_divisor:
    return
  if not iteration % (iteration_divisor * 1000):
    main_time_stamp = EpochDisplayTime(
        main_start_time, format_string='%m%d%H%M%S')
    this_time_stamp = EpochDisplayTime(
        time.time(), format_string='%Y%m%d%H%M')
    file_name = '%s-%s-%d-%d.dump' % (
        main_time_stamp, this_time_stamp, iteration, initial_frame_count)
    full_path_name = os.path.join(MEMORY_DIRECTORY, file_name)
    snapshot = tracemalloc.take_snapshot()
    snapshot.dump(full_path_name)


def main():
  """Traffic cop between radio, configuration, and messageboard.

  This is the main logic, checking for new flights, augmenting the radio
  signal with additional web-scraped data, and generating messages in a
  form presentable to the messageboard.
  """
  VersionControl()

  # Since this clears log files, it should occur first before we start logging
  if '-s' in sys.argv:
    global SIMULATION_COUNTER
    SimulationSetup()

  last_heartbeat_time = HeartbeatRestart()




                            <----SKIPPED LINES---->





  # Redirect any errors to a log file instead of the screen, and add a datestamp
  if not SIMULATION:
    sys.stderr = open(STDERR_FILE, 'a')
    Log('', STDERR_FILE)

  init_timing.append((time.time(), 1))
  Log('Starting up process %d' % os.getpid())
  already_running_ids = FindRunningParents()
  if already_running_ids:
    for pid in already_running_ids:
      Log('Sending termination signal to %d' % pid)
      os.kill(pid, signal.SIGTERM)
  init_timing.append((time.time(), 2))

  SetPinMode()

  configuration = ReadAndParseSettings(CONFIG_FILE)
  Log('Read CONFIG_FILE at %s: %s' % (CONFIG_FILE, str(configuration)))

  initial_frame_count = configuration.get('memory_frames', 1)
  if configuration.get('memory', 0):
    tracemalloc.start(initial_frame_count)

  startup_time = time.time()
  json_desc_dict = {}

  init_timing.append((time.time(), 3))
  flights = UnpickleObjectFromFile(
      PICKLE_FLIGHTS, True, max_days=MAX_INSIGHT_HORIZON_DAYS, heartbeat=True)
  # Clear the loaded flight of any cached data, identified by keys
  # with a specific suffix, since code fixes may change the values for
  # some of those cached elements
  init_timing.append((time.time(), 4))
  for flight in flights:
    for key in list(flight.keys()):
      if key.endswith(CACHED_ELEMENT_PREFIX):
        flight.pop(key)
  init_timing.append((time.time(), 5))

  screen_history = UnpickleObjectFromFile(PICKLE_SCREENS, True, max_days=2)

  # If we're displaying just a single insight message, we want it to be




                            <----SKIPPED LINES---->




  # time is long, we don't wait another x seconds after processing completes
  next_loop_time = time.time() + LOOP_DELAY_SECONDS

  # These files are read only if the version on disk has been modified more
  # recently than the last time it was read
  last_dump_json_timestamp = 0

  init_timing.append((time.time(), 7))
  WaitUntilKillComplete(already_running_ids)
  init_timing.append((time.time(), 8))

  personal_message = None  # Unknown what personal message is displayed

  temp_last_logged = 0  # Keeps track of when temperature was last logged

  LogTimes(init_timing)
  reboot = False

  iteration = 0  # counter that tracks how many times thru while loop
  start_time = time.time()
  DumpMemorySnapsnot(configuration, iteration, start_time, initial_frame_count)

  Log('Finishing initialization of %d; starting radio polling loop' %
      os.getpid())
  while ((not SIMULATION or SIMULATION_COUNTER < len(DUMP_JSONS))
         and not SHUTDOWN_SIGNAL):

    last_heartbeat_time = Heartbeat(last_heartbeat_time)

    new_configuration = ReadAndParseSettings(CONFIG_FILE)
    UpdateRollingLogSize(new_configuration)
    CheckForNewFilterCriteria(
        configuration, new_configuration, message_queue, flights)
    configuration = new_configuration

    ResetLogs(configuration)  # clear the logs if requested
    UpdateRollingLogSize(configuration)

    # if this is a SIMULATION, then process every diff dump. But if it
    # isn't a simulation, then only read & do related processing for the
    # next dump if the last-modified timestamp indicates the file has been




                            <----SKIPPED LINES---->




    # check time & if appropriate, display next message from queue
    next_message_time = ManageMessageQueue(
        message_queue, next_message_time, configuration, screen_history)

    reboot = CheckRebootNeeded(
        startup_time, message_queue, json_desc_dict, configuration)

    temp_last_logged = CheckTemperature(configuration, temp_last_logged)

    if not SIMULATION:
      time.sleep(max(0, next_loop_time - time.time()))
      next_loop_time = time.time() + LOOP_DELAY_SECONDS
    else:
      SIMULATION_COUNTER += 1
      if simulation_slowdown:
        SimulationSlowdownNearFlight(flights, persistent_nearby_aircraft)

    # now that we've completed the loop, lets potentially dump the
    # memory snapshot
    iteration += 1  # this completes the iteration-th time thru the loop
    DumpMemorySnapsnot(
        configuration, iteration, start_time, initial_frame_count)

  if SIMULATION:
    SimulationEnd(message_queue, flights, screen_history)
  PerformGracefulShutdown(shutdown, reboot)


if __name__ == "__main__":
  #interrupt, as in ctrl-c
  signal.signal(signal.SIGINT, InterruptShutdownFromSignal)

  #terminate, when another instance found or via kill
  signal.signal(signal.SIGTERM, InterruptShutdownFromSignal)

  if '-i' in sys.argv:
    BootstrapInsightList()
  else:
    main_settings = ReadAndParseSettings(CONFIG_FILE)
    if 'code_profiling_enabled' in main_settings:
      import cProfile
      cProfile.run(




                            <----SKIPPED LINES---->