Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PLATFORM tests

4 views
Skip to first unread message

Kaspar Schiess

unread,
Nov 22, 2005, 11:34:15 AM11/22/05
to
Dear list,

A common Ruby idiom seems to be something like
if PLATFORM =~ /mswin32/
# do windows stuff
end

Variations include matching for /mswin/.

Neither of those works on the mingw32 PLATFORM (i386-mingw32).

As a consequence of fixing this in a lot of libraries all the time I
would like to create a small library that permits
Platform.windows?
Platform.unix?
...
tests. To that end, I would need a complete collection of those PLATFORM
strings and how to classify them. I ask you all to send me:
a) output of PLATFORM on your .. well.. platform
b) a short description of that very same.

I will gather all of those emails and create said minimal library.
Comments on interface propositions are also welcome.

Thank you all in advance.
kaspar

---
code manufacture & ruby lab at http://www.tua.ch/ruby

Robert Klemme

unread,
Nov 22, 2005, 12:07:47 PM11/22/05
to
Kaspar Schiess wrote:
> Dear list,
>
> A common Ruby idiom seems to be something like
> if PLATFORM =~ /mswin32/
> # do windows stuff
> end
>
> Variations include matching for /mswin/.
>
> Neither of those works on the mingw32 PLATFORM (i386-mingw32).
>
> As a consequence of fixing this in a lot of libraries all the time I
> would like to create a small library that permits
> Platform.windows?
> Platform.unix?
> ...
> tests. To that end, I would need a complete collection of those
> PLATFORM strings and how to classify them. I ask you all to send me:
> a) output of PLATFORM on your .. well.. platform

18:05:35 [~]: ruby -e 'p PLATFORM, RUBY_VERSION'
"i386-cygwin"
"1.8.3"

Now, how do you classify that? Normally I'd say unix but there might be
application cases where it's more on the Windows side...

> b) a short description of that very same.
>
> I will gather all of those emails and create said minimal library.
> Comments on interface propositions are also welcome.

I think I remember having seen something like this. Maybe you check with
ruby-talk archive.

Kind regards

robert

Daniel Berger

unread,
Nov 22, 2005, 12:23:02 PM11/22/05
to
Robert Klemme wrote:
> Kaspar Schiess wrote:
>
>>Dear list,
>>
>>A common Ruby idiom seems to be something like
>> if PLATFORM =~ /mswin32/
>> # do windows stuff
>> end
>>
>>Variations include matching for /mswin/.
>>
>>Neither of those works on the mingw32 PLATFORM (i386-mingw32).
>>
>>As a consequence of fixing this in a lot of libraries all the time I
>>would like to create a small library that permits
>> Platform.windows?
>> Platform.unix?
>> ...
>>tests. To that end, I would need a complete collection of those
>>PLATFORM strings and how to classify them. I ask you all to send me:
>> a) output of PLATFORM on your .. well.. platform
>
>
> 18:05:35 [~]: ruby -e 'p PLATFORM, RUBY_VERSION'
> "i386-cygwin"
> "1.8.3"
>
> Now, how do you classify that?

cygwin != windows
mingw != windows

The whole pointof cygwin/mingw is to give you a Unix like environment on
Windows, including header files, etc.

The notion that PLATFORM.match("mswin") doesn't work for cygwin/ming strikes me
as odd, since you'll get the unixy behavior you (presumably) want if you're
running cygwin/mingw.

Regards,

Dan

Stefan Lang

unread,
Nov 22, 2005, 12:25:44 PM11/22/05
to
On Tuesday 22 November 2005 18:12, Robert Klemme wrote:
> 8:05:35 [~]: ruby -e 'p PLATFORM, RUBY_VERSION'
> "i386-cygwin"
> "1.8.3"
>
> Now, how do you classify that?  Normally I'd say unix but there
> might be application cases where it's more on the Windows side...

Perhaps:

Platform.unix? # => true
Platform.windows? # => false
Platform.cygwin? # => true

Kind regards,
Stefan


Peter Hickman

unread,
Nov 22, 2005, 12:34:18 PM11/22/05
to
Daniel Berger wrote:

> cygwin != windows
> mingw != windows
>
> The whole pointof cygwin/mingw is to give you a Unix like environment
> on Windows, including header files, etc.
>
> The notion that PLATFORM.match("mswin") doesn't work for cygwin/ming
> strikes me as odd, since you'll get the unixy behavior you
> (presumably) want if you're running cygwin/mingw.

Not really that odd, "a Unix like environment" and "unixy behaviour"
could mean anything. The windows command shell is "unix like" for some
value of /like/ that approaches zero.


men...@rydia.net

unread,
Nov 22, 2005, 12:47:14 PM11/22/05
to
Quoting Peter Hickman <pe...@semantico.com>:

> Not really that odd, "a Unix like environment" and "unixy
> behaviour" could mean anything. The windows command shell is "unix
> like" for some value of /like/ that approaches zero.

Precisely. Tests for specific platforms are almost worthless -- you
want to test for specific features or behaviors instead.

At best, attempts to infer particular properties of the runtime
environment from the platform name will be incomplete, and at worst
they will be outright wrong (e.g. due to new platform variations).

-mental


Damphyr

unread,
Nov 23, 2005, 4:02:56 AM11/23/05
to
men...@rydia.net wrote:
> Quoting Peter Hickman <pe...@semantico.com>:
>
>
>> Not really that odd, "a Unix like environment" and "unixy
>> behaviour" could mean anything. The windows command shell is "unix
>> like" for some value of /like/ that approaches zero.
>
>
> Precisely. Tests for specific platforms are almost worthless -- you
> want to test for specific features or behaviors instead.
OK, how do we test that win32ole and winapi are valid choices for cygwin
and mingw?
Up to which point can the application figure out things for itself and
when do we start writing code like

if (win32? && cygwin? && mingw?)
require 'win_optimized'
else
require 'properly_done'
end

I wouldn't mind a Plattform module or class, even if it is incomplete,
since most of the time the incopatability culprit is Windows (and I am
thinking mostly of fork and how I miss it) and the choices there are finite.
Cheers,
V.-

--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - δωρεάν υπηρεσία ηλεκτρονικού ταχυδρομείου.
http://www.freemail.gr - free email service for the Greek-speaking.


Kaspar Schiess

unread,
Nov 23, 2005, 4:16:18 AM11/23/05
to

> I think I remember having seen something like this. Maybe you check with
> ruby-talk archive.

Matt Mower proposed this:
http://matt.blogs.it/2005/06/29.html#a1882

This actually supports my case in that libraries should really be written
using this (or another) small lib. This should be standard, even. Matching
with Regexps just does not cut it.

And no, mingw is not like cygwin at all. Just for the record.

best regards,
kaspar

--

Luke Kanies

unread,
Nov 23, 2005, 10:59:14 AM11/23/05
to
On Wed, 23 Nov 2005, Kaspar Schiess wrote:

> Matt Mower proposed this:
> http://matt.blogs.it/2005/06/29.html#a1882
>
> This actually supports my case in that libraries should really be written
> using this (or another) small lib. This should be standard, even. Matching
> with Regexps just does not cut it.
>
> And no, mingw is not like cygwin at all. Just for the record.

I also have a relatively small library called 'facter' that is a bit more
generic, in that it can used to retrieve any set of facts that varies by
platform, release, or whatever. The initial and probably most important
facts are the platform and release (I develop software for sysadmins, so the
OS release matters quite a bit), but I've also got resolution mechanisms for
things like IP addresses, MAC addresses, and the domain name.

I wrote it because I was tired of having nasty switch statements based on
the output of 'uname -s', and then often repeating the same switch
statements in different programs.

You can find the library here:

http://reductivelabs.com/projects/facter/

It's pretty easy to use:

require 'facter'

os = Facter["operatingsystem"].value

It actually returns the fact object, and calling 'value' on it calls each of
its resolution mechanisms in turn until one returns a value. I should
probably short circuit that and just return the value itself, but, well, I
haven't, at this point.

You can also iterate across all of the known facts, in which case you get
the actual fact values:

Facter.each { |fact, value| puts "%s => %s" % [fact, value] }

It's very easy to add new resolution mechanisms, with arbitrary
restrictions. Here are the latest ones I've added:


# ps for most people
Facter["ps"].add { |obj|
obj.code = "echo 'ps -ef'"
}

# ps for darwin; note the tag
Facter["ps"].add { |obj|
obj.tag("operatingsystem","=","Darwin")
obj.code = "echo 'ps -auxwww'"
}

# how to get your name on linux
Facter["id"].add { |obj|
obj.tag("operatingsystem","=","Linux")
obj.code = "whoami"
}

You can add tag restrictions based on any other facts, and you can add as
many tags as you want. The tags are just a triad of an existing fact, an
operator, and the value. I basically just eval the three, so it's nothing
complicated I do here.

So, this is probably a bit more functionality than you need in this case,
but, well, it's out there, and if you're doing any sysadmin-style
development (I haven't seen many other people using Ruby for sysadmin work
yet), this could be a pretty useful library for you.

At this point it's only used in my Puppet project, I believe.

--
It is a mistake to think you can solve any major problems just with
potatoes. --Douglas Adams
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com

Kaspar Schiess

unread,
Nov 23, 2005, 12:46:33 PM11/23/05
to
Hello Luke,

Your library looks tremendously useful for a certain kind of work. I am
asking myself why you didn't put it into the RAA. Thank you for calling my
attention to your project.

I am not at all in sysadmin work currently, only that each time I install
something useful on my platform, it turns out to be .. well .. not useful
at all until I do a scan for 'mswin' in the source and replace that with
something else. Getting tired of that, hence the proposal.

I think your library can happily coexist with platform.rb, and I will be
looking at it to standardize (platform) nomenclature where possible. I
don't think it should replace my proposal though, since it is a bit
heavier, and the goal is to make usage threshold as small as possible.

best regards, thanks for your answer,

MenTaLguY

unread,
Nov 23, 2005, 9:17:14 PM11/23/05
to
On Wed, 2005-11-23 at 18:02 +0900, Damphyr wrote:
> OK, how do we test that win32ole and winapi are valid choices for cygwin
> and mingw?

If you're building Ruby, autoconf tests or similar.

If you're writing a Ruby application, do a require 'win32ole' or require
'winapi' within begin/rescue block. If the require fails, you fall back
on the alternatives.

-mental

signature.asc

Kero

unread,
Nov 24, 2005, 6:54:46 PM11/24/05
to
>> Not really that odd, "a Unix like environment" and "unixy
>> behaviour" could mean anything. The windows command shell is "unix
>> like" for some value of /like/ that approaches zero.
>
> Precisely. Tests for specific platforms are almost worthless -- you
> want to test for specific features or behaviors instead.

Duck platforming

men...@rydia.net

unread,
Nov 24, 2005, 9:24:57 PM11/24/05
to
Quoting Kero <ke...@chello.single-dot.nl>:

> > Precisely. Tests for specific platforms are almost worthless
> > -- you want to test for specific features or behaviors instead.
>
> Duck platforming

BINGO.

-mental


James Britt

unread,
Nov 24, 2005, 9:56:21 PM11/24/05
to

Which, for example, is a preferred way to do cross-browser JavaScript
coding, as simply inspecting a user-agent string is often misleading.

Better to to see if a critical function or property is defined, and does
The Right Thing, and gracefully fall back if missing.

See the things you learn using JavaScript?

James

--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools


0 new messages