From 3ac199f10736585e7196132474abe7e2cebc2d79 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Mon, 19 Jun 2023 20:33:19 +0200 Subject: [PATCH 1/2] cli: Fix copy/paste error in translators comment Fixes: 3f88c4e769bb ("Add new is-satisfied check to test relations from the command-line") --- tools/appstreamcli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/appstreamcli.c b/tools/appstreamcli.c index e2509c76a..0ad50bdd4 100644 --- a/tools/appstreamcli.c +++ b/tools/appstreamcli.c @@ -1340,7 +1340,7 @@ as_client_run (char **argv, int argc) as_client_run_check_license); ascli_add_cmd (commands, 2, "is-satisfied", NULL, "FILE|COMPONENT-ID", - /* TRANSLATORS: `appstreamcli `check-license` command description. */ + /* TRANSLATORS: `appstreamcli `is-satisfied` command description. */ _("Check if requirements of a component (via its ID or MetaInfo file) are satisfied on this system."), as_client_run_is_satisfied); From 4e8120af5940f35429d04f617bac1a6d0b1536c0 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Mon, 19 Jun 2023 21:02:21 +0200 Subject: [PATCH 2/2] cli: Add "get-latest-version" util command This gets the latest version specified in an appdata file, which, combined with utilities such as check-news.sh[1], can be used to check whether the date specified in the appdata's release information is correct. See https://gitlab.gnome.org/GNOME/evince/-/issues/1950 Closes: #502 [1]: https://gitlab.gnome.org/GNOME/totem/-/blob/master/check-news.sh --- tools/appstreamcli.c | 25 ++++++++++++++ tools/ascli-actions-mdata.c | 66 +++++++++++++++++++++++++++++++++++++ tools/ascli-actions-mdata.h | 2 ++ 3 files changed, 93 insertions(+) diff --git a/tools/appstreamcli.c b/tools/appstreamcli.c index 0ad50bdd4..7f4fb0da1 100644 --- a/tools/appstreamcli.c +++ b/tools/appstreamcli.c @@ -564,6 +564,26 @@ as_client_run_is_satisfied (const gchar *command, char **argv, int argc) optn_no_cache); } +/** + * as_client_run_get_latest_version: + * + * Get the latest version specified in an AppData file. + */ +static int +as_client_run_get_latest_version (const gchar *command, char **argv, int argc) +{ + const gchar *fname = NULL; + + if (argc > 2) + fname = argv[2]; + if (argc > 3) { + as_client_print_help_hint (command, argv[3]); + return 1; + } + + return ascli_get_latest_version_file (fname); +} + /** * as_client_run_put: * @@ -1343,6 +1363,11 @@ as_client_run (char **argv, int argc) /* TRANSLATORS: `appstreamcli `is-satisfied` command description. */ _("Check if requirements of a component (via its ID or MetaInfo file) are satisfied on this system."), as_client_run_is_satisfied); + ascli_add_cmd (commands, + 2, "get-latest-version", NULL, "FILE", + /* TRANSLATORS: `appstreamcli get-latest-version command description. */ + _("Get the latest version from the AppData file"), + as_client_run_get_latest_version); ascli_add_cmd (commands, 3, "install", NULL, "COMPONENT-ID", diff --git a/tools/ascli-actions-mdata.c b/tools/ascli-actions-mdata.c index 5155212d7..9f056d2f6 100644 --- a/tools/ascli-actions-mdata.c +++ b/tools/ascli-actions-mdata.c @@ -739,3 +739,69 @@ ascli_check_is_satisfied (const gchar *fname_or_cid, const gchar *cachepath, gbo return res? ASCLI_EXIT_CODE_SUCCESS : ASCLI_EXIT_CODE_FAILED; } + +gint +ascli_get_latest_version_file (const gchar *fname) +{ + g_autoptr(AsMetadata) metad = NULL; + g_autoptr(AsComponent) cpt = NULL; + g_autoptr(GFile) infile = NULL; + g_autoptr(GError) error = NULL; + + if (fname == NULL) { + ascli_print_stderr (_("You need to specify an input file.")); + return 1; + } + + /* load input file */ + infile = g_file_new_for_path (fname); + if (!g_file_query_exists (infile, NULL)) { + ascli_print_stderr (_("Metadata file '%s' does not exist."), fname); + return 2; + } + + metad = as_metadata_new (); + as_metadata_set_locale (metad, "ALL"); + + if ((g_str_has_suffix (fname, ".yml.gz")) || + (g_str_has_suffix (fname, ".yaml.gz")) || + (g_str_has_suffix (fname, ".yml")) || + (g_str_has_suffix (fname, ".yaml"))) { + /* if we have YAML, we also automatically assume a catalog style */ + as_metadata_set_format_style (metad, AS_FORMAT_STYLE_CATALOG); + } else if (g_str_has_suffix (fname, ".metainfo.xml") || g_str_has_suffix (fname, ".appdata.xml")) { + as_metadata_set_format_style (metad, AS_FORMAT_STYLE_METAINFO); + } else { + as_metadata_set_format_style (metad, AS_FORMAT_STYLE_CATALOG); + } + + as_metadata_parse_file (metad, + infile, + AS_FORMAT_KIND_UNKNOWN, + &error); + if (error != NULL) { + g_printerr ("%s\n", error->message); + return 3; + } + + cpt = g_object_ref (as_metadata_get_component (metad)); + + if (as_component_get_releases (cpt)->len > 0) { + GPtrArray *releases = as_component_get_releases (cpt); + AsRelease *release_newest = NULL; + + for (guint i = 0; i < releases->len; i++) { + AsRelease *release_tmp = g_ptr_array_index (releases, i); + if (release_newest == NULL || + as_release_get_timestamp (release_tmp) > as_release_get_timestamp (release_newest)) + release_newest = release_tmp; + } + + g_print ("%s\n", as_release_get_version (release_newest)); + } else { + ascli_print_stderr (_("No releases information available in '%s'."), fname); + return 4; + } + + return ASCLI_EXIT_CODE_SUCCESS; +} diff --git a/tools/ascli-actions-mdata.h b/tools/ascli-actions-mdata.h index c4cde490c..9910aa9a1 100644 --- a/tools/ascli-actions-mdata.h +++ b/tools/ascli-actions-mdata.h @@ -72,6 +72,8 @@ gint ascli_check_is_satisfied (const gchar *fname_or_cid, const gchar *cachepath, gboolean no_cache); +gint ascli_get_latest_version_file (const gchar *fname); + G_END_DECLS