Forum: Ruby PLATFORM tests

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6e32b16ec35070a346dd4e08799589e5?d=identicon&s=25 eule (Guest)
on 2005-11-22 17:38
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-11-22 18:14
(Received via mailing list)
Kaspar Schiess 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
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel.Berger (Guest)
on 2005-11-22 18:26
(Received via mailing list)
Robert Klemme 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
2d532341317628fbb2cb22ec427a1d62?d=identicon&s=25 langstefan (Guest)
on 2005-11-22 18:26
(Received via mailing list)
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
6d9bf78ca49a017e9e3e6b0357b6c59e?d=identicon&s=25 peter (Guest)
on 2005-11-22 18:34
(Received via mailing list)
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.
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-11-22 18:51
(Received via mailing list)
Quoting Peter Hickman <peter@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
Cfdeff3ac35010e4de8f85d954f24f4a?d=identicon&s=25 damphyr (Guest)
on 2005-11-23 10:05
(Received via mailing list)
mental@rydia.net wrote:
> Quoting Peter Hickman <peter@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
6e32b16ec35070a346dd4e08799589e5?d=identicon&s=25 eule (Guest)
on 2005-11-23 10:17
(Received via mailing list)
> 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
Ea6905beef63a84a75793f4f72cb8ff7?d=identicon&s=25 luke (Guest)
on 2005-11-23 17:02
(Received via mailing list)
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.
6e32b16ec35070a346dd4e08799589e5?d=identicon&s=25 eule (Guest)
on 2005-11-23 18:47
(Received via mailing list)
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
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-11-24 03:19
(Received via mailing list)
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
Dd54c22454b4e3c21cadf3bdb5192e28?d=identicon&s=25 kero (Guest)
on 2005-11-25 00:58
(Received via mailing list)
>> 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
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-11-25 03:27
(Received via mailing list)
Quoting Kero <kero@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
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 james_b (Guest)
on 2005-11-25 04:00
(Received via mailing list)
mental@rydia.net wrote:
> Quoting Kero <kero@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.

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
This topic is locked and can not be replied to.