Logo Search packages:      
Sourcecode: langupdate version File versions  Download package

gint main ( gint  argc,
gchar *  argv[] 
)

Read the apt-cache to know what is available. then compare dpkg_list with apt_list.

Todo:
put the variables into a context struct for easier initialisation.

Bug:
assuming here that no src means not Debian which works for -cross packages but is still not reliable. End up with this message appearing for TDebs inside Grip.

Definition at line 719 of file langupdate.cc.

References append_sourceslist(), apt_init(), c, debug, lu_check_pkg_name(), lu_check_supported_suites(), lu_clear_caches(), lu_get_sourcepkg(), lu_parse_installed(), lu_strip_components(), not_ours, and output().

{
      GError * em_gerr;
      gchar * dpkg, *code, *lgen, * log, * root, * name, *src, *version, *pkg,
       *gen, *srclist, *dpkgfile;
      const gchar * const * locale_v;
      GList * dpkg_list, * p, *locales;
      GHashTable * locale_table, * dpkg_table;
      gchar ** status_list, ** gen_list;
      guint mcount, l;
      gboolean pkg_changed;
      GOptionContext *context;

      /** \todo put the variables into a context struct for easier
       initialisation. */
      code = root = src = version = pkg = NULL;
      em_gerr = NULL;
      locales = dpkg_list = NULL;
      orphans = g_hash_table_new (g_str_hash, g_str_equal);
      locale_table = g_hash_table_new (g_str_hash, g_str_equal);
      dpkg_table = g_hash_table_new (g_str_hash, g_str_equal);
      choices = g_hash_table_new (g_str_hash, g_str_equal);
      l = mcount = 0;
#ifdef ENABLE_NLS
      setlocale (LC_ALL, "");
      bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
      bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
      textdomain (GETTEXT_PACKAGE);
#endif
      context = g_option_context_new (_("- updates language support for Emdebian"));
      g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
      g_option_context_set_summary (context,
            _("Apt frontend to match the Emdebian TDeb support with the installed\n"
            "packages and supported locales."));
      g_option_context_set_description (context, 
            /* Translators: the line endings need to be in roughly the same place in
            your own language, counting by characters and allowing for all breaks to
            be between words. */
            _("Emdebian TDebs provide a method for splitting all translation files out of\n"
            "packages and into separate TDeb packages, organised by the locale root and\n"
            "source package. This allows individual users to only install translations\n"
            "files for the locales supported on their own machine and only for the\n"
            "packages installed at the time.\n\n"
            "Note that this functionality is not available for Debian TDebs.\n"));
      g_option_context_parse (context, &argc, &argv, &em_gerr);
      if (show_version)
      {
            g_print ("%s (%s)\n", PACKAGE, VERSION);
            g_print ("%s", GNU_WARRANTY);
            return (0);
      }
      if (!user_suite)
            suite_codename = g_strdup("unstable");
      else
            lu_check_supported_suites(user_suite);
      if (!prefix)
            prefix = g_strdup("");
      else
      {
            gchar * ours;
            ours = g_build_filename (prefix, G_DIR_SEPARATOR_S, "var/lib/"PACKAGE"/", NULL);
            if (!g_file_test (ours, G_FILE_TEST_IS_DIR))
            {
                  gint i;
                  i = g_mkdir_with_parents (ours, 0755);
                  if (i != 0)
                  {
                        /* Translators: First string is the package name,
                         second and third are a user-specified directory. */
                        g_error (_("Unable to create %s data using prefix '%s'.\n"
                              "Do you have permission to write to '%s'?"),
                              PACKAGE, prefix, prefix);
                        return -1;
                  }
            }
      }
      if (dry_run and not purge_mode)
            g_print ("%s: %s\n", PACKAGE,
                  _("Dry run, just showing cache data for installable packages."));
      if (!start_sources_list())
            return (1);
      // parse /etc/locale.gen here
      gen = get_locale_gen (prefix);
      if (!g_file_get_contents (gen, &lgen, NULL, &em_gerr))
      {
            g_critical (em_gerr->message);
            g_message ("%s: %s '%s'", PACKAGE,
                  _("Unable to open the locale.gen configuration file."), gen);
            g_clear_error (&em_gerr);
            return -1;
      }
      gen_list = g_strsplit (lgen, "\n", -1);
      for (mcount = 0; mcount <= g_strv_length(gen_list); mcount++)
      {
            gchar * line;

            if (!gen_list[mcount])
                  continue;
            if (g_str_has_prefix (gen_list[mcount], "#"))
                  continue;
            if (!g_strcmp0 ("", gen_list[mcount]))
                  continue;
            line = g_strdup (gen_list[mcount]);
            if (!lu_strip_components (line, &code, &root))
                  continue;
            if (!code || !root)
                  continue;
            g_free (line);
            if (!g_hash_table_lookup(locale_table, root))
            {
                  if (verbose >= 1)
                        g_print ("%s: %s%s %s\n", PACKAGE, prefix,
                              _("/etc/locale.gen requires adding support for:"), code);
                  if (!append_sourceslist (root)) 
                        return -1;
                  g_hash_table_insert (locale_table, root, code);
                  if (verbose >= 1)
                        g_print ("%s: %s %s\n", PACKAGE, 
                              _("Adding support for:"), root);
            }
      }
      g_strfreev (gen_list);
      locale_v = g_get_language_names ();
      l = 0;
      while (g_strcmp0 (locale_v[l], "C") != 0)
      {
            gchar * line;

            line = g_strdup (locale_v[l]);
            code = root = NULL;
            if (!lu_strip_components (line, &code, &root))
                  continue;
            if (!code || !root)
                  continue;
            g_free (line);
            if (!g_hash_table_lookup(locale_table, root))
            {
                  if (verbose >= 1)
                        g_print ("%s: %s %s\n", PACKAGE, 
                              _("Environment needs support for:"), code);
                  if (!append_sourceslist (root)) 
                        return (-1);
                  if (verbose >= 1)
                        g_print ("%s: %s %s\n", PACKAGE, 
                              _("Adding support for:"), root);
                  g_hash_table_insert (locale_table, root, code);
            }
            else
            {
                  // don't free entries in a GHashTable
//                g_free (code);
                  g_free (root);
                  code = NULL;
                  root = NULL;
            }
            l++;
      }
      g_list_free (locales);
      locales = g_hash_table_get_values (locale_table);
      locales = g_list_concat (locales, g_hash_table_get_keys (locale_table));

      /* end of the locale identification code */

      srclist = get_sources_list (prefix);
      g_return_val_if_fail (apt_init (srclist, suite_codename, prefix, verbose), -1);
      dpkgfile = get_dpkg_file (prefix);
      if (!g_file_get_contents (dpkgfile, &dpkg, NULL, &em_gerr))
      {
            g_critical (em_gerr->message);
            g_message ("%s: %s '%s'", PACKAGE,
                  _("Unable to open the dpkg status file."), dpkgfile);
            g_clear_error (&em_gerr);
            return -1;
      }
      status_list = g_strsplit (dpkg, "\n", -1);
      pkg_changed = FALSE;
      name = NULL;
      version = NULL;
      for (mcount = 0; mcount <= g_strv_length(status_list); mcount++)
      {
            if (!status_list[mcount])
                  continue;
            /* detect the empty line between package stanzas */
            if (!g_strcmp0 (status_list[mcount], ""))
                  pkg_changed = TRUE;
            else
                  pkg_changed = FALSE;
            /* get the status of each package in the dpkg list */

            if (g_str_has_prefix (status_list[mcount], PACKAGE_CHK))
                  name = lu_parse_control (status_list[mcount]);

            if (status_list[mcount+1] and purge_mode and 
                  g_str_has_prefix (status_list[mcount+1], STATUS_CHK))
            {
                  if (g_str_has_suffix (status_list[mcount+1], "not-installed"))
                  {
                        purged = g_list_prepend (purged, name);
                        continue;
                  }
            }
            if (g_str_has_prefix (status_list[mcount], "Version: "))
            {
                  gchar * em_chunk;
                  version = lu_parse_control (status_list[mcount]);
                  if (pkg)
                        g_hash_table_replace (dpkg_table, pkg, version);
                  /* -1 to allow for em1, em2, em3 etc. */
                  em_chunk = g_strndup (version, strlen (version) -1);
                  if ((!g_str_has_suffix (em_chunk, "em")) and (src))
                  {
                        gchar * pkg_prefix;
                        // need to add to a list that can be subtracted later.
                        // but existing TDebs also show up here due to the lack
                        // of an em1 version suffix.
                        /** \bug assuming here that no src means not Debian
                        which works for -cross packages but is still not reliable.
                        End up with this message appearing for TDebs inside Grip.
                        */
                        pkg_prefix = g_strconcat (src, "-", LOCALE_SUFFIX, "-", NULL);
                        if ((!g_str_has_prefix(name, pkg_prefix)) and
                              (verbose >= 2))
                        {
                              g_print ("%s: %s: %s (%s)\n", PACKAGE,
                                    _("Not an Emdebian package"), src, version);
                        }
                        if (!purge_mode)
                              installed = g_list_append (installed, src);
                        not_ours = g_list_prepend (not_ours, g_strdup(src));
                        g_free (em_chunk);
                        g_free (pkg_prefix);
                        continue;
                  }
            }
            if ((!pkg_changed) and 
                  (g_str_has_prefix (status_list[mcount], PACKAGE_CHK)))
            {
                  gchar * orphaned, * l;
                  orphaned = l = NULL;
                  l = g_strconcat ("-", LOCALE_SUFFIX, "-", NULL);
                  orphaned = g_strrstr_len (name, strlen(name), l);
                  if (orphaned)
                        g_hash_table_insert (orphans, g_strdup(name), g_strdup(src));
                  orphaned = NULL;
                  g_free (l);
                  src = lu_get_sourcepkg (name);
                  for (p = locales; p != NULL; p = p->next)
                  {
                        gchar * code;
                        code = g_strdup ((gchar*)p->data);
                        if (!code)
                              continue;
                        code = g_strstrip (code);
                        if (src)
                              pkg = lu_parse_installed (name, src, code);
                        if (pkg)
                        {
                              gchar * replace;
                              replace = (gchar*)g_hash_table_lookup(dpkg_table, pkg);
                              if (replace or !g_strcmp0 ("",replace))
                              {
                                    g_hash_table_replace (dpkg_table, pkg, g_strdup(""));
                              }
                              else
                              {
                                    g_hash_table_insert (dpkg_table, pkg, g_strdup(""));
                              }
                              dpkg_list = g_list_prepend (dpkg_list, pkg);
                        }
                        g_free (code);
                  }
            }
      }
      g_strfreev (status_list);

      /* finished with the status list */

      if (verbose >= 3)
      {
            GList * dpkg_keys = NULL;
            GList * p = NULL;
            g_print ("%s choices: %d\n", "choices complete", g_hash_table_size(choices));
            g_print ("dpkg_table has %d entries.\n", g_hash_table_size(dpkg_table));
            dpkg_keys = g_hash_table_get_keys (dpkg_table);
            if (verbose >= 4)
            {
                  for (p=g_list_sort(dpkg_keys, lu_check_pkg_name); p!= NULL; p = p->next)
                  {
                        g_print ("dkg_table contains: %s\n", (gchar*)p->data);
                  }
            }
      }

      g_hash_table_foreach (dpkg_table, lu_parse_dpkg, NULL);

      for (p = not_ours; p != NULL; p = p->next)
      {
            g_hash_table_foreach_remove (choices, lu_skip_not_ours, p->data);
      }
      if (verbose >= 3)
      {
            g_print ("dpkg_table has %d entries.\n", g_hash_table_size(dpkg_table));
            g_print ("installed has %d entries.\n", g_list_length(installed));
            g_print ("not_ours has %d entries.\n", g_list_length(not_ours));
            g_print ("targets has %d entries.\n", g_list_length(targets));
            g_print ("choices has %d entries.\n", g_hash_table_size(choices));
      }
      c = g_hash_table_size (choices);
      if (!purge_mode)
      {
            GList * sorted = NULL;

            if (c == 0)
                  g_print ("%s: %s\n", PACKAGE, _("Nothing to do."));
            else
            {
                  g_print ("%s: ", PACKAGE);
                  g_print (ngettext("%d package to be installed\n",
                        "%d packages to be installed\n", c), c);
                  sorted = g_list_sort (g_hash_table_get_keys (choices), lu_check_pkg_name);
                  g_list_foreach (sorted, output, NULL);
            }
      }
      g_hash_table_destroy (locale_table);
      g_hash_table_destroy (dpkg_table);
      g_hash_table_destroy (choices);
      remove_cache ("lists/lock", suite_codename);
      remove_cache ("lock", suite_codename);
      if (!debug)
      {
            g_print ("%s: %s: '%s'.\n", PACKAGE,
                  _("Cleaning up apt lists"), suite_codename);
            run_apt_clean (suite_codename);
            remove_cache ("srcpkgcache.bin", suite_codename);
            remove_cache ("pkgcache.bin", suite_codename);
            remove_lists (suite_codename);
            g_unlink (srclist);
      }
      log = g_build_filename (prefix, G_DIR_SEPARATOR_S, "var","lib",
                        PACKAGE,"/var/log/apt/term.log", NULL);
      g_unlink (log);
      g_free (log);
      g_list_free (locales);
      if (purge_mode)
      {
            gchar * command;

            /* to convert to a "remove all TDebs" option, whether the
             package is installed or not, switch these
             from g_hash_table_remove to g_hash_table_insert */
            for (p = targets; p != NULL; p = p->next)
            {
                  g_hash_table_remove (orphans, (gchar*)p->data);
            }
            for (p = installed; p != NULL; p = p->next)
            {
                  g_hash_table_remove (orphans, (gchar*)p->data);
            }
            purge_names = g_strdup("");
            g_hash_table_foreach (orphans, lu_purge_orphans, NULL);
            if (g_strcmp0(purge_names, ""))
            {
                  command = g_strconcat ("apt-get -y --purge remove ", purge_names, NULL);
                  if (dry_run)
                        g_print ("%s: %s '%s'\n", PACKAGE, _("Dry-run only."), command);
                  else
                        g_spawn_command_line_sync (command, NULL, NULL, NULL, &em_gerr);
                  if (em_gerr)
                  {
                        g_critical (em_gerr->message);
                        g_message (_("Unable to execute command: %s."), command);
                        g_clear_error (&em_gerr);
                  }
                  g_free (command);
            }
            else
                  g_print ("%s: %s\n", PACKAGE, _("No TDeb packages to remove."));
      }
      g_print ("\n");
      g_free (suite_codename);
      g_free (srclist);
      if (!debug)
            lu_clear_caches ();
      return 0;
}


Generated by  Doxygen 1.6.0   Back to index