PLATFORM tests

K

Kaspar Schiess

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
 
R

Robert Klemme

Kaspar said:
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
 
D

Daniel Berger

Robert said:
Kaspar said:
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
 
S

Stefan Lang

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

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

Perhaps:

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

Kind regards,
Stefan
 
P

Peter Hickman

Daniel said:
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.
 
K

Kaspar Schiess

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
 
L

Luke Kanies

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.
 
K

Kaspar Schiess

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top