Friday, August 17, 2012

monkeyrunner: detecting the OS

Sometimes you monkeyrunner script should know the Operating System it is running on. In the big majority of the cases you don't have to worry if you are running on Linux or Mac OS X, but things are not so smooth on Windows.

I'll give you an example. I've received some bug reports about AndroidViewClient not being able to find adb. AndroidViewClient tries to be clever and not to invoke adb if it's going to fail because it's not found or it's not executable. To determine this, it is using:


        if not os.access(adb, os.X_OK):
            raise Exception('adb="%s" is not executable' % adb)

the trick here is that for Windows platforms adb should include the trailing .exe.
Then the problem is to determine the OS the script is running on.

There are several ways of determining the OS in python and jython. Let's see what are the results using monkeyrunner

Command Linux Mac OS X Windows
os.getenv('os') None None Windows_NT
os.name java java java
platform.system() Java Java Java
sys.platform java1.6.0_26 java1.6.0_33 java1.7.0_05
java.lang.System.getProperty('os.name') Linux Mac OS X Windows XP

From the previous table we can determine that the best way of obtaining the OS from a monkeyrunner script is

     java.lang.System.getProperty('os.name')

I hope this helps you
Post a Comment