def uninstall_Windows():
    """
  <Purpose>
    Removes seattle from the Winodws registry startup key and/or the
    startup folder should either exist, then stops all seattle processes using
    stop_all_seattle_process.py
  <Arguments>
    None.
  <Exceptions>
    Possible IOError could be caused by filepath manipulation from a
      sub-function.
    SeattleNotInstalledError if seattle was not installed prior to uninstall.
  <Side Effects>
    Removes seattle from the Windows registry key and/or the Windows startup
    folder if it exists in either place.
    Stops seattle from running.
  <Returns>
    True if the uninstall succeeded.  Currently, if uninstall fails, it must be
    because seattle was not installed prior to uninstall.  We must return a
    boolean value for the parent function.
  """
    # First see if seattle appears as a value in the Windows startup registry key,
    # and remove it if it exists.
    # removed_from_registry is used later and thus must have a value in case the
    # try: block below raises an exception.
    removed_from_registry = False
    try:
        removed_from_registry = remove_seattle_from_win_startup_registry()
    except WindowsError:
        print "The uninstaller does not have access to the Windows registry " \
            + "startup keys. This means that seattle is likely not installed in " \
            + "your Windows registry startup key, though you may want to " \
            + "manually check the following registry keys and remove seattle " \
            + "from those keys should it exist there: "
        print "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
        print "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
        # Distinguish the above-printed text from what will be printed later by
        # by printing a blank line.
        print
        servicelogger.log(" uninstaller could not access the Windows registry " \
                            + "during this attempted uninstall.")

    # Next, see if there is a link to the seattle starter script in the startup
    # folder and remove it if it is there.
    if not WIN_STARTUP_SCRIPT_PATH == None:
        removed_from_startup_folder = \
            remove_seattle_from_win_startup_folder()

    # Check to see if uninstall actually removed seattle from the computer.
    if not removed_from_registry and not removed_from_startup_folder:
        raise SeattleNotInstalledError("Seattle could not be detected as " \
                                         + "having been installed prior to " \
                                         + "uninstall.")
    elif removed_from_registry or removed_from_startup_folder:
        # Stop all instances of seattle from running before returning.
        stop_all_seattle_processes.main()
        return True
def _stop_seattle_processes():
  """
  <Purpose>
    Kills all the seattle programs that are running.

  <Arguments>
    None

  <Side Effects>
    Kills all the seattle programs that are running.
    If this fails, this process will be terminated.

  <Exceptions>
    None.

  <Returns>
    None.
  """
  # Wrap the lock_acquired value so that it can referred to by reference
  context = {'lock_acquired': False}

  # 10 seconds should be enough time to get the lock on normal operation.
  # Otherwise, assume we have a deadlock and exit.
  wait_time = 10
  def check_failed_lock_acquire():
    for i in xrange(wait_time):
      if context['lock_acquired']:
        break
      time.sleep(1)
    else:
      print>>sys.stderr, "Uninstallation cannot be completed; Failed to acquire lock."
      harshexit.harshexit(1)

  lock_check_thread = threading.Thread(target=check_failed_lock_acquire)
  lock_check_thread.daemon = True
  lock_check_thread.start()

  # Stop all instances of seattle from running before returning.
  stop_all_seattle_processes.main()
  context['lock_acquired'] = True
def uninstall_Linux_and_Mac():
  """
  <Purpose>
    Remove the seattle entry from the crontab, and kill all seattle processes
    by using stop_all_seattle_processes.py
  <Arguments>
    None.
  <Exceptions>
    SeattleNotInstalledError if seattle had not been initially installed when
      the uninstaller was run.
    UnsupportedOSError from a child function if the OS running this script is
      not supported.
  <Side Effects>
    Removes the seattle entry from the crontab and stops seattle from running.
  <Returns>
    True if succeeded in uninstalling,
    False otherwise.
  """


  # Derek Cheng: Find out if this is a Nokia N800/900 Tablet, and if so runs a 
  # separate uninstaller because there is no crontab on the tablets.
  if platform.machine().startswith('armv'):
    return uninstall_nokia()

  if platform.machine().startswith('mips'):
    return True


  # Find out if Seattle is installed (currently in the crontab), and remove if
  # so.
  crontab_contents_stdout = subprocess.Popen(["crontab","-l"],
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.PIPE).stdout

  # Generate a temp file with the user's crontab minus our task.
  temp_crontab_file = tempfile.NamedTemporaryFile()

  seattle_crontab_entry_found = False
  for line in crontab_contents_stdout:
    if not seattleinstaller.get_starter_file_name() in line:
      temp_crontab_file.write(line)
    else:
      seattle_crontab_entry_found = True


  if not seattle_crontab_entry_found:
    temp_crontab_file.close()
    raise SeattleNotInstalledError("Seattle cannot be uninstalled because it " \
                                     + "is not currently installed.")



  # Replace the old crontab with the updated crontab contents.
  temp_crontab_file.flush()
  replace_crontab = subprocess.Popen(["crontab",temp_crontab_file.name],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
  replace_crontab.wait()
  temp_crontab_file.close()



  # Confirm that seattle was successfully removed from the crontab, and set
  # the 'crontab_updated_for_2009_installer' value in nodeman.cfg to False.
  modified_crontab_contents_stdout,modified_crontab_contents_stderr = \
      subprocess.Popen(["crontab","-l"],
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE).communicate()

  if seattleinstaller.get_starter_file_name() \
        in modified_crontab_contents_stdout:
    return False
  else:
    # Stop all instances of seattle from running before returning.
    stop_all_seattle_processes.main()

    configuration = persist.restore_object("nodeman.cfg")
    configuration['crontab_updated_for_2009_installer'] = False
    persist.commit_object(configuration,"nodeman.cfg")

    return True
def uninstall_Windows():
  """
  <Purpose>
    Removes seattle from the Winodws registry startup key and/or the
    startup folder should either exist, then stops all seattle processes using
    stop_all_seattle_process.py
  <Arguments>
    None.
  <Exceptions>
    Possible IOError could be caused by filepath manipulation from a
      sub-function.
    SeattleNotInstalledError if seattle was not installed prior to uninstall.
  <Side Effects>
    Removes seattle from the Windows registry key and/or the Windows startup
    folder if it exists in either place.
    Stops seattle from running.
  <Returns>
    True if the uninstall succeeded.  Currently, if uninstall fails, it must be
    because seattle was not installed prior to uninstall.  We must return a
    boolean value for the parent function.
  """
  # First see if seattle appears as a value in the Windows startup registry key,
  # and remove it if it exists.
  # removed_from_registry is used later and thus must have a value in case the
  # try: block below raises an exception.
  removed_from_registry = False
  try:
    removed_from_registry = remove_seattle_from_win_startup_registry()
  except WindowsError:
    print "The uninstaller does not have access to the Windows registry " \
        + "startup keys. This means that seattle is likely not installed in " \
        + "your Windows registry startup key, though you may want to " \
        + "manually check the following registry keys and remove seattle " \
        + "from those keys should it exist there: "
    print "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
    print "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
    # Distinguish the above-printed text from what will be printed later by
    # by printing a blank line.
    print
    servicelogger.log(" uninstaller could not access the Windows registry " \
                        + "during this attempted uninstall.")



  # Next, see if there is a link to the seattle starter script in the startup
  # folder and remove it if it is there.
  if not WIN_STARTUP_SCRIPT_PATH == None:
    removed_from_startup_folder = \
        remove_seattle_from_win_startup_folder()



  # Check to see if uninstall actually removed seattle from the computer.
  if not removed_from_registry and not removed_from_startup_folder:
    raise SeattleNotInstalledError("Seattle could not be detected as " \
                                     + "having been installed prior to " \
                                     + "uninstall.")
  elif removed_from_registry or removed_from_startup_folder:
    # Stop all instances of seattle from running before returning.
    stop_all_seattle_processes.main()
    return True
    os.remove(symlink_path)
  # Cannot remove the symlink due to some reason.
  except OSError, e:
    # The symlink does not exist - that is fine.
    if e.errno == errno.ENOENT:
      pass
    else:
      # The symlink cannot be removed.
      _output("The symlink cannot be removed. Make sure you have the " \
                + "permission to do so.")
      servicelogger.log("Seattle cannot be uninstalled because " \
                          + symlink_path + " cannot be removed.")
      return False

  # Stop all instances of seattle from running.
  stop_all_seattle_processes.main()

  return True


def uninstall_Linux_and_Mac():
  """
  <Purpose>
    Remove the seattle entry from the crontab, and kill all seattle processes
    by using stop_all_seattle_processes.py
  <Arguments>
    None.
  <Exceptions>
    SeattleNotInstalledError if seattle had not been initially installed when
      the uninstaller was run.
    UnsupportedOSError from a child function if the OS running this script is
def uninstall_Linux_and_Mac():
    """
  <Purpose>
    Remove the seattle entry from the crontab, and kill all seattle processes
    by using stop_all_seattle_processes.py
  <Arguments>
    None.
  <Exceptions>
    SeattleNotInstalledError if seattle had not been initially installed when
      the uninstaller was run.
    UnsupportedOSError from a child function if the OS running this script is
      not supported.
  <Side Effects>
    Removes the seattle entry from the crontab and stops seattle from running.
  <Returns>
    True if succeeded in uninstalling,
    False otherwise.
  """

    # Derek Cheng: Find out if this is a Nokia N800/900 Tablet, and if so runs a
    # separate uninstaller because there is no crontab on the tablets.
    if platform.machine().startswith('armv'):
        return uninstall_nokia()

    if platform.machine().startswith('mips'):
        return True

    # Find out if Seattle is installed (currently in the crontab), and remove if
    # so.
    crontab_contents_stdout = subprocess.Popen(["crontab", "-l"],
                                               stdout=subprocess.PIPE,
                                               stderr=subprocess.PIPE).stdout

    # Generate a temp file with the user's crontab minus our task.
    temp_crontab_file = tempfile.NamedTemporaryFile()

    seattle_crontab_entry_found = False
    for line in crontab_contents_stdout:
        if not seattleinstaller.get_starter_file_name() in line:
            temp_crontab_file.write(line)
        else:
            seattle_crontab_entry_found = True

    if not seattle_crontab_entry_found:
        temp_crontab_file.close()
        raise SeattleNotInstalledError("Seattle cannot be uninstalled because it " \
                                         + "is not currently installed.")

    # Replace the old crontab with the updated crontab contents.
    temp_crontab_file.flush()
    replace_crontab = subprocess.Popen(["crontab", temp_crontab_file.name],
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
    replace_crontab.wait()
    temp_crontab_file.close()

    # Confirm that seattle was successfully removed from the crontab, and set
    # the 'crontab_updated_for_2009_installer' value in nodeman.cfg to False.
    modified_crontab_contents_stdout,modified_crontab_contents_stderr = \
        subprocess.Popen(["crontab","-l"],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE).communicate()

    if seattleinstaller.get_starter_file_name() \
          in modified_crontab_contents_stdout:
        return False
    else:
        # Stop all instances of seattle from running before returning.
        stop_all_seattle_processes.main()

        configuration = persist.restore_object("nodeman.cfg")
        configuration['crontab_updated_for_2009_installer'] = False
        persist.commit_object(configuration, "nodeman.cfg")

        return True
        os.remove(symlink_path)
    # Cannot remove the symlink due to some reason.
    except OSError, e:
        # The symlink does not exist - that is fine.
        if e.errno == errno.ENOENT:
            pass
        else:
            # The symlink cannot be removed.
            _output("The symlink cannot be removed. Make sure you have the " \
                      + "permission to do so.")
            servicelogger.log("Seattle cannot be uninstalled because " \
                                + symlink_path + " cannot be removed.")
            return False

    # Stop all instances of seattle from running.
    stop_all_seattle_processes.main()

    return True


def uninstall_Linux_and_Mac():
    """
  <Purpose>
    Remove the seattle entry from the crontab, and kill all seattle processes
    by using stop_all_seattle_processes.py
  <Arguments>
    None.
  <Exceptions>
    SeattleNotInstalledError if seattle had not been initially installed when
      the uninstaller was run.
    UnsupportedOSError from a child function if the OS running this script is