rulururu

post Determine OS with ruby

June 1st, 2009

Filed under: Ruby — Kai @ 6:30 pm

I’m developing a tiny application that should run on (almost) any operating system. Though to that I sometimes have to do some switches (e.g. for console coloring).
The constant RUBY_PLATFORM helps me, so that I wrote a small module

module OsHelper
 
  def is_linux?
     RUBY_PLATFORM.downcase.include?("linux")
  end
 
  def is_windows?
     RUBY_PLATFORM.downcase.include?("mswin")
  end
 
  def is_mac?
    RUBY_PLATFORM.downcase.include?("darwin")
  end
 
end

Unfortunately I quickly realized that it’s more a bad than a good idea because RUBY_PLATFORM will return 'java' when using JRuby for example.

I found sys-uname library that gives much more information than that constant can do. (gem install sys-uname)

Finally you can use it like that:

begin # use Sys::Uname library if present
        require 'sys/uname'
        @@os_name = Sys::Uname.sysname
        @@architecture = Sys::Uname.machine
        @@os_version = Sys::Uname.release
      rescue # otherwise use shell
        @@os_name = `uname -s`.strip
        @@architecture = `uname -p`.strip
        @@os_version = `uname -r`.strip
      end

I hope it someday will help someone ;)

post Are you redundant or backed up?

February 2nd, 2009

Filed under: Security — Kai @ 11:21 pm

So, for the reason that quite a while ago my external HD crashed; I do not think that that might be of value or interest to people reading my blog; I have to tell you something. While it turns out I didn’t lose any data, thanks to my backup stategy I improve from time to time.

I bet anyway some of you probably don’t have a backup solution for your machine. Don’t tell me I didn’t warn you when your HD crashes. And it will crash, it’s just a matter of time!

Some years ago, after a very time-consuming loss of data, I wanted to solve this new problem of backing up data in the best possible way, so I started researching all this stuff that I never really paid attention to before. I started looking into external HDs, NAS boxes (because it would be cool to stream data to my home network in addition to providing storage for my PC), RAID, and everything in-between. But it probably took a weekend of research into these things before I realized the the simple yet so very important distinction between data redundancy and data backup.

As you probably know there are different RAID-Levels most of them provide besides an improvment of read/write speed a data security.

Redundancy is something you get e.g. by having two or more HDs in a RAID1 or RAID5 configuration. If one HD fails, you can recover your data from the other HDs, either due to mirroring or due to having a parity disk that will allow your data to be recreated. However, this is not a backup!

If your whole PC is fried, or whatever it might be, your data is irreparably lost. Backup is something that should protect you from data loss even in the case of a severe hardware failure.

My solution is very simple provided by rsync that copies data from one pc to another (via ssh). Very simple - but just as effective & safe.

When thinking about backup solutions I got this obviously overstated idea: If my backup files are in, let’s say Hong Kong while I’m in Nuremberg I’ll be able to recover my files even if Nuremberg falls into the ocean from a earthquake (assuming I survive the ordeal).
Okay, you’re right - I’m loosing track of reality, of course that’s not necesarry at all. Just a few years ago this would have been inconceivable for private purpose but nowadays it’s not such a bad idea… ;-)

Nevertheless I continue in copying data to a machine that’s just a few arm length from my workstation.

post Bravery or just stupidity?

December 29th, 2008

Filed under: Uncategorized — Kai @ 6:26 pm

No comment.

Bravery or Stupidity?

post Python Trick: Check for Substring

December 26th, 2008

Filed under: Python — Kai @ 7:36 pm

Today I’d like to show you a quick hint that might be obvious, but it took me quite some time of Python programming to figure it out.

You probably know that you can test if a list, tuple, or dict contains an item by testing the expression “item in list” or “item not in list”. I never realized that this would work for strings as well.

I was always writing code like:

string = 'Hi there' # True example
string = 'Good bye' # False example
if string.find('Hi') != -1:
   print 'Success!'

It’s kinda ugly code but it didn’t mind at all since I found otu that it is completely equivalent to do “if substring in string”:

string = 'Hi there' # True example
string = 'Good bye' # False example
if 'Hi' in string:
   print 'Success!'

Much cleaner and simpler. Might be obvious to 99% of the population, but I wish I’d known about it sooner.

post Why I hate Christmas that much

December 23rd, 2008

Filed under: Uncategorized — Kai @ 1:51 pm

Last year on 21st I expressed my contempt for Christmas briefly. With the hope that I don’t recur myself I’d like to write some lines also this year.

The lack of understanding really feels bad when outing yourself to be kinda “christmas-hater”, it’s elusive but true ;-)

I hate the way it makes people behave. I hate the shallow, fake little advertisements I see every damn place I go that tell me how by going into endless debt to buy shit for my family and friends I’ll somehow be a better person. I hate the lines in the stores, I hate how the decorations go up mid-October under the guise of “Holiday Spirit”. And I hate the people that buy into that sort of thing. They want your MONEY, you fools!

While we’re on the subject, I really hate how people can love Christmas and go all out. I hate how everyone remembers poor people and homeless people for two weeks and then promptly forgets about them again.

I’m in no mood for Christmas - I’m sick, sick, sick, of all the superficial cutsie little raindeer, snowmen, Santa Clauses, jingle bells, etc. Oh, excuse me just a moment while I wretch. I mean, driving by some houses looks like the light monster walked by and threw up all over the place. A very few are done well and are attractive but the vast majority are gaudy and asthetically unbalanced.

I vehemently resent all the expectations and demands placed on me at Christmas. I’m not poor but I’m not rich either, and I have bills to pay. There is an endless list of things I am expected to spend my money on - gifts for everyone, tips for all my service providers, food, Christmas Cards, interior and exterior decorations, lights, etc, etc, etc. It is financially irresponsible to buy gifts for everybody at the same time.

Instead of enjoying a few days of holiday everybody uses up an unbelievable amount of time and money on Christmas stuff. To me it’s just overwhelming crass commercialization and a waste of my time and money.

Fear

P.S.: Hey kids, Santa was the invention of Coca-Cola’s 1930s advertising campaign… HE’S NOT REAL!!!

post VBS-Battle: command line for output

December 10th, 2008

Filed under: General Programming, Windows — Kai @ 2:04 pm

From time to time I have battles with Visual Basic Script, which I usually avoid using.

I wanted console output because the process I’m starting should be unattended, rather than clicking through a bunch of MsgBoxes.

Typically, one prints in VBscript using a Wscript.echo("Hello, world!") line or some variant thereof. If you do not invoke with a cscript hello-world.vbs, you get a GUI/pop-up MsgBox, which I wish to avoid. I just wanted something to go right to the command line for output, without putting cscript at the beginning.
Wscript.StdOut.WriteLine dies, of course, if not also invoked with a cscript.

For different reasons I didn’t want to permanently set my default scripting host to cscript, either.

Probably the best solution to solve that problem is a simple stub in the script that will be called when it starts, which detects how the script was started, and re-starts it explicitly using cscript.exe if needed. It makes use of the wscript.fullname property (which is the path to the running host executable, either c:\windows\system32\cscript.exe or wscript.exe). If the script is running as wscript.exe, it will simply re-launch the script using cscript.exe and exit.
This way, if the local machine has wscript as the default host, it will immediately launch, detect that it was launched via wscript, and re-launch itself using wshell.run as a cscript. The local host doesn’t need to be reconfigured for this to work.

'this is at the start of your script
CheckStartMode
 
' This is somewhere else in your script
Sub CheckStartMode
     ' Returns the running executable as upper case from the last \ symbol
     strStartExe = UCase( Mid( wscript.fullname, instrRev(wscript.fullname, "\") + 1 ) )
 
     If Not strStartExe = "CSCRIPT.EXE" Then
          ' This wasn't launched with cscript.exe, so relaunch using cscript.exe explicitly!
          ' wscript.scriptfullname is the full path to the actual script
 
          set           oSh = CreateObject("wscript.shell")
          oSh.Run "cscript.exe """ & wscript.scriptfullname & """"
          wscript.quit
 
     End If
End Sub

The only disadvantage of that solution is that it closes the console window it opens as soon as the script is finished. I went through all of the options for intWindowStyle in the Run method of the WshShell object and none of them kept the spawned console open for more than a flash. Maybe there’s a hack for it, too.

ruldrurd
« Previous PageNext Page »
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)