4

I am working on a Python script that will rely on rosetta being installed. Rosetta is a dynamic binary translator for Mac OS X which allows many PowerPC applications to run on certain Intel-based Macintosh computers without modification. Is there anyway for to check the OS to see if rosetta is there?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Mizmor
  • 1,391
  • 6
  • 21
  • 43
  • as in rosetta stone (the learning languages thing?)? – Joran Beasley Jul 09 '12 at 18:13
  • 3
    nope. As in: Rosetta is a dynamic binary translator for Mac OS X which allows many PowerPC applications to run on certain Intel-based Macintosh computers without modification. (Pasted from wikipedia) – Mizmor Jul 09 '12 at 18:17
  • could you run "which rosetta" from python and check if you get anything back? Wouldn't that determine if it's installed? – Tyler Ferraro Jul 09 '12 at 18:19
  • Maria Zverina's solution is probably the best. The only alternative I can think of is that OS < 10.7 and CPU is Intel. Apple says to never ever check OS version, and instead use the appropriate ways to check for features, but if there is no documented way to check for this feature, is it better to rely on an implementation detail ("/usr/bin/rosetta") that could theoretically have changed (but never did before Rosetta was killed), or to check OS version? – abarnert Jul 09 '12 at 18:40

4 Answers4

4

Haven't got rosetta installed anymore but if I recall correctly it would give some kind of usage screen if you just type translate (rosetta command line). If so, something like this should work.

if os.system("/usr/libexec/oah/translate > /dev/null 2>&1"):
  print "Not installed"
else:
  print "Installed"
Maria Zverina
  • 10,863
  • 3
  • 44
  • 61
  • +1. Although you might want to use /usr/bin/translate (or whatever the appropriate path is), in case someone has a different thing called translate elsewhere on the path (e.g., from some Fink/MacPorts/Homebrew package). – abarnert Jul 09 '12 at 18:38
  • couldnt a user pick to install the package outside of its standard path location (as long as its still part of the $PATH)? (I dunno im not a mac person) – Joran Beasley Jul 09 '12 at 18:45
  • Actually, it looks like the tool is /usr/libexec/oah/translate, which is not on PATH by default. So this won't work as is; you have to specify the absolute path to it. And, @JoranBeasley: Generally, for packages that come as a built-in part of the OS, you don't get a choice of where to install them. 10.6 has a separate sub-installer that you can pull out and run manually, and you can even give it a root path if you do so with the command-line installer interface—but the tool ends up in the same place. – abarnert Jul 09 '12 at 19:22
  • That sounds right! :) Modified the solution above to reflect this. – Maria Zverina Jul 09 '12 at 19:23
  • 1
    I think os.path.exists('/usr/libexec/oah/translate') might be a slightly better solution. If it exists, you've got Rosetta. The only additional thing your version catches is people who have Rosetta but have done something horrible to break it. (I verified that a 10.5 machine upgraded to 10.7 deletes the /usr/libexec/oah and replaces it with a new one containing only RosettaNonGrata, which is what actually prints the "the PowerPC architecture is no longer supported" message. (Odd design there…) – abarnert Jul 09 '12 at 19:30
  • Awesome! Thank you for your help! Sorry that it took so long! – Mizmor Jul 17 '12 at 16:28
0

If you are really just trying to check whether something with a PPC dependency will likely run, you could do a loose check that the running CPU type is PPC or the running OS X version >= 10.4 and < 10.7 since those are the OS X versions where Rosetta is supported and, at least on 10.6, OS X will automatically prompt the user to install Rosetta when needed if it wasn't already installed. Note that the Darwin kernel versions are different from the OS X version number, i.e 10.4 -> Darwin 8, 10.5 -> 9, etc.:

>>> import os
>>> os.uname()
('Darwin', 'kitt.local', '11.4.0', 'Darwin Kernel Version 11.4.0: Mon Apr  9 19:32:15 PDT 2012; root:xnu-1699.26.8~1/RELEASE_X86_64', 'x86_64')
>>> un = os.uname()
>>> darwin_major_version = int(os.uname()[2].split('.')[0])
>>> cputype = un[4]
>>> can_run_ppc = cputype.startswith('ppc') or (darwin_major_version > 7 and darwin_major_version < 11)
>>> can_run_ppc
False
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • First, >4 is wrong, because 10.4 Intel comes with Rosetta, so you'd want >=4. But, more importantly, you don't need to check a lower bound, because all versions before that only run on PPC in the first place. As a minor note, Apple specifically recommends not parsing the uname to get the kernel version; platform.mac_ver() is a better way of getting this. – abarnert Jul 09 '12 at 19:12
  • Right, I had fixed the comment to be >= 10.4. Point taken about parsing the kernel string, though under the covers `platform.mac_ver` is kind of hacky itself. In any case, depending on the OP's use case, there also may need to be a test for actually running on OS X, like `sys.platform == 'darwin'. – Ned Deily Jul 09 '12 at 19:24
  • Yes, but it's much better to use mac_ver, part of the standard library, where the hacky stuff has been deployed and tested for years, than to try to reproduce it yourself. Also, >= 10.4 is still completely unnecessary, as I said. And finally, this doesn't handle the 10.6 optional Rosetta issue. – abarnert Jul 09 '12 at 19:26
  • Actually, at least in recent builds, mac_ver uses gestalt.gestalt() and/or /System/Library/CoreServices/SystemVersion.plist, and it even handles the 10.4 changes to gestalt. So it isn't "kind of hacky"; it uses the Apple-approved way to get the OS version. – abarnert Jul 09 '12 at 20:17
0

There is no official way to get this.

Rosetta works via a program called /usr/libexec/oah/translate. Officially, this is an implementation detail which is subject to change, and therefore shouldn't be relied on. However, we know that it never did change until 10.7, when Rosetta was killed completely, so it's safe despite the caveats. Maria Zverina's answer works for that (if you add the path), and it's probably the simplest. Or, maybe, just check for the presence of such a file instead of running it.

Alternatively, Rosetta came with Intel 10.4-10.6 (earlier versions of the OS were PPC-only and didn't have Intel). Again, officially you're never supposed to rely on OS version, instead using the appropriate APIs to check for features. But in this case, there don't seem to be any appropriate APIs, so maybe this is appropriate. Except for the caveat that you don't have to install Rosetta with 10.6, so this won't detect users who turned off the checkbox. If you want to do this:

import platform
release, versioninfo, machine = platform.mac_ver()
versionbits = [int(bit) for bit in release.split('.')]
rosetta = (versionbits < (10,7) and not machine.startswith('ppc'))

(Note that this is also "bad" because on some versions platform.mac_ver() does some hacky stuff that you're not supposed to do—the right way to get the OS X version bits is to call Gestalt. But mac_ver() is part of the standard library, so at least you can rely on it doing the hacky stuff as well as possible, and it being widely tested.)

If you're not actually after Rosetta, but whether you can run PPC either natively or via Rosetta, that's even simpler. All pre-10.7 versions that don't come with Rosetta are PPC; all 10.7+ versions can't run PPC period. So, just "release < '10.7'" does it. (Again, with the caveat that 10.6 can optionally skip Rosetta install.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Your test fails if there ever is an OS X 10.10 or later. Better to do the split and convert to integer as I suggested in my answer. – Ned Deily Jul 09 '12 at 19:26
  • @NedDeily: Good point; changed to do numeric comparisons. (I'm too used to using NSNumericSearch when comparing OS X versions, because I'm usually doing it in Cocoa/PyObjC/etc.) – abarnert Jul 09 '12 at 20:11
0

Try to run:

brew config

Rosetta 2: true

enter image description here

Quockhanh Pham
  • 384
  • 2
  • 11