[banshee] [solitary] proper native lib detection, cleanup



commit 7f5abb9a59add09ad7696504f3bd6b223b69fa45
Author: Aaron Bockover <abockover novell com>
Date:   Wed Dec 30 11:31:11 2009 -0500

    [solitary] proper native lib detection, cleanup
    
    Various cleanup and organization around external processes.
    Invoke the 'file' tool to sniff for Mach-O and PE32 (managed),
    and create the right item object based on the sniff instead of
    working against the file extension.
    
    Also detect the host type (e.g. linux, darwin) and invoke ldd
    or otool accordingly.
    
    Blacklist regular expressions are now supported.

 build/bundle/solitary/DataItem.cs          |   40 +++++++++++
 build/bundle/solitary/Entry.cs             |    7 +-
 build/bundle/solitary/Item.cs              |   51 +++++++++++---
 build/bundle/solitary/Makefile             |    2 +
 build/bundle/solitary/NativeLibraryItem.cs |   47 +-----------
 build/bundle/solitary/ProcessTools.cs      |  105 ++++++++++++++++++++++++++++
 build/bundle/solitary/Solitary.cs          |   22 +++----
 7 files changed, 204 insertions(+), 70 deletions(-)
---
diff --git a/build/bundle/solitary/DataItem.cs b/build/bundle/solitary/DataItem.cs
new file mode 100644
index 0000000..0e530ae
--- /dev/null
+++ b/build/bundle/solitary/DataItem.cs
@@ -0,0 +1,40 @@
+// 
+// DataItem.cs
+//  
+// Author:
+//   Aaron Bockover <abockover novell com>
+// 
+// Copyright 2009-2010 Novell, Inc.
+// 
+// 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 System.Collections.Generic;
+
+public class DataItem : Item
+{
+    public override IEnumerable<Item> Load ()
+    {
+        if (!IsValidConfinementItem (this)) {
+            yield break;
+        }
+        
+        yield return this;
+    }
+}
diff --git a/build/bundle/solitary/Entry.cs b/build/bundle/solitary/Entry.cs
index 5cc26c7..d4b8032 100644
--- a/build/bundle/solitary/Entry.cs
+++ b/build/bundle/solitary/Entry.cs
@@ -56,15 +56,16 @@ public static class Entry
             return;
         }
 
-        if (show_help) {
-            Console.WriteLine ("Usage: Shoes [OPTIONS]+ <managed_dir1> <managed_dir2>...");
+        if (show_help || paths.Count == 0) {
+            Console.WriteLine ("Usage: solitary [OPTIONS]+ <paths>...");
             Console.WriteLine ();
             Console.WriteLine ("Options:");
             p.WriteOptionDescriptions (Console.Out);
+            Console.WriteLine ();
             return;
         }
         
-        solitary.LoadBlacklist (blacklist_file);
+        solitary.LoadPathBlacklist (blacklist_file);
         
         long total_size = 0;
         Console.WriteLine ("Locating items...");
diff --git a/build/bundle/solitary/Item.cs b/build/bundle/solitary/Item.cs
index 2ffe20e..bc821a8 100644
--- a/build/bundle/solitary/Item.cs
+++ b/build/bundle/solitary/Item.cs
@@ -30,6 +30,13 @@ using System.Collections.Generic;
 
 using Mono.Unix;
 
+public enum FileType
+{
+    PE32Executable,
+    MachO,
+    Data
+}
+
 public abstract class Item
 {
     public Solitary Confinement { get; set; }
@@ -70,26 +77,48 @@ public abstract class Item
 
         Item item = null;
 
-        switch (Path.GetExtension (file.Name)) {
-            case ".dll":
-            case ".exe":
+        foreach (var regex in confinement.PathBlacklist) {
+            if (regex.IsMatch (file.FullName)) {
+                return null;
+            }
+        }
+
+        switch (GetFileType (file)) {
+            case FileType.PE32Executable: 
                 item = new AssemblyItem ();
                 break;
-            case ".la":
-            case ".a":
+            case FileType.MachO:
+                item = new NativeLibraryItem ();
                 break;
             default:
-                item = new NativeLibraryItem ();
+                item = new DataItem ();
                 break;
         }
 
-        if (item != null) {
-            item.Confinement = confinement;
-            item.File = file;
+        item.Confinement = confinement;
+        item.File = file;
+        return item;
+    }
+
+    public static FileType GetFileType (FileInfo file)
+    {
+        var proc = ProcessTools.CreateProcess ("file", file.FullName);
+        if (proc == null) {
+            return FileType.Data;
+        }
+        
+        var line = proc.StandardOutput.ReadLine ();
+        if (line == null) {
+            return FileType.Data;
         }
 
-        return item;
+        if (line.Contains ("Mach-O")) {
+            return FileType.MachO;
+        } else if (line.Contains ("PE32 executable")) {
+            return FileType.PE32Executable;
+        }
+
+        return FileType.Data;
     }
 }
 
-
diff --git a/build/bundle/solitary/Makefile b/build/bundle/solitary/Makefile
index c23b2c1..8d790a8 100644
--- a/build/bundle/solitary/Makefile
+++ b/build/bundle/solitary/Makefile
@@ -4,6 +4,8 @@ SOURCE = \
 	Item.cs \
 	AssemblyItem.cs \
 	NativeLibraryItem.cs \
+	DataItem.cs \
+	ProcessTools.cs \
 	Entry.cs \
 	Options.cs
 
diff --git a/build/bundle/solitary/NativeLibraryItem.cs b/build/bundle/solitary/NativeLibraryItem.cs
index ab576ad..45c2bdf 100644
--- a/build/bundle/solitary/NativeLibraryItem.cs
+++ b/build/bundle/solitary/NativeLibraryItem.cs
@@ -26,7 +26,6 @@
 
 using System;
 using System.IO;
-using System.Diagnostics;
 using System.Collections.Generic;
 
 public class NativeLibraryItem : Item
@@ -37,54 +36,16 @@ public class NativeLibraryItem : Item
             yield break;
         }
 
-        foreach (var item in LddFile ()) {
-            yield return item;
-        }
-    }
-
-    private Process CreateProcess (string cmd, string args)
-    {
-        return Process.Start (new ProcessStartInfo (cmd, args) {
-            RedirectStandardOutput = true,
-            RedirectStandardError = true,
-            UseShellExecute = false
-        });
-    }
-
-    private IEnumerable<Item> LddFile ()
-    {
-        Process proc = null;
-
-        try {
-            proc = CreateProcess ("ldd", File.FullName);
-        } catch {
-            proc = CreateProcess ("otool", "-L " + File.FullName);
-        }
-
-        proc.WaitForExit ();
-        if (proc.ExitCode != 0) {
+        var deps = ProcessTools.GetNativeDependencies (File);
+        if (deps == null) {
             yield break;
         }
 
         yield return this;
 
-        string line;
-        while ((line = proc.StandardOutput.ReadLine ()) != null) {
-            line = line.Trim ();
-
-            int addr = line.IndexOf ('(');
-            if (addr < 0) {
-                continue;
-            }
-
-            line = line.Substring (0, addr);
-            int map = line.IndexOf (" => ");
-            if (map > 0) {
-                line = line.Substring (map + 4);
-            }
-
+        foreach (var dep in deps) {
             var item = new NativeLibraryItem () {
-                File = new FileInfo (line.Trim ()),
+                File = new FileInfo (dep),
                 Confinement = Confinement
             };
 
diff --git a/build/bundle/solitary/ProcessTools.cs b/build/bundle/solitary/ProcessTools.cs
new file mode 100644
index 0000000..52e3429
--- /dev/null
+++ b/build/bundle/solitary/ProcessTools.cs
@@ -0,0 +1,105 @@
+// 
+// ProcessTools.cs
+//  
+// Author:
+//   Aaron Bockover <abockover novell com>
+// 
+// Copyright 2009-2010 Novell, Inc.
+// 
+// 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 System.IO;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+
+public enum ProcessHost
+{
+    Linux,
+    Darwin,
+    Windows
+}
+
+public static class ProcessTools
+{
+    public static ProcessHost Host { get; private set; }
+
+    static ProcessTools ()
+    {
+        var uname = CreateProcess ("uname");
+        if (uname == null) {
+            Host = ProcessHost.Windows;
+            return;
+        }
+        
+        switch (uname.StandardOutput.ReadToEnd ().Trim ().ToLower ()) {
+            case "darwin": Host = ProcessHost.Darwin; break;
+            case "linux": Host = ProcessHost.Linux; break;
+        }
+    }
+
+    public static Process CreateProcess (string cmd)
+    {
+        return CreateProcess (cmd, null);
+    }
+
+    public static Process CreateProcess (string cmd, string args)
+    {
+        try {
+            var proc = Process.Start (new ProcessStartInfo (cmd, args) {
+                RedirectStandardOutput = true,
+                RedirectStandardError = true,
+                UseShellExecute = false
+            });
+
+            proc.WaitForExit ();
+            if (proc.ExitCode != 0) {
+                return null;
+            }
+            
+            return proc;
+        } catch {
+            return null;
+        }
+    }
+ 
+    public static List<string> GetNativeDependencies (FileInfo file)
+    {
+        var proc = Host == ProcessHost.Darwin
+            ? CreateProcess ("otool", "-L " + file.FullName)
+            : CreateProcess ("ldd", file.FullName);
+        
+        if (proc == null) {
+            return null;
+        }
+
+        var items = new List<string> ();
+        string line;
+
+        while ((line = proc.StandardOutput.ReadLine ()) != null) {
+            var match = Regex.Match (line, @"^\s+(.+)\(.+");
+            if (match.Success) {
+                items.Add (match.Groups[1].Value.Trim ());
+            }
+        }
+
+        return items;
+    }
+}
diff --git a/build/bundle/solitary/Solitary.cs b/build/bundle/solitary/Solitary.cs
index 62740e6..258ebcd 100644
--- a/build/bundle/solitary/Solitary.cs
+++ b/build/bundle/solitary/Solitary.cs
@@ -28,10 +28,11 @@ using System;
 using System.IO;
 using System.Diagnostics;
 using System.Collections.Generic;
+using System.Text.RegularExpressions;
 
 public class Solitary
 {
-    public List<string> Blacklist { get; private set; }
+    public List<Regex> PathBlacklist { get; private set; }
     public List<Item> Items { get; private set; }
     public List<string> SearchPaths { get; private set; }
     public string MonoPrefix { get; set; }
@@ -46,7 +47,7 @@ public class Solitary
 
     public Solitary ()
     {
-        Blacklist = new List<string> ();
+        PathBlacklist = new List<Regex> ();
         Items = new List<Item> ();
         SearchPaths = new List<string> ();
 
@@ -92,7 +93,7 @@ public class Solitary
         }
     }
 
-    public void LoadBlacklist (string blacklistFile)
+    public void LoadPathBlacklist (string blacklistFile)
     {
         if (blacklistFile == null) {
             return;
@@ -103,7 +104,7 @@ public class Solitary
             while ((line = reader.ReadLine ()) != null) {
                 line = line.Trim ();
                 if (!String.IsNullOrEmpty (line) && line[0] != '#') {
-                    Blacklist.Add (line);
+                    PathBlacklist.Add (new Regex (line, RegexOptions.Compiled));
                 }
             }
         }
@@ -151,7 +152,8 @@ public class Solitary
             path = Path.Combine (OutputPath, path);
 
             Directory.CreateDirectory (Path.GetDirectoryName (path));
-            if (!strip || !StripBinary (item.File.FullName, path)) {
+            if (!strip || !(item is NativeLibraryItem) ||
+                !StripBinary (item.File.FullName, path)) {
                 File.Copy (item.File.FullName, path);
             }
         }
@@ -159,13 +161,7 @@ public class Solitary
 
     private static bool StripBinary (string source, string target)
     {
-        var proc = Process.Start (new ProcessStartInfo ("strip",
-            String.Format ("-u -r -o \"{1}\" \"{0}\"", source, target)) {
-            RedirectStandardOutput = true,
-            RedirectStandardError = true,
-            UseShellExecute = false
-        });
-        proc.WaitForExit ();
-        return proc.ExitCode == 0;
+        return ProcessTools.CreateProcess ("strip",
+            String.Format ("-u -r -o \"{1}\" \"{0}\"", source, target)) != null;
     }
 }



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