From 0e3777fc9c076e1a63ba73010dbe0b221cf0d370 Mon Sep 17 00:00:00 2001 From: dbera Date: Fri, 21 Apr 2006 21:35:45 +0000 Subject: [PATCH] * Filters/FilterPackage.cs, Filters/FilterRPM.cs, Filters/FilterEbuild.cs: Add FilterPackage base class to contain the basic information about packages. Change FilterRPM, FilterEbuild to derive from FilterPackage. --- Filters/FilterEbuild.cs | 62 +++++++++++++++++----------------- Filters/FilterPackage.cs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ Filters/FilterRPM.cs | 88 +++++++++++++++++++++++++++++------------------- Filters/Makefile.am | 3 +- 4 files changed, 175 insertions(+), 66 deletions(-) create mode 100644 Filters/FilterPackage.cs diff --git a/Filters/FilterEbuild.cs b/Filters/FilterEbuild.cs index 9973ff88..f93299c1 100644 --- a/Filters/FilterEbuild.cs +++ b/Filters/FilterEbuild.cs @@ -31,7 +31,7 @@ using Beagle.Daemon; namespace Beagle.Filters { - public class FilterEbuild : Beagle.Daemon.Filter { + public class FilterEbuild : FilterPackage { static Regex metadata_pattern = new Regex ("\\s*(?([A-Z_]+))\\s*=\\s*\"(?(.*))\"\\s*"); static Regex einfo_pattern = new Regex ("\\s*(einfo|ewarn)\\s+\"(?(.*))\"\\s*"); static Regex package_pattern = new Regex ("(?([^0-9]+))-(?(.+)).ebuild"); @@ -41,46 +41,47 @@ namespace Beagle.Filters { AddSupportedFlavor (FilterFlavor.NewFromExtension (".ebuild")); } - override protected void DoOpen (FileInfo file) + override protected void DoOpen (FileInfo file) { Match match = package_pattern.Match (file.Name); - String pkgname = match.Groups ["name"].ToString(); - if (pkgname.Length > 0) - AddProperty (Beagle.Property.New ("dc:title", pkgname)); - - String version = match.Groups ["version"].ToString(); - if (version.Length > 0) - AddProperty (Beagle.Property.NewUnsearched ("fixme:version", version)); + + PackageName = match.Groups ["name"].ToString(); + PackageVersion = match.Groups ["version"].ToString(); + + if (PackageName.Length == 0 && PackageVersion.Length == 0) + return; // get download file size - if (pkgname.Length > 0 && version.Length > 0) { - FileInfo digest = new FileInfo (file.Directory.FullName + "/files/digest-" + pkgname + "-" + version); - if (digest.Exists) { - long download_size = 0; - StreamReader digest_reader = new StreamReader (new FileStream (digest.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)); - string digest_line = null; - while ((digest_line = digest_reader.ReadLine ()) != null) { - string[] digest_parts = digest_line.Split (' '); - if (digest_parts.Length < 4) - continue; - if (digest_parts[0].Equals ("MD5")) - download_size += Int64.Parse (digest_parts[3]); - } - AddProperty (Beagle.Property.NewUnsearched ("fixme:download_size", download_size)); + FileInfo digest = new FileInfo (file.Directory.FullName + "/files/digest-" + PackageName + "-" + PackageVersion); + if (digest.Exists) { + long download_size = 0; + StreamReader digest_reader = new StreamReader (new FileStream (digest.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)); + string digest_line = null; + while ((digest_line = digest_reader.ReadLine ()) != null) { + string[] digest_parts = digest_line.Split (' '); + if (digest_parts.Length < 4) + continue; + if (digest_parts[0].Equals ("MD5")) + download_size += Int64.Parse (digest_parts[3]); } + AddProperty (Beagle.Property.NewUnsearched ("fixme:download_size", download_size)); + digest_reader.Close (); } + - StreamReader reader = new StreamReader (new FileStream (file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)); + } + override protected void PullPackageProperties () + { string str = null; - while ((str = reader.ReadLine ()) != null) { + while ((str = TextReader.ReadLine ()) != null) { // Skip comments if (str.StartsWith ("#")) continue; // Handle line continuation string str2 = null; - while (str.Trim ().EndsWith ("\\") && ((str2 = reader.ReadLine ()) != null) ) { + while (str.Trim ().EndsWith ("\\") && ((str2 = TextReader.ReadLine ()) != null) ) { str = str.Trim (); if (str.Length == 1) str = str2; @@ -97,13 +98,13 @@ namespace Beagle.Filters { if (matches.Count > 0) { foreach (Match the_match in matches) { String key = the_match.Groups ["key"].ToString (); - String value = the_match.Groups ["value"].ToString (); + String val = the_match.Groups ["value"].ToString (); if (key.Equals ("DESCRIPTION")) - AddProperty (Beagle.Property.New ("dc:description", value)); + Description = val; else if (key.Equals ("LICENSE")) - AddProperty (Beagle.Property.New ("dc:rights", value)); + License = val; else if (key.Equals ("HOMEPAGE")) - AddProperty (Beagle.Property.New ("dc:source", value)); + Homepage = val; } } else { // check for einfo/ewarn @@ -114,7 +115,6 @@ namespace Beagle.Filters { } } } - Finished (); } } } diff --git a/Filters/FilterPackage.cs b/Filters/FilterPackage.cs new file mode 100644 index 00000000..5308ddce --- /dev/null +++ b/Filters/FilterPackage.cs @@ -0,0 +1,88 @@ +// +// FilterPackage.cs +// +// Copyright (C) 2006 Debajyoti Bera +// + +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + +using System; + +using Beagle.Util; + +namespace Beagle.Filters { + + public abstract class FilterPackage : Beagle.Daemon.Filter { + + public FilterPackage () + { + } + + protected virtual void PullPackageProperties () { } + + private string package_name, package_version, description, license, homepage, summary; + + protected string PackageName { + get { return package_name; } + set { package_name = value; } + } + + protected string PackageVersion { + get { return package_version; } + set { package_version = value; } + } + + protected string Description { + get { return description; } + set { description = value; } + } + + protected string License { + get { return license; } + set { license = value; } + } + + protected string Homepage { + get { return homepage; } + set { homepage = value; } + } + + protected string Summary { + get { return summary; } + set { summary = value; } + } + + protected override void DoPullProperties () + { + PullPackageProperties (); + + AddProperty (Beagle.Property.New ("dc:title", package_name)); + AddProperty (Beagle.Property.NewUnsearched ("fixme:version", package_version)); + AddProperty (Beagle.Property.New ("dc:description", description)); + AddProperty (Beagle.Property.New ("dc:rights", license)); + AddProperty (Beagle.Property.NewUnsearched ("dc:source", homepage)); + AddProperty (Beagle.Property.New ("dc:subject", summary)); + + Finished (); + } + + } +} diff --git a/Filters/FilterRPM.cs b/Filters/FilterRPM.cs index 22bb1112..34fafb23 100644 --- a/Filters/FilterRPM.cs +++ b/Filters/FilterRPM.cs @@ -35,7 +35,7 @@ using Beagle.Daemon; using Beagle.Util; namespace Beagle.Filters { - public class FilterRPM : Beagle.Daemon.Filter { + public class FilterRPM : FilterPackage { private class RpmPropertyInfo { public string property_name; @@ -54,14 +54,8 @@ namespace Beagle.Filters { hash_property_list = new Hashtable (); // mapping between rpm tagname and beagle property name - hash_property_list ["Name"] = new RpmPropertyInfo ("dc:title", false); - hash_property_list ["Version"] = new RpmPropertyInfo ("fixme:version", true); hash_property_list ["Release"] = new RpmPropertyInfo ("fixme:release", true); - hash_property_list ["Summary"] = new RpmPropertyInfo ("dc:subject", false); - hash_property_list ["Description"] = new RpmPropertyInfo ("dc:description", false); - hash_property_list ["License"] = new RpmPropertyInfo ("dc:rights", false); hash_property_list ["Group"] = new RpmPropertyInfo ("fixme:group", false); - hash_property_list ["Url"] = new RpmPropertyInfo ("dc:source", true); hash_property_list ["Os"] = new RpmPropertyInfo ("fixme:os", false); hash_property_list ["Arch"] = new RpmPropertyInfo ("fixme:arch", false); hash_property_list ["Changelogtext"] = new RpmPropertyInfo ("fixme:changelog", false); @@ -72,7 +66,7 @@ namespace Beagle.Filters { AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-rpm")); } - protected override void DoPullProperties () + protected override void PullPackageProperties () { SafeProcess pc = new SafeProcess (); pc.Arguments = new string [] { "rpm", "-qp", "--queryformat", "[%{*:xml}\n]", FileInfo.FullName }; @@ -99,14 +93,10 @@ namespace Beagle.Filters { reader.Close (); pc.Close (); } - - Finished (); } private void ParseRpmTags (XmlTextReader reader) { - RpmPropertyInfo prop_info = null; - reader.Read (); while (reader.Read ()) { if (reader.IsEmptyElement || ! reader.IsStartElement ()) @@ -118,20 +108,23 @@ namespace Beagle.Filters { string attr_name = reader ["name"]; //Logger.Log.Debug ("Read element:" + reader.Name + " - " + attr_name); - // store basenames values as Text - they are like the "text"-content of rpm files - if (attr_name == "Basenames") { - ReadStringValues (reader, true, null); - continue; - } - - prop_info = (RpmPropertyInfo) hash_property_list [attr_name]; - if (prop_info != null) - ReadStringValues (reader, false, prop_info); + ReadStringValues (reader, attr_name); } } - private void ReadStringValues (XmlTextReader reader, bool store_as_text, RpmPropertyInfo prop_info) + private void ReadStringValues (XmlTextReader reader, string attr_name) { + RpmPropertyInfo prop_info = (RpmPropertyInfo) hash_property_list [attr_name]; + if (attr_name != "Basenames" && + attr_name != "Name" && + attr_name != "Version" && + attr_name != "License" && + attr_name != "Description" && + attr_name != "Url" && + attr_name != "Summary" && + prop_info == null) + return; + reader.ReadStartElement (); while (reader.IsStartElement ()) { @@ -140,20 +133,47 @@ namespace Beagle.Filters { reader.Skip (); string content = HtmlAgilityPack.HtmlEntity.DeEntitize (reader.ReadInnerXml ()); - //Logger.Log.Debug (prop_info.property_name - // + (prop_info.is_keyword ? " (keyword)" : " (text)") - // + " = [" + content + "]"); - if (store_as_text) { - AppendText (content); - AppendWhiteSpace (); - continue; + switch (attr_name) { + case "Basenames": + // store basenames values as Text - they are like the "text"-content of rpm files + AppendText (content); + AppendWhiteSpace (); + break; + + case "Name": + PackageName = content; + break; + + case "Version": + PackageVersion = content; + break; + + case "License": + License = content; + break; + + case "Summary": + Summary = content; + break; + + case "Description": + Description = content; + break; + + case "Url": + Homepage = content; + break; + + default: + if (prop_info == null) + break; + if (prop_info.is_keyword) + AddProperty (Beagle.Property.NewUnsearched (prop_info.property_name, content)); + else + AddProperty (Beagle.Property.New (prop_info.property_name, content)); + break; } - - if (prop_info.is_keyword) - AddProperty (Beagle.Property.New (prop_info.property_name, content)); - else - AddProperty (Beagle.Property.NewUnsearched (prop_info.property_name, content)); } //Logger.Log.Debug (" Done reading values. Now at " + diff --git a/Filters/Makefile.am b/Filters/Makefile.am index 389614ce..5641804e 100644 --- a/Filters/Makefile.am +++ b/Filters/Makefile.am @@ -65,7 +65,8 @@ CSFILES = \ $(srcdir)/FilterEbuild.cs \ $(srcdir)/FilterGif.cs \ $(srcdir)/FilterXslt.cs \ - $(srcdir)/FilterRPM.cs + $(srcdir)/FilterRPM.cs \ + $(srcdir)/FilterPackage.cs if ENABLE_GSF_SHARP CSFILES += \ -- 2.11.4.GIT