Edit displayed text.
[beagle.git] / tools / Info.cs
blob80314bb497842992a403b647a536ac4e786113de
1 //
2 // Info.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 // Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
28 using System;
29 using System.IO;
30 using System.Text;
31 using System.Reflection;
32 using System.Collections;
34 using Beagle;
35 using Beagle.Daemon;
36 using Beagle.Util;
38 class InfoTool {
40 public static void PrintUsageAndExit ()
42 string usage =
43 "beagle-info: Statistics from the Beagle daemon.\n" +
44 "Web page: http://www.gnome.org/projects/beagle\n" +
45 "Copyright (C) 2004-2005 Novell, Inc.\n\n";
46 usage +=
47 "Usage: beagle-info <OPTIONS>\n\n" +
48 "Options:\n" +
49 " --daemon-version\t\tPrint the version of the running daemon.\n" +
50 " --status\t\t\tDisplay status of the running daemon.\n" +
51 " --index-info\t\t\tDisplay statistics of the Beagle indexes.\n" +
52 " --list-backends\t\tList the currently available backends.\n" +
53 " --list-filters\t\tList the currently available filters.\n" +
54 " --list-static-indexes\t\tList the available static indexes.\n" +
55 " --help\t\t\tPrint this usage message.\n";
57 Console.WriteLine (usage);
59 System.Environment.Exit (0);
62 static int Main (string[] args)
64 if (args.Length == 0 || Array.IndexOf (args, "--help") > -1)
65 PrintUsageAndExit ();
67 if (Array.IndexOf (args, "--list-filters") > -1)
68 PrintFilterInformation ();
69 else if (Array.IndexOf (args, "--list-backends") > -1)
70 PrintBackendInformation ();
71 else if (Array.IndexOf (args, "--list-static-indexes") > -1)
72 PrintStaticIndexInformation ();
73 else
74 return PrintDaemonInformation (args);
76 return 0;
79 private static int PrintDaemonInformation (string[] args)
81 DaemonInformationRequest request = new DaemonInformationRequest ();
82 DaemonInformationResponse response;
84 try {
85 response = (DaemonInformationResponse) request.Send ();
86 } catch (Beagle.ResponseMessageException) {
87 Console.WriteLine ("Could not connect to the daemon.");
88 return 1;
91 if (Array.IndexOf (args, "--daemon-version") > -1)
92 Console.WriteLine ("Daemon version: {0}", response.Version);
94 if (Array.IndexOf (args, "--status") > -1)
95 Console.Write (response.HumanReadableStatus);
97 if (Array.IndexOf (args, "--index-info") > -1) {
98 Console.WriteLine ("Index information:");
99 Console.WriteLine (response.IndexInformation);
102 if (Array.IndexOf (args, "--is-indexing") > -1)
103 Console.WriteLine ("Daemon indexing: {0}", response.IsIndexing);
105 return 0;
108 private static void PrintFilterInformation ()
110 ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_FILTER_PATH", PathFinder.FilterDir, PrintFilterDetails);
113 static void PrintFilterDetails (Assembly assembly)
115 StringBuilder sb = new StringBuilder ();
116 foreach (Type t in ReflectionFu.ScanAssemblyForClass (assembly, typeof (Filter))) {
117 Filter filter = null;
119 try {
120 filter = (Filter) Activator.CreateInstance (t);
121 } catch (Exception ex) {
122 Logger.Log.Error ("Caught exception while instantiating {0}", t);
123 Logger.Log.Error (ex);
126 if (filter == null)
127 continue;
129 string name;
131 if (t.FullName.StartsWith (t.Namespace))
132 name = t.FullName.Substring (t.Namespace.Length + 1);
133 else
134 name = t.FullName;
136 sb.Length = 0;
137 sb.Append (name + " - Version " + filter.Version + " (" + assembly.Location + ")\n");
138 bool has_filter = false;
140 foreach (FilterFlavor flavor in filter.SupportedFlavors) {
141 if (flavor.MimeType != null && (! flavor.MimeType.StartsWith ("beagle"))) {
142 sb.Append (" - " + flavor.MimeType + "\n");
143 has_filter = true;
145 if (flavor.Extension != null) {
146 sb.Append (" - *" + flavor.Extension + "\n");
147 has_filter = true;
151 if (has_filter)
152 Console.WriteLine (sb.ToString ());
156 private static void PrintBackendInformation ()
158 ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_BACKEND_PATH", PathFinder.BackendDir);
160 // Add BeagleDaemonLib if it hasn't already been added.
161 bool found_daemon_lib = false;
162 foreach (Assembly assembly in assemblies) {
163 if (assembly.GetName ().Name == "BeagleDaemonLib") {
164 found_daemon_lib = true;
165 break;
169 if (!found_daemon_lib) {
170 try {
171 assemblies.Add (Assembly.LoadFrom (Path.Combine (PathFinder.PkgLibDir, "BeagleDaemonLib.dll")));
172 } catch (FileNotFoundException) {
173 Console.WriteLine ("WARNING: Could not find backend list.");
174 Environment.Exit (1);
178 foreach (Assembly assembly in assemblies) {
179 foreach (Type type in ReflectionFu.ScanAssemblyForInterface (assembly, typeof (Beagle.Daemon.IQueryable))) {
180 foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute (type, typeof (Beagle.Daemon.QueryableFlavor))) {
181 Console.WriteLine ("{0,-20} (" + assembly.Location + ")", flavor.Name);
187 private static void PrintStaticIndexInformation ()
189 try {
190 foreach (DirectoryInfo index_dir in new DirectoryInfo (PathFinder.SystemIndexesDir).GetDirectories ())
191 Console.WriteLine ("[System index] " + index_dir.Name + " (" + index_dir.FullName + ")");
192 } catch (DirectoryNotFoundException) { }
194 foreach (string index_path in Conf.Daemon.StaticQueryables)
195 Console.WriteLine ("[User index] " + index_path);