2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / Filters / FilterEbuild.cs
blob5dac7c28e183b06948ed0997392110128a1e9075
1 //
2 // FilterEbuild.cs
3 //
4 // Copyright (C) 2006 Pat Double <pat@patdouble.com>
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Text.RegularExpressions;
30 using Beagle.Daemon;
32 namespace Beagle.Filters {
34 public class FilterEbuild : FilterPackage {
35 static Regex metadata_pattern = new Regex ("\\s*(?<key>([A-Z_]+))\\s*=\\s*\"(?<value>(.*))\"\\s*");
36 static Regex einfo_pattern = new Regex ("\\s*(einfo|ewarn)\\s+\"(?<message>(.*))\"\\s*");
37 static Regex package_pattern = new Regex ("(?<name>([^0-9]+))-(?<version>(.+)).ebuild");
39 public FilterEbuild ()
41 AddSupportedFlavor (FilterFlavor.NewFromExtension (".ebuild"));
44 override protected void DoOpen (FileInfo file)
46 Match match = package_pattern.Match (file.Name);
48 PackageName = match.Groups ["name"].ToString();
49 PackageVersion = match.Groups ["version"].ToString();
51 if (PackageName.Length == 0 && PackageVersion.Length == 0)
52 return;
54 // get download file size
55 FileInfo digest = new FileInfo (file.Directory.FullName + "/files/digest-" + PackageName + "-" + PackageVersion);
56 if (digest.Exists) {
57 long download_size = 0;
58 StreamReader digest_reader = new StreamReader (new FileStream (digest.FullName, FileMode.Open, FileAccess.Read, FileShare.Read));
59 string digest_line = null;
60 while ((digest_line = digest_reader.ReadLine ()) != null) {
61 string[] digest_parts = digest_line.Split (' ');
62 if (digest_parts.Length < 4)
63 continue;
64 if (digest_parts[0].Equals ("MD5"))
65 download_size += Int64.Parse (digest_parts[3]);
67 Size = download_size.ToString ();
68 digest_reader.Close ();
72 override protected void PullPackageProperties ()
74 string str = null;
75 while ((str = TextReader.ReadLine ()) != null) {
76 // Skip comments
77 if (str.StartsWith ("#"))
78 continue;
80 // Handle line continuation
81 string str2 = null;
82 while (str.Trim ().EndsWith ("\\") && ((str2 = TextReader.ReadLine ()) != null) ) {
83 str = str.Trim ();
84 if (str.Length == 1)
85 str = str2;
86 else
87 str = str.Substring (0, str.Length - 1) + " " + str2.Trim ();
90 if (str.Length == 0)
91 continue;
93 // check for meta data
94 MatchCollection matches;
95 matches = metadata_pattern.Matches (str);
97 if (matches.Count > 0) {
98 foreach (Match the_match in matches) {
99 String key = the_match.Groups ["key"].ToString ();
100 String val = the_match.Groups ["value"].ToString ();
101 if (key.Equals ("DESCRIPTION"))
102 Summary = val; // Ebuild descriptions are short - use them as summary.
103 else if (key.Equals ("LICENSE"))
104 AddProperty (Beagle.Property.NewUnsearched ("dc:rights", val));
105 else if (key.Equals ("HOMEPAGE"))
106 Homepage = val;
108 } else {
109 // check for einfo/ewarn
110 matches = einfo_pattern.Matches (str);
111 if (matches.Count > 0) {
112 foreach (Match the_match in matches)
113 AppendText (the_match.Groups ["message"].ToString ());