Hi All,

Attached is a patch to wpkg.js (against 0.9-test4 - I'm a bit behind the times) and a perl script to add the following file comparisons:


<check
        type=file
                condition=versiongreaterthan
                condition=versiongreaterorequal
                condition=versionequalto
                condition=versionlessorequal
                condition=versionlessthan
                        path=path\to\file
                                value=comparison_value />

e.g.

<check type="file" condition="versiongreaterthan" path='path\to\file.exe' value="10.0.6501.0" />

will be met if path\to\file.exe has any of the following versions:

11.*.*.*
10.1.*.*, 10.2.*.* (etc)
10.0.6502.*, 10.0.6503 (etc)
10.0.6501.1, 10.0.6501.2 (etc)

The patch also includes a call to expand environment variables in the 'path' of the check if the check type is 'file'.

Version comparison is done by a perl script which requires perl and an extra perl library installed. Does anyone know how to get a file version in native JScript without requiring the use of perl, which may not be available on all machines?

Yours,

Frank

--
Frank Lee
Sidney Sussex College, Cambridge.              http://www.sid.cam.ac.uk/
Semiconductor Physics, Cavendish Laboratory http://www.sp.phy.cam.ac.uk/
CRL, Toshiba Research Europe     http://www.toshiba-europe.com/research/
--- wpkg-original.js    2005-09-07 15:02:34.000000000 +0100
+++ wpkg.js     2005-09-07 15:00:50.000000000 +0100
@@ -1122,8 +1122,12 @@
  * tests for files
  */
     } else if (checkType == "file") {
+        var objShell = WScript.CreateObject("WScript.Shell");
+       // Perform environment substitution
+       checkPath=objShell.ExpandEnvironmentStrings(checkPath);
         var fso = new ActiveXObject("Scripting.FileSystemObject");
         try {
+           var objShell = WScript.CreateObject("WScript.Shell");
             val = fso.GetFile(checkPath);
         } catch (e) {
             val = null;
@@ -1200,6 +1204,91 @@
                 }
                 return false;
             }
+       } else if (checkCond == "versionsmallerthan") {
+            CheckValFromFileSystem = GetFileVersion(checkPath);
+           CheckValFromWpkg       = checkValue;
+           if ((CheckValFromFileSystem != "UNKNOWN") 
+            && (VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) == 
-1)) {
+               if (debug) {
+                   info ("successful File check on \"" + val.Path + "\"\nFile 
version " +
+                         CheckValFromFileSystem + " is smaller than " + 
CheckValFromWpkg + ".");
+               }
+               return true;
+           } else {
+               if (debug) {
+                   info ("failed File check on \"" + val.Path + "\"\nFile 
version " + 
+                         CheckValFromFileSystem + " is NOT smaller than " + 
CheckValFromWpkg + ".");
+               }
+               return false;
+           }
+       } else if (checkCond == "versiongreaterthan") {
+            CheckValFromFileSystem = GetFileVersion(checkPath);
+            CheckValFromWpkg       = checkValue;
+            if ((CheckValFromFileSystem != "UNKNOWN") 
+            && (VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) == 1)) 
{
+               if (debug) { 
+                   info ("successful File check on  \"" + val.Path + "\"\nFile 
version " +
+                         CheckValFromFileSystem + " is greater than " + 
CheckValFromWpkg + ".");
+               }
+               return true;
+           } else {
+               if (debug) {
+                   info ("failed File check on  \"" + val.Path + "\"\nFile 
version " +
+                         CheckValFromFileSystem + " is NOT greater than " + 
CheckValFromWpkg + ".");
+               }
+           }
+       } else if (checkCond == "versionequalto") {
+           CheckValFromFileSystem = GetFileVersion(checkPath);
+            CheckValFromWpkg       = checkValue;
+            if ((CheckValFromFileSystem != "UNKNOWN") 
+            && (VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) == 0)) 
{
+                if (debug) {
+                    info ("successful File check on  \"" + val.Path + 
"\"\nFile version " +
+                          CheckValFromFileSystem + " is greater than " + 
CheckValFromWpkg + ".");
+               }
+               return true;
+           } else {
+               if (debug) {
+                   info ("failed File check on  \"" + val.Path + "\"\nFile 
version " +
+                         CheckValFromFileSystem + " is NOT greater than " + 
CheckValFromWpkg + ".");
+               }
+           }
+       } else if (checkCond == "versiongreaterorequal") {
+           CheckValFromFileSystem = GetFileVersion(checkPath);
+           CheckValFromWpkg       = checkValue;
+           if ((CheckValFromFileSystem != "UNKNOWN")
+               && ((VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) == 
0)
+                   || (VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) 
== 1))) {
+                if (debug) {
+                   info ("Successful file check on \"" + val.Path + "\"/nFile 
version " +
+                         CheckValFromFileSystem + " is greater than or equal 
to " + CheckValFromWpkg + ".");
+               }
+               return true;
+            } else {
+               if (debug) {
+                   info ("Failed file check on \"" + val.Path + "\"\nFile 
version " +
+                         CheckValFromFileSystem + " is NOT (greater than or 
equal to) " + CheckValFromWpkg + ".");
+               }
+               
+           }
+       } else if (checkCond == "versionlessorequal") {
+           CheckValFromFileSystem = GetFileVersion(checkPath);
+           CheckValFromWpkg       = checkValue;
+           if ((CheckValFromFileSystem != "UNKNOWN")
+               && ((VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) == 
0)
+                   || (VersionCompare(CheckValFromFileSystem,CheckValFromWpkg) 
== -1))) {
+               if (debug) {
+                   info ("Successful file check on \"" + val.Path + "\"/nFile 
version " +
+                         CheckValFromFileSystem + " is greater than or equal 
to " + CheckValFromWpkg + ".");
+               }
+               return true;
+           } else {
+               if (debug) {
+                   info ("Failed file check on \"" + val.Path + "\"\nFile 
version " +
+                         CheckValFromFileSystem + " is NOT (greater than or 
equal to) " + CheckValFromWpkg + "."
+                         );
+               }
+           }
         } else {
             throw new Error("Check condition " + checkCond + " unknown " +
                 "for type " + checkType + ".");
@@ -1238,7 +1327,6 @@
                 "for type " + checkType + ".");
             return false;
         }
-
     } else {
         throw new Error("Check condition type " + checkType + " unknown.");
         return false;
@@ -1248,8 +1336,69 @@
 }
 
 
+/**
+ *  Gets the version of a file - by running perl, of course (-:
+ */
+function GetFileVersion (file) {
+    // Check for perl library
+    var library="c:\\perl\\site\\lib\\Win32\\File\\VersionInfo.pm";
+    var fso=new ActiveXObject("Scripting.FileSystemObject");
+    var version="UNKNOWN";
+    try {
+       val = fso.GetFile(library);
+       if (debug) { info ("Located "+library+" successfully.");}
+    } catch (e) {
+        val = null;
+       if (debug) { info ("Unable to locate "+library+" successfully.");}
+    }
+    if (val != null) {
+        try {
+            if (debug) { info ("Finding version of "+file+"\n");}
+
 
+           var WshShell = new ActiveXObject("WScript.Shell");
+           var oExec    = WshShell.Exec("c:\\perl\\bin\\perl 
\\\\spd\\wpkg\\file-version.pl \""+file+"\"");
+           var output = oExec.StdOut.ReadAll();
+
+            if (oExec.Status == 0) {
+               WScript.Echo ("Sleeping more");
+               WScript.Sleep (100);
+               output += oExec.StdOut.ReadAll();
+           }
+           WScript.Echo ("Got output "+output);
+           version = output;
+           if (debug) { info ("Obtained version \""+version+"\".");}
+       } catch (e) {
+           version="UNKNOWN";
+           if (debug) { info ("Unable to run the perl script...");}
+       }
+        if (debug) { info ("Leaving GetFileVersion with version "+version); }
+    }
+    return version;
+}
 
+/**
+ *   VersionCompare - compare two executable versions
+ */
+function VersionCompare(a,b) {
+ // Return 0 if equal 
+ // Return -1 if a < b
+ // Return +1 if a > b
+ var as = a.split(".");
+ var bs = b.split(".");
+ var length=as.length;
+ var ret=0;
+ for (var i = 0; i < length; i++) {
+  if (as[i]<bs[i]) {
+   ret=-1;
+   i=length; // Hack to exit loop
+  } else if (as[i]<bs[i]) {
+   ret=1;
+   i=length;
+  } 
+ }
+ return ret;
+}
 
 /**
  *  Check if package is installed.

Attachment: file-version.pl
Description: Perl program

Reply via email to