Murga-Projects Forums
testing presence of ML feature - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: testing presence of ML feature (/showthread.php?tid=262)


testing presence of ML feature - mikshaw - 01-20-2008 05:37 AM

I sometimes write scripts to be supported by multiple versions of murgaLua, for example in the case where I want to utilize a recent addition but don't want the application to break if the user has an older version. What I've done so far is check for the existence of a package that was added with the same version as the desired feature:

Code:
if not package.loaded.md5 then
print("This feature requires murgaLua version 0.6 or later")
else
<code for version 0.6>
end


This isn't ideal for more than one reason.

I don't know why it never occurred to me before, but you can use the same concept to check for the specific feature you want to use. For example, if you want to use Fl_Pixmap on 0.6 but prevent a close on 0.5.5 and earlier:

Code:
if fltk.Fl_Pixmap then
<code for version 0.6>
end


Of course you'll need to know the path to the specific object you want to test, but you can use this tool to help.


RE: testing presence of ML feature - mikshaw - 02-07-2008 05:45 AM

Another possible test is for a specific version of murgaLua, in the event that you have multiple new features in your script and just don't want to support older versions. You could check for a feature that was added to that specific version, but it's *possible* that sometime in the future that feature could be removed or renamed. You would always need to make sure you use a feature that is specific to your script, rather than just a generic cut-and-paste test. That's not a serious issue, but anything that can reuse existing code makes development go more smoothly.

I couldn't figure out why I couldn't use tonumber(murgaLua.version) for comparison, so I didn't bother with it. Then it struck me that a string with two decimals in it can't be converted into a number, so I decided to do string comparisons instead:

Code:
-- find the smallest negative index in the arg table
-- this is the Lua interpreter
for i in pairs(arg) do
if not low or i < low then low=i end
end
lua_interpreter=arg[low]

-- test the murgaLua version
err="This program requires murgaLua 0.5.5 or newer.\nhttp://www.murga-projects.com/murgaLua/index.html"
notallowed={"0.1","0.2","0.3","0.4","0.4.1","0.5"} -- add "0.5.5" if you need 0.6
for k,v in ipairs(notallowed) do
  if not murgaLua.version or v == murgaLua.version then
    print(lua_interpreter..": "..err) -- print error in case it's tried in regular Lua
    fltk.fl_alert(err) -- popup error
    os.exit(1)
  end
end


This method allows you to simply copy and paste the code reliably into any script that needs at least version 0.5.5
It also uses an additional print() just in case the end user tries to run the script in some other kind of Lua interpreter. I haven't tested this in Lua proper yet, but i think it would exit cleanly just from not finding murgaLua.version

Maybe there are simpler ways to do this, but this at least is more reliable for general use.


RE: testing presence of ML feature - Juergen - 02-07-2008 06:54 AM

I think you are looking for something like that:

Code:
do
  local iter=string.gmatch(murgaLua.version,"%d+")
  local major,minor,sub=tonumber(iter()),tonumber(iter()),tonumber(iter())
  if minor<=5 and sub<=5 then print"You must use a murgaLua version > 0.5.5" os.exit(1) end
end


Juergen


RE: testing presence of ML feature - Juergen - 02-07-2008 08:40 AM

Alternatively:

Code:
do
  local version=0; for x in string.gmatch(murgaLua and murgaLua.version or "0","%d+") do version=version*100+tonumber(x) end
  if version<=0505 then print"You must use a murgaLua version > 0.5.5" os.exit(1) end
end


This works also in alternative Lua interpreters. This way you don't have to keep track over all the past murgaLua versions

Juergen


RE: testing presence of ML feature - mikshaw - 02-07-2008 08:44 AM

Yes, that's exactly the result I was originally thinking of, but wasn't sure where to start. Thanks. =o)
I'm definitely saving this for future tests, and putting it in with the rest of the code snippets.

I don't really understand the syntax of "do...end" though. Is that just to keep the variables local?


RE: testing presence of ML feature - Juergen - 02-07-2008 09:07 AM

mikshaw Wrote:
Yes, that's exactly the result I was originally thinking of, but wasn't sure where to start. Thanks. =o)
I'm definitely saving this for future tests, and putting it in with the rest of the code snippets.

I don't really understand the syntax of "do...end" though. Is that just to keep the variables local?


The do and end delimits a block. Inside you have your own scope. It is basically the same as { } in C, where you can also define new variables.

I used the do .. end only because I wanted to declare the variables local, since it doesn't make sense to have them as globals. Lua doesn't allow you to have local variables in the outermost scope:

Code:
> local a=6
> print(a)
nil


which also doesn't make sense.

Of course it would make sense to put it into a function.

Code:
function minimal_version(min_version)
  local version=0; for x in string.gmatch(murgaLua and murgaLua.version or "0","%d+") do version=version*100+tonumber(x) end
  local min_vers=0; for x in string.gmatch(min_version,"%d+") do min_vers=min_vers*100+tonumber(x) end
  if version<min_vers then print"You must use a murgaLua version > 0.5.5" os.exit(1) end
end

minimal_version("0.5.5")



Juergen


RE: testing presence of ML feature - mikshaw - 02-07-2008 10:13 AM

Yeah, I had the assumption that local variables could only be used within a function.

I like that last one best. It seems it would be completely reusable without modification, except the print command should use min_version instead of a static "0.5.5" in its string. As a personal preference I'd also change the variable name of min_vers. It's a bit too similar to min_version for quick reading; it took me a few minutes to figure out they were unique.


RE: testing presence of ML feature - Juergen - 02-07-2008 10:52 AM

mikshaw Wrote:
Yeah, I had the assumption that local variables could only be used within a function.

I like that last one best. It seems it would be completely reusable without modification, except the print command should use min_version instead of a static "0.5.5" in its string. As a personal preference I'd also change the variable name of min_vers. It's a bit too similar to min_version for quick reading; it took me a few minutes to figure out they were unique.


Another version:

Code:
function minimal_version(min_version)
  local x=string.gmatch((murgaLua and murgaLua.version or "0").." " .. min_version,"%d+")
  local t=function(x) return tonumber(x()) end
  if t(x)*10000+t(x)*100+t(x)-t(x)*10000-t(x)*100-t(x)<0 then
    print("You must use a murgaLua version > "..min_version) os.exit(1) end
end

minimal_version("0.5.6")


Juergen


RE: testing presence of ML feature - mikshaw - 02-08-2008 12:52 AM

It just occurred to me that testing the version string is not going to be as reliable as I'd hoped for a little while. The version string in 0.6 is "0.5.5", so until 0.6.5 it will continue to be a problem for this test. After that, testing for 0.6 will probably need to be explicitly discouraged.


RE: testing presence of ML feature - Juergen - 02-08-2008 01:25 AM

mikshaw Wrote:
It just occurred to me that testing the version string is not going to be as reliable as I'd hoped for a little while. The version string in 0.6 is "0.5.5", so until 0.6.5 it will continue to be a problem for this test. After that, testing for 0.6 will probably need to be explicitly discouraged.


Then the function should be changed a little ;-)

Code:
function minimal_version(minimal_version)
  local x=string.gmatch((murgaLua and murgaLua.version or "0"),"%d+") y=string.gmatch(minimal_version,"%d+")
  local t=function(x) return tonumber((x() or 0)) end
  local version=0; local min_version=0
  for i=1,3 do version=version*100+t(x); min_version=min_version*100+t(y) end
  if version<min_version then
    print("You must use a murgaLua version > "..minimal_version) os.exit(1) end
end

minimal_version("0.6")


Juergen


RE: testing presence of ML feature - mikshaw - 02-08-2008 01:36 AM

I don't think it works? I tested it with murgaLua 0.6 and 0.5.5, and both exited.
They both work, as expected, when you run minimal_version("0.5.5")


RE: testing presence of ML feature - Juergen - 02-08-2008 01:49 AM

mikshaw Wrote:
I don't think it works? I tested it with murgaLua 0.6 and 0.5.5, and both exited.
They both work, as expected, when you run minimal_version("0.5.5")


Have you checked the murgaLua.version string? I don't know what binary you have, but mine says still "0.5.5".

Juergen


RE: testing presence of ML feature - mikshaw - 02-08-2008 04:35 AM

I've been doing various version checking tests on all versions from 0.1 to current (excluding 0.2, which I never had for some reason).
Both 0.5.5 and 0.6.PRE-RELEASE have a version string of "0.5.5". According to John there won't be a final 0.6 release, so as far as 0.6 is concerned its version string will be permanently wrong. This leads me to believe that testing for "0.6" as a minimum version string should be actively discouraged. It won't work properly if the script has features that were first adopted in 0.6, because it will assume your version is too old if you're using 0.6.

So my suggestion when it comes to using the version string as a test is either to retain 0.5.5 support in your script or skip to 0.6.5 when it is released. Otherwise you'll need to test for specific features in the required minimum version, which isn't quite as reliable unless you can be guaranteed that all features currently in 0.6 will remain in all future versions.

From what I gather, though, 0.6.5 looks to be quite an impressive update, with features I'll probably want to utilize often, so perhaps this issue will soon be moot. For more basic scripts 0.5.5 support should be sufficient, and for features that are new to 0.6 it looks like 0.6.5 will be a better alternative anyway.


RE: testing presence of ML feature - Juergen - 02-08-2008 04:51 AM

mikshaw Wrote:
I've been doing various version checking tests on all versions from 0.1 to current (excluding 0.2, which I never had for some reason).
Both 0.5.5 and 0.6.PRE-RELEASE have a version string of "0.5.5". According to John there won't be a final 0.6 release, so as far as 0.6 is concerned its version string will be permanently wrong. This leads me to believe that testing for "0.6" as a minimum version string should be actively discouraged. It won't work properly if the script has features that were first adopted in 0.6, because it will assume your version is too old if you're using 0.6.

So my suggestion when it comes to using the version string as a test is either to retain 0.5.5 support in your script or skip to 0.6.5 when it is released. Otherwise you'll need to test for specific features in the required minimum version, which isn't quite as reliable unless you can be guaranteed that all features currently in 0.6 will remain in all future versions.


I don't know, what you mean. The funtion tests if a certain version is higher than another version.
The function makes the following assumtions:
There version strings are of the form "x" or "x"."y" or "x"."y"."z" where the "." can be any character except a number (it will also match "0 5 5" or "5yy5" or "test5test5") and that x,y,z are not bigger than a 2 digit number.
If x_version<x_min_version then it will exit, If x_version=x_min_version and y_version<y_min_version it will exit. If x_versinon=x_min_version and y_version=y_min_version and z_version<z_min_version it will exit. In all other cases it won't.
So it doesn't matter how the strings are formed or what version it is.

Juergen


RE: testing presence of ML feature - mikshaw - 02-08-2008 05:50 AM

Ok, let me try to explain this...third time's a charm =o)

When murgaLua 0.6-PRE-RELEASE was posted here a few weeks ago, its version string still said "0.5.5" rather than "0.6". Not long afterward, Mr. Murga announced in the "full release" thread that a final 0.6 release would not be made since he was already jumping toward new features for 0.6.5. The "UPDATED murgaLua build" thread states that the version numbers have been updated, but I've downloaded the newer file twice (once just now), and the version string still says "0.5.5".

So, apparently the only 0.6 version in circulation is the one with the faulty version string, and I know of at least one Linux distro that has already included it in the most recent versions of both of its flavors. What that means is that 0.6 is out there, whether its listed on the murgaLua front page or not.

The result is that there are many people who currently have 0.6, and might continue to keep it for a while. If they try to run a script that has 0.6 features and that includes a test for the "0.6" string, the test will fail. If the test is for "0.5.5", the test will pass but the script will break because it has features not available in 0.5.5.

Anyway, I wasn't saying that your string test isn't useable...it's the best thing possible as far as I can tell. My issue was that developers should be aware that they shouldn't use "0.6" as a test. They should either test for individual 0.6 features, or retain support for 0.5.5, or wait until 0.6.5 and use "0.6.5" as the minimum string.


RE: testing presence of ML feature - Juergen - 02-08-2008 06:13 AM

There was a small bug in the previous version. This should fix it all:

Code:
function minimal_version(minimal_version)
  local x=string.gmatch((murgaLua and murgaLua.version or "0"),"%d+")
  local y=string.gmatch(minimal_version,"%d+")
  local t=function(x) return tonumber(x() or 0) end
  local version=0; local min_version=0 ; local a,b
  for i=1,3 do a,b=t(x),t(y);
    version=version*(a==0 and 1 or 100)+a
    min_version=min_version*(b==0 and 1 or 100)+b end
  if version<min_version then
    print("You must use a murgaLua version > "..minimal_version) os.exit(1) end
end

minimal_version("<>5test6.............")


Maybe this could be a little beautified.

Juergen


RE: testing presence of ML feature - JohnMurga - 02-08-2008 07:54 AM

mikshaw Wrote:
Anyway, I wasn't saying that your string test isn't useable...it's the best thing possible as far as I can tell. My issue was that developers should be aware that they shouldn't use "0.6" as a test. They should either test for individual 0.6 features, or retain support for 0.5.5, or wait until 0.6.5 and use "0.6.5" as the minimum string.

Or use the "murgaLua_Version" ... That IS correct in the pre-release.

The full release is probably 0.6.5, and my target for pushing it out is Sunday night (Switzerland).

I have dropped one of my target features, but added several others along with a few FLTK fixes and additions.

Cheers
John de Murga


RE: testing presence of ML feature - mikshaw - 02-08-2008 10:42 AM

Quote:
Or use the "murgaLua_Version"

ohhh...I didn't see that. So I suspect the test should include the existence of murgaLua_Version before checking its value, since it doesn't seem to be in earlier versions?
Thank you.