[tracker/wip/sam/resource: 4/21] cli: Add `tracker extract` command



commit 42b40ea9f5773966fa611128ef0eb857ec6a3f2a
Author: Sam Thursfield <ssssam gmail com>
Date:   Sun Mar 27 00:51:06 2016 +0000

    cli: Add `tracker extract` command
    
    This wraps `$libexecdir/tracker-extract --file`. I think that the
    extractor is a really important part of Tracker that needs to be
    accessible in a standard way.
    
    Currently the output of `tracker-extract --file` contains a lot of
    log information even with `--verbosity=0`. And dumping the result as
    a SPARQL UPDATE operation isn't ideal, it'd be nicer to output Turtle
    or something.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=751991

 po/POTFILES.in                      |    1 +
 src/tracker/Makefile.am             |    3 +
 src/tracker/bash-completion/tracker |    2 +-
 src/tracker/tracker-extract.c       |  124 +++++++++++++++++++++++++++++++++++
 src/tracker/tracker-extract.h       |   25 +++++++
 src/tracker/tracker-main.c          |    2 +
 6 files changed, 156 insertions(+), 1 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 76c245b..2d11910 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -55,6 +55,7 @@ src/tracker-store/org.freedesktop.Tracker.Store.gschema.xml.in
 src/tracker/tracker-config.c
 src/tracker/tracker-daemon.c
 src/tracker/tracker-dbus.c
+src/tracker/tracker-extract.c
 src/tracker/tracker-help.c
 src/tracker/tracker-index.c
 src/tracker/tracker-info.c
diff --git a/src/tracker/Makefile.am b/src/tracker/Makefile.am
index 2b284eb..7df0d68 100644
--- a/src/tracker/Makefile.am
+++ b/src/tracker/Makefile.am
@@ -1,5 +1,6 @@
 AM_CPPFLAGS =                                          \
        $(BUILD_CFLAGS)                                \
+       -DLIBEXECDIR=\""$(libexecdir)"\"               \
        -DLOCALEDIR=\""$(localedir)"\"                 \
        -DMANDIR=\""$(mandir)"\" \
        -DTRACKER_EXTRACTOR_RULES_DIR=\""$(TRACKER_EXTRACT_RULES_DIR)"\" \
@@ -25,6 +26,8 @@ tracker_SOURCES = \
        tracker-daemon.h \
        tracker-dbus.c \
        tracker-dbus.h \
+       tracker-extract.c \
+       tracker-extract.h \
        tracker-help.c \
        tracker-help.h \
        tracker-index.c \
diff --git a/src/tracker/bash-completion/tracker b/src/tracker/bash-completion/tracker
index 12749cd..dad5dee 100644
--- a/src/tracker/bash-completion/tracker
+++ b/src/tracker/bash-completion/tracker
@@ -17,7 +17,7 @@ tracker_cmds()
        -h|--help)
            return 0;
            ;;
-       info|-f|--file)
+       extract|info|-f|--file)
            if [[ $cur != -* ]]; then
                _filedir
                return 0;
diff --git a/src/tracker/tracker-extract.c b/src/tracker/tracker-extract.c
new file mode 100644
index 0000000..a6f23c4
--- /dev/null
+++ b/src/tracker/tracker-extract.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2015-2016, Sam Thursfield <sam afuera me uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+
+#include "tracker-extract.h"
+
+static gchar **filenames;
+
+#define EXTRACT_OPTIONS_ENABLED()        \
+       ((filenames && g_strv_length (filenames) > 0))
+
+static GOptionEntry entries[] = {
+       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames,
+         N_("FILE"),
+         N_("FILE") },
+       { NULL }
+};
+
+
+static gint
+extract_files (void)
+{
+       char **p;
+       char *tracker_extract_path;
+       GError *error = NULL;
+
+       tracker_extract_path = g_build_filename(LIBEXECDIR, "tracker-extract", NULL);
+
+       for (p = filenames; *p; p++) {
+               char *argv[] = {tracker_extract_path, "--file", *p, NULL};
+
+               g_spawn_sync(NULL, argv, NULL, G_SPAWN_DEFAULT, NULL, NULL, NULL, NULL, NULL, &error);
+
+               if (error) {
+                       g_printerr ("%s: %s\n",
+                                   _("Could not run tracker-extract: "),
+                                   error->message);
+                       g_error_free (error);
+                       g_free (tracker_extract_path);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       g_free (tracker_extract_path);
+       return EXIT_SUCCESS;
+}
+
+static int
+extract_run (void)
+{
+       return extract_files ();
+}
+
+static int
+extract_run_default (void)
+{
+       GOptionContext *context;
+       gchar *help;
+
+       context = g_option_context_new (NULL);
+       g_option_context_add_main_entries (context, entries, NULL);
+       help = g_option_context_get_help (context, TRUE, NULL);
+       g_option_context_free (context);
+       g_printerr ("%s\n", help);
+       g_free (help);
+
+       return EXIT_FAILURE;
+}
+
+static gboolean
+extract_options_enabled (void)
+{
+       return EXTRACT_OPTIONS_ENABLED ();
+}
+
+int
+tracker_extract (int argc, const char **argv)
+{
+       GOptionContext *context;
+       GError *error = NULL;
+
+       context = g_option_context_new (NULL);
+       g_option_context_add_main_entries (context, entries, NULL);
+
+       argv[0] = "tracker extract";
+
+       if (!g_option_context_parse (context, &argc, (char***) &argv, &error)) {
+               g_printerr ("%s, %s\n", _("Unrecognized options"), error->message);
+               g_error_free (error);
+               g_option_context_free (context);
+               return EXIT_FAILURE;
+       }
+
+       g_option_context_free (context);
+
+       if (extract_options_enabled ()) {
+               return extract_run ();
+       }
+
+       return extract_run_default ();
+}
diff --git a/src/tracker/tracker-extract.h b/src/tracker/tracker-extract.h
new file mode 100644
index 0000000..97fd3ca
--- /dev/null
+++ b/src/tracker/tracker-extract.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2015, Sam Thursfield <sam afuera me uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __TRACKER_EXTRACT_H__
+#define __TRACKER_EXTRACT_H__
+
+int tracker_extract (int argc, const char **argv);
+
+#endif /* __TRACKER_EXTRACT_H__ */
diff --git a/src/tracker/tracker-main.c b/src/tracker/tracker-main.c
index 8c7d5b5..57d9af8 100644
--- a/src/tracker/tracker-main.c
+++ b/src/tracker/tracker-main.c
@@ -29,6 +29,7 @@
 #include <libtracker-common/tracker-common.h>
 
 #include "tracker-daemon.h"
+#include "tracker-extract.h"
 #include "tracker-help.h"
 #include "tracker-index.h"
 #include "tracker-info.h"
@@ -94,6 +95,7 @@ struct cmd_struct {
 
 static struct cmd_struct commands[] = {
        { "daemon", tracker_daemon, NEED_WORK_TREE, N_("Start, stop, pause and list processes responsible for 
indexing content") },
+       { "extract", tracker_extract, NEED_WORK_TREE, N_("Extract information from a file") },
        { "help", tracker_help, NEED_NOTHING, N_("Get help on how to use Tracker and any of these commands") 
},
        { "info", tracker_info, NEED_WORK_TREE, N_("Show information known about local files or items 
indexed") }, 
        { "index", tracker_index, NEED_NOTHING, N_("Backup, restore, import and (re)index by MIME type or 
file name") },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]