How to properly detect the underlying OS inside of XBMC python?

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
bossanova808 Offline
Donor
Posts: 2,235
Joined: Sep 2009
Reputation: 34
Location: Melbourne, Australia
Post: #1
This should in principle be easy enough, but it's not working as expected for me.

Some background - I need to know which OS my addon is running on as it supplies and calls an external binary.

I need to distinguish OSX, Windows, and then Linux 32 and Linux 64.

I have tried both platform.platform() and platform.system() and they seem to work on Windows but raise an exception on OSX.

Has anyone got a good XBMC Ok way of doing this? I don't have a Mac very handy to try this out unfortunately.

ANY HELP MUCH APPRECIATED!!

My code is currently:

Code:
LOCALSQUEEZESLAVEVERSION = 'squeezeslave-1.2-311'
BINWIN    = xbmc.translatePath(os.path.join( BIN_PATH, LOCALSQUEEZESLAVEVERSION + "-win") + "//squeezeslave.exe")
BINOSX    = xbmc.translatePath(os.path.join( BIN_PATH, LOCALSQUEEZESLAVEVERSION + "-osx") + "//squeezeslave")
BINLIN32  = xbmc.translatePath(os.path.join( BIN_PATH, LOCALSQUEEZESLAVEVERSION + "-lnx26") + "//squeezeslave")
BINLIN64  = xbmc.translatePath(os.path.join( BIN_PATH, LOCALSQUEEZESLAVEVERSION + "-lnx26") + "//squeezeslave-i64")

#need to work out what system we're on
SYSTEM=""

try:
  #will return Windows or Darwin
  SYSTEM = platform.platform()
except:
  #otherwise we assume some linux 2.6+ flavour...
  SYSTEM = "Linux"

#32 or 64 bit?
is_64bits = sys.maxsize > 2**32

#choose the right executable
if SYSTEM.startswith("Windows"):
  EXE = [BINWIN]
elif SYSTEM.startswith("Darwin"):
  EXE = [BINOSX]
else:
  if is_64bits:
    EXE = [BINLIN64]
  else:
    EXE = [BINLIN32]

#chmod +X the exe file...
if SYSTEM=="Darwin" or SYSTEM=="Linux":
  try:
    #attempt to make the binary executable - this never works really...
    #it's really about triggering the messages in the except clause below...
    os.system("chmod a+x " + EXE[0])
    xbmc.log(__addonname__ + "-" + __version__ +": ### chmod +x the Squeezeslave binaries - success")
  except:
    xbmc.log(__addonname__ + __version__ +": chmod +x the Squeezeslave binaries - failure -> You must do this manually!!")
    #Logger.notify("Failed to chmod +x the binaries" "Please do so manually - most likely a user permissions error", 10000)

My XBMC stuff:
find quote
giftie Offline
Skilled Python Coder
Posts: 2,364
Joined: Mar 2010
Reputation: 54
Location: Calgary, Alberta
Post: #2
Have you tried
Code:
sys.platform

you probably will need to use a combination of sys.platform and the platform module.

[Image: e4f63e45ba34fe4695b3bb08eb2499d8e4ee484e...4c076g.jpg]
For troubleshooting and bug reporting please make sure you read this first you can also use XBMC Log Uploader Script.
Cinema Experience
Cinema Experience Wiki
cdART Manager
fanart.tv


find quote
bossanova808 Offline
Donor
Posts: 2,235
Joined: Sep 2009
Reputation: 34
Location: Melbourne, Australia
Post: #3
I am pretty sure I have actually. It's not having a Mac that makes this hard....but I think all the ones I have tried raise exceptions. Although maybe I have been confusing platform.platform and sys.platform....

My XBMC stuff:
find quote
analogue Offline
Skilled Python Coder
Posts: 149
Joined: May 2008
Reputation: 5
Post: #4
Most everything except for 32 bit vs 64 bit but you can narrow that down easily...

https://github.com/analogue/mythbox/blob...orm.py#L31
find quote
sphere Offline
Team-Kodi Member
Posts: 1,205
Joined: Jul 2009
Reputation: 50
Location: Germany
Post: #5
Or you can go with this code: https://github.com/dersphere/script.xbmc...gui.py#L68

No 32/64 bit detection in linux...

My GitHub. My Add-ons:
[Image: IOoywq0.jpg]
find quote
bossanova808 Offline
Donor
Posts: 2,235
Joined: Sep 2009
Reputation: 34
Location: Melbourne, Australia
Post: #6
Hey folks - thanks, those examples really helped and I think I've got it going on ok now. sys.platform is definitly the way to go, as platform.platform() raises exception (only on OSX)

Next issue is the subprocess call.... http://forum.xbmc.org/showthread.php?tid=123613

My XBMC stuff:
find quote
bossanova808 Offline
Donor
Posts: 2,235
Joined: Sep 2009
Reputation: 34
Location: Melbourne, Australia
Post: #7
Looks like my subprocess call may have been a permissions error (despite the wierd error message)...the call to os.system(chmod) - does NOT raise an exception on the Mac but apparently also does not do what it should

grrr. I hate platform stuff, such a pain to work on and test!

My XBMC stuff:
find quote