PLATFORM tests


#1

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


#2

Kaspar S. wrote:

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

#3

Robert K. wrote:

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


#4

Daniel B. 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.


#5

Quoting Peter H. removed_email_address@domain.invalid:

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


#6

On Tuesday 22 November 2005 18:12, Robert K. 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


#7

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

Matt M. 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


#8

removed_email_address@domain.invalid wrote:

Quoting Peter H. removed_email_address@domain.invalid:

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


#9

On Wed, 23 Nov 2005, Kaspar S. wrote:

Matt M. 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.


#10

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,
kaspar


#11

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


#12

Quoting K. removed_email_address@domain.invalid:

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

Duck platforming

BINGO.

-mental


#13

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


#14

removed_email_address@domain.invalid wrote:

Quoting K. removed_email_address@domain.invalid:

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

Duck platforming

BINGO.

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