25 August 2012

Encrypting and Decrypting SQLDeveloper 3 Passwords

Some Oracle products are fairly sweet, let's be honest. One of them is the revamped SQLDeveloper, which has finally caught up (mostly) with MSSQL Management Studio.

One of its best features is the ability to import and export list of connections via XML (right-click on Connections to find the relevant menu). The resulting file is very readable, and hence easily manipulable. The only opaque item is the encrypted password, but it turns out that they are not particularly hardened. This is what you have to do to be able to manipulate them for fun and profit.

  1. Get Jython, or your favourite choice of JVM dialect that can work with jars. I picked Jython because 1) it's Python! and 2) Oracle ships it with most products, under oracle_common\util\jython.
  2. Load ojmisc.jar and db-ca.jar. These can be found in different places depending on your SQLDeveloper version.
  3. Import oracle.jdevimpl.db.adapter.DatabaseProviderHelper. That class has the two methods you need: goingOut (i.e. encrypt) and comingIn (i.e. decrypt).

So here's a complete Jython script for the Windows version of SQLDeveloper:

# set this to the path where you extracted SQLDeveloper
SQLDEV_ROOT = r'C:\sqldeveloper' 
# here's the real stuff
import sys
from os.path import join
sys.path.append(join(SQLDEV_ROOT,r'sqldeveloper\extensions\oracle.datamodeler\lib\ojmisc.jar'))
sys.path.append(join(SQLDEV_ROOT,r'sqldeveloper\modules\oracle.adf.model_11.1.1\db-ca.jar'))
from oracle.jdevimpl.db.adapter.DatabaseProviderHelper import goingOut as encrypt, comingIn as decrypt

if __name__=='__main__':
    print "Encrypted 'password': " + encrypt('password')
    print "Decrypted 'password': " + decrypt(encrypt('password'))

24 August 2012

Oracle XE 11.2.0.2 + Oracle Client 11.2.0.1 = cannot "connect / as sysdba"

While having fun with some complicated (and likely illegal!) scenarios I won't go into now, I ended up this morning with an Oracle XE database to which I wanted to connect with the usual DBA routine:

sqlplus /nolog
connect / as sysdba
I kept getting the dreadful "ORA-12560: TNS:protocol adapter error" and couldn't understand why. Surely I didn't break it that much? The service was still up and I could connect regularly in every way except that.

It turns out this is what happens when you try to do too many things on the same machine. I had installed an Oracle Client on top, because I needed a few things from there (OleDB provider etc) and because other products are guaranteed to work with that client rather than XE.

The problem is that the sqlplus version that comes with the Client is older (!) than the one from XE, but because of how the Windows PATH system variable gets manipulated by the installer, the old version takes over and gives you this problem.

Solution? In your PATH, make sure the folder from the oraclexe directory comes before the one from any client you might have there. However, understand that this is likely to break your Client, so either make sure the change is temporary, or set it locally only when you need it (batch files!).

19 August 2012

How to compile PyObjC for Python 3 on OSX 10.8 Mountain Lion

Another one for teh Google...

It so happens that I am curious about PyObjC, the Python bindings for Objective-C, which is the "native" language of choice for OSX/iOS.

As usual, my timing is completely wrong: recent versions of Xcode dropped support for PyObjC, and the project has shrunk to basically one person (that Ronald Oussoren I previously mentioned). The version on PyPI seems to work with Python 2.x only. Even the official page on Sourceforge is basically abandoned, and packages available from there are obsolete. This is a problem because I'm really trying hard to do everything with Python 3 these days, and the PyObjC version shipped with OSX 10.8 "Mountain Lion" is for 2.7 (the only Python version Apple ships and supports).

Luckily, from my past tribolations I knew that Ronald had his own repository on BitBucket, so I tried that and it worked fine. However, the documentation on how to build PyObjC from source is quite scarce (in fine geek tradition), and I had to figure out the following principles in the hard way:

  • Ronald's repository is split into many separate packages that have to be individually built. This is very fine-grained, but a bit cumbersome for the general case.
  • Do not use the setup.py script you'll find under /pyobjc . These are just for people pulling from PyPI, i.e. post-release.
  • /pyobjc-xcode is obsolete, and there's nothing to build there.
  • /pyobjc-framework-XgridFoundation simply refuses to build under ML. Xgrid is a somewhat obscure, proprietary Apple technology for highly-parallel computation. If you don't know what it is, chances are that you won't need it. I personally don't care about it.
  • /pyobjc-core is a requirement for all other packages, so it should be built first.
  • In order of importance, /pyobjc-framework-Cocoa, -Quartz and -CoreData are dependencies of other packages, so they should be built in this order before any other pyobjc-framework-*.
  • Python 3 support is occasionally shaky. In one occasion, one file had to be patched to remove unicode literals (the u'mystring' notation from Python 2 that was dropped in Python 3.0), but that's just a temporary snag: Python 3.3 will reintroduce that syntax as a compatibility hack for exactly this type of situation. I've submitted a patch anyway, but if you can't wait for Ronald to consider it, it is available in the below-mentioned repository.
  • Looking at BitBucket, I noticed there's at least one significant fork that is arguably targeting Python 3 more consistently. You might want to try that if Ronald's version is not good enough for you.

Because I don't plan to do this sort of work every day, I've put together a script so that I won't have to remember all this stuff when starting a new virtualenv environment. It's now available from my utils repo on BitBucket. There is no documentation but OMG IT'S FULL OF COMMENTS so there. As usual, any feedback is more than welcome.

13 August 2012

How to run py2app on OSX 10.8 Mountain Lion and Python 3.2

This post is for teh Google and all poor souls trying to use py2app on Mountain Lion.

To make it short, the latest official release of py2app does not work with ML and Python 3.2, you have to get the current development snapshot. Unfortunately, py2app requires a number of smaller libraries written by his developer, Ronald Oussoren, and most of them have to be upgraded as well (and before you curse his name: he's single-handedly maintaining py2app, pyObjC and virtualenv-mac; what have you done recently for the community?).

So here's my recipe:

  1. Clone all required repos.
    Oussoren uses Bitbucket, which is better accessed through Mercurial (hg); you can get hg from your favourite package manager (Homebrew/MacPorts/Fink/whatever).
    Then:
    hg clone https://bitbucket.org/ronaldoussoren/altgraph
    hg clone https://bitbucket.org/ronaldoussoren/macholib
    hg clone https://bitbucket.org/ronaldoussoren/py2app
    
  2. Install the packages. Since you're basically tracking trunk, you should probably use the develop mode of setuptools:
    cd altgraph && python setup.py develop && cd ..
    cd macholib && python setup.py develop && cd ..
    cd py2app && python setup.py develop && cd ..
    Note that this means you'll have to keep these "source" folders available forever. If you don't like that, you should create an egg (e.g. python setup.py bdist_egg), then install it (easy_install dist/your-resulting.egg).

    For the record, altgraph will present itself as version 0.10, macholib as 0.7, and py2app as 1.5.
  3. Now you should be able to run your python setup.py py2app

Bonus achievement: if you're using PyQt, this version of py2app will give you Retina-ready packages, by automatically adding the NSPrincipalClass key to the generated Info.plist and setting it to NSApplication. Nice one, Roland!

13 July 2012

MacBook Pro "Retina" Quick Review - Or How I'm Learning To Stop Worrying And Love The Mac

Today I finally picked up my new MacBook Pro "Retina" (2.6Ghz/16GB/512GB). I have to say that it's the first Mac I've ever truly wanted; I've used others in the past (the original "60s-tv" iMac, the white iBook, a few G5s and occasional MBP), but they were bought by other people for other people. So I'm not really a "Mac person": since 2002 I've mostly used Linux at home and Windows at work, and kinda abandoned Linux last year for Windows 7.

The first thing I noticed, and a big reason for the switch to The Land Of Steve, is how thin and light this MBP is. It's roughly half as thick as my Dell Latitude E6510 (which is powerful and packs a nice screen, but by God is it bulky), and probably about 35% lighter.

My second thought was about silence. I don't think I've ever owned a laptop this quiet, let alone one with an i7 CPU on board. You can't really appreciate it in a chaotic Apple Store, but when you're all alone at night, the lack of noise is incredibly refreshing. Note that I live in a quiet residential suburb; for any city-dweller this MBP can be considered completely silent.

Most things you've read elsewhere are also true: it boots faster than a phone, and you don't really need to actually shut it down unless forced by installs/updates; the screen is gorgeous (as long as you stick to updated apps like the Developer branch of Google Chrome, avoiding sucky ones like the official Twitter client) and it feels faster than any laptop I've ever used.

I have to say I couldn't notice any lag on my model until now, although I did feel it was a bit sluggish when I first tried one at the Store. I suspect the 2.3Ghz/8Gb configuration (which is what you get there, and what most reviewers have tried) is not enough to smoothly drive the über-screen; but I don't think I'll regret the choice not to shell out another £200 for the 2.7Ghz model, which comes with a larger cache (8Mb vs 6Mb).

And this is all that separates this particular Mac from other Macs out there. But what about differences between OSX and Windows? Obviously there are zillions of flame-threads on this subject, but these are my first thoughts as a switcher:

  • The damn keyboard layout. I must find a way to REMAP ALL THE THINGS!
  • No PgUp/PgDn/Del/PrtScrn (update: for PgUp/Dn use Fn-ArrowUp/ArrowDown). I'm wearing a black armband to mourn them right now.
  • Shortcuts are all fucked up. If you minimize a window and you want to bring it back, you have to Cmd-Tab back to the program, keep Cmd pressed, then press Alt before leaving Cmd. This is just Wrong. To maximize a window without going fullscreen, you have to invoke the "Zoom" feature, which often requires a custom shortcut. And so on and so forth. There is a steep learning curve for keyboard monkeys.
  • Trackpad gestures become necessary to survive. This might not be such a bad thing.
  • Any non-Apple peripheral will require an Internet connection to automatically download a half-decent, probably-uncustomizable driver. But, of course, why would you ever buy anything not Made By Steve?
  • Some DMG files, you launch them and they launch the app right there. Others will show a funny "drag app from this icon to that icon" screen. Others will open a window and expect you to know what to drag where. In comparison, Windows installers look admirably consistent.
  • Many developers (VmWare, Ascendo...) make you pay twice for the same product on a different platform. This is not nice. I should be able to deactivate a product on computer A and activate it on computer B with the same license. I'm still one person using one program.
  • gfxCardStatus, Little Snitch and iTerm2 are lovely. GPlus Tab and Facebook Tab suck.

More to follow in a few weeks; until Mountain Lion is officially out, I'm not going to deploy the full barrage of developer tools -- I'll probably format the disk anyway to install the new OS, so there's no point in wasting time now.