Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, May 29, 2012

ssdownloader

I recently purchased some electronic media from Simon & Schuster.

Apparently the technology guys at Simon & Schuster believe in "Java Over Functionality." Unlike most companies that sell DRM-free digital media, I was not given a link to download zip file containing the contents of my purchase.

Instead, I was given a JNLP file, which is nothing more than an XML file. A little Googling revealed that these files should be openable by javaws, a Java Web Starter application. After getting the file to successfully run, I was greeted with a custom download manager. This is great because why offer a simple download that will be managed with my browser's download manager when I can have some half-rate, custom download manager to fudge things up?

So the download manager asked me where I would like to save the file, downloaded each file individually for about twenty minutes, and then told me it was done. There was a big problem, my files where not in the location I specified. In fact, a hard drive search revealed they didn't exist anywhere on my machine.

I tried again, no dice.

So, if Mother Necessity if the driver of invention, then welcome her new baby, ssdownloader. I wrote this Python app up in a little over an hour. It was a good chance to strengthen my weak command of Python, and to actually get the stuff I paid for.

It simply parses the JNLP file to find the other XML file that defines where the actual downloads are. It then loops through that file, grabbing each file and saving it locally. If anyone out there gets frustrated trying to make good on their Simon & Schuster purchases, feel free to use this to make your life happy.

Monday, April 25, 2011

cx_Oracle on Ubuntu 11.04 Natty

I used the following to install cx_Oracle on Ubuntu 11.04 for use with Python 3.2, though they should work with any version of Python supported by cx_Oracle.  This will basically involve using alien to convert lots of rpms into debian package.  Go ahead and install alien as well as some other libraries we will need.

sudo apt-get install alien libaio1 python3.2 libpython3.2

Change the above to reflect the version of Python you want to use.

First we need the Oracle Instant Client software.  Go to http://www.oracle.com/technetwork/topics/linuxsoft-082809.html or http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html (for 64-bit) and download either the basic or basiclite rpm.  I did not try the basiclite client, but I think they should both work.  You can also grab sqlplus and whatever other packages you may need while working with Oracle, but only the basic package is needed for cx_Oracle to work.  Once downloaded convert it to a debian package.

sudo alien -d oracle-instantclient11.2-sqlplus-11.2.0.2.0.i386.rpm

Replace the above line with the appropriate version of the instant client that you downloaded.

Now install it.

sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.2.0-2_i386.deb

We need to create a configuration file pointing to the oracle library folder.

sudo vi /etc/ld.so.conf.d/oracle.conf

Depending on the version of the instant client installed you should have a folder like /usr/lib/oracle/11.2/client/lib.  This folder should be the first line to the oracle.conf file we create here.
Finally, for our Oracle client install, we need to define our ORACLE_HOME.  We need to create a new file.

sudo vi /etc/profile.d/oracle.sh

Add the following line, tweaking it as necessary to point to the version of Oracle you installed.
export ORACLE_HOME=/usr/lib/oracle/11.2/client
export LD_LIBRARY_PATH=$ORACLE_HOME/lib

On 64-bit installs then do the following line instead.
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib

Now that our client software is setup, we can install the python library to connect to Oracle.

Download cx_Oracle from http://cx-oracle.sourceforge.net/  I want the 11gR2 client for my x86 (32-bit) machine, and I want to use it with Python 3.2, so here I download the line labeled CentOS 5 i386 RPM (Oracle 11g, Python 3.2)  Make sure NOT to download the Unicode version unless you know for sure you need it.  Download the version for the version of Oracle and Python that you plan on using.  Usually, regardless of the version of Oracle you plan on using, downloading the latest version will work with any of the older versions of Oracle.

After downloading we use alien again to created a debian package.

sudo alien -d cx_Oracle-5.1-11g-py32-1.i386.rpm
sudo dpkg -i cx-oracle_5.1-2_i386.deb

The way our debian installs cx_Oracle isn't quite what Ubuntu needs, so run the following command to set things right.

cd /usr/lib/python3.2
sudo mv site-packages/cx_Oracle* dist-packages/
sudo rmdir site-packages/
sudo ln -s dist-packages site-packages

If the dist-packages folder does not already exist, you may need to run the following.

sudo mkdir dist-packages

Now you should be able to import the cx_Oracle module into Python and start creating connection.

david@fink:/usr/lib/python3.2$ python3.2
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle

Profit!