Merge the recent changes from HEAD onto the branch
[beagle.git] / Filters / FilterEbuild.cs
blob5553632e09e1f89c0accf3c3aa98a1c326dabd30
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 ();
71 // get installation information
72 if (! file.FullName.StartsWith ("/var/db/pkg/"))
73 return;
74 FileInfo contents = new FileInfo (Path.Combine (file.Directory.FullName, "CONTENTS"));
75 if (!contents.Exists)
76 return;
77 AddProperty (Beagle.Property.NewDate ("fixme:install_time", contents.LastWriteTime));
79 // find installed objects
80 long file_count = 0;
81 long dir_count = 0;
82 long byte_count = 0;
83 StreamReader contents_reader = new StreamReader (new FileStream (contents.FullName, FileMode.Open, FileAccess.Read, FileShare.Read));
84 string contents_line = null;
85 while ((contents_line = contents_reader.ReadLine ()) != null) {
86 if (contents_line.StartsWith ("dir"))
87 dir_count ++;
88 else if (!contents_line.StartsWith ("obj"))
89 continue;
91 file_count ++;
92 string[] contents_parts = contents_line.Split (' ');
93 if (contents_parts.Length < 2)
94 continue;
95 FileInfo desktop_file = null;
96 try {
97 FileInfo installed = new FileInfo (contents_parts[1]);
98 if (installed.Exists)
100 byte_count += installed.Length;
101 if (contents_parts[1].EndsWith (".desktop"))
102 desktop_file = installed;
105 catch (IOException) {
106 // Mostly likely caused by permission error
109 if (desktop_file == null)
110 continue;
111 // verify this is a desktop file
112 StreamReader desktop_reader = new StreamReader (new FileStream (desktop_file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read));
113 string desktop_line = null;
114 bool desktop_valid = false;
115 while ((desktop_line = desktop_reader.ReadLine ()) != null) {
116 if (desktop_line.Trim ().Length > 0) {
117 desktop_valid = desktop_line.Equals ("[Desktop Entry]");
118 break;
122 // add property
123 if (desktop_valid)
124 AddProperty (Beagle.Property.NewUnsearched ("fixme:desktop_file", desktop_file.FullName));
127 AddProperty (Beagle.Property.NewUnsearched ("fixme:contents_byte_count", byte_count));
128 AddProperty (Beagle.Property.NewUnsearched ("fixme:contents_file_count", file_count));
129 AddProperty (Beagle.Property.NewUnsearched ("fixme:contents_dir_count", dir_count));
132 override protected void PullPackageProperties ()
134 string str = null;
135 while ((str = TextReader.ReadLine ()) != null) {
136 // Skip comments
137 if (str.StartsWith ("#"))
138 continue;
140 // Handle line continuation
141 string str2 = null;
142 while (str.Trim ().EndsWith ("\\") && ((str2 = TextReader.ReadLine ()) != null) ) {
143 str = str.Trim ();
144 if (str.Length == 1)
145 str = str2;
146 else
147 str = str.Substring (0, str.Length - 1) + " " + str2.Trim ();
150 if (str.Length == 0)
151 continue;
153 // check for meta data
154 MatchCollection matches;
155 matches = metadata_pattern.Matches (str);
157 if (matches.Count > 0) {
158 foreach (Match the_match in matches) {
159 String key = the_match.Groups ["key"].ToString ();
160 String val = the_match.Groups ["value"].ToString ();
161 if (key.Equals ("DESCRIPTION"))
162 Summary = val; // Ebuild descriptions are short - use them as summary.
163 else if (key.Equals ("LICENSE"))
164 AddProperty (Beagle.Property.NewUnsearched ("dc:rights", val));
165 else if (key.Equals ("HOMEPAGE"))
166 Homepage = val;
168 } else {
169 // check for einfo/ewarn
170 matches = einfo_pattern.Matches (str);
171 if (matches.Count > 0) {
172 foreach (Match the_match in matches)
173 AppendText (the_match.Groups ["message"].ToString ());