Search This Blog

Monday, November 28, 2011

No module named PIL - after buildout

If you are using buildout to install PIL and if you get this error, check once in the eggs directory. If there is a PIL egg directory, check its contents. If there are files directly, that is the reason for the error. You can manually move all those contents to PIL subdirectory or add the following buildout part to your buildout script

# ============================================================================================== #
# PIL Hack #
# When buildout install PIL egg, it keeps all files under the PIL-1.1.7-py2.6-X-X-universal.egg. #
# We need to put all the files under a subdirectory PIL inside this folder to make it work. #
# ============================================================================================== #
>>> import os
>>> while True:
>>> lst = []
>>> for p in os.listdir('${buildout:eggs-directory}'):
>>> if p.startswith('PIL-${versions:PIL}') and p.endswith('.egg'):
>>> lst.append(p)
>>> if len(lst) > 1:
>>> print '\033[91m' + 'I am confused. Multiple PIL eggs found. %s.' % lst + '\033[0m'
>>> raw_input('Delete the one which is not required and then press Enter to continue...')
>>> elif len(lst) == 0:
>>> print '\033[91m' + 'No PIL egg found in eggs directory' + '\033[0m'
>>> raw_input('Press Enter to continue...')
>>> break
>>> elif len(lst) == 1:
>>> path = os.path.join('${buildout:eggs-directory}', lst[0])
>>> path2 = os.path.join(path, 'PIL')
>>> if not os.path.exists(path2):
>>> print '\033[92m' + ('Using PIL egg: %s' % lst[0]) + '\033[0m'
>>> lstdir = os.listdir(path)
>>> os.mkdir(path2)
>>> for p in lstdir:
>>> os.rename(os.path.join(path, p), os.path.join(path2, p))
>>> break

Python colors on command line print

While writing a command line script, adding colors might ease understanding the output. To print a line in color just follow this simple technique
print '\033[91m' + 'my error string' + '\033[0m'
You might have guessed it by now. '\033[' is a control char and '0m'/ '91m' define the color to be displayed.
Few examples: 0m is white 91m red 92m green 93m orange 94m blue Hope this helps

Monday, July 11, 2011

Python httplib2 certificate verify failed

If you are trying to make a https connection using httplib2.Http to a server which uses self-signed certificate, you might face "httplib2.SSLHandshakeError: [Errno 1] _ssl.c:480: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed".

There are 2 solutions:
1. httplib2.Http(disable_ssl_certificate_validation=True).request('')

2. httplib2 uses its own certificate store. Usually, the location would be /usr/local/lib/python2.7/dist-packages/httplib2/httplib2/cacerts.txt.
Edit this file to add the certificate of your server and you should be good to go.

Steps to download site certificates (eg. certificates from DigiCert):

  1. Open the site
  2. Click on the lock icon in the address bar.

    1. Chrome:
      1. Go to "Connection" tab
      2. Click on "Certificate Information"
    2. Firefox:
      1. Click "More Information"
      2. Go to "Security" tab
      3. Click "View certificate"
      4. Go to "Details" tab
  3. Make a note of the first node of certificate (eg "DigiCert High Assurance EV Root CA")
  4. Go to DigiCert certificates page (
  5. Search for the certificate with the name you found above and download it.
  6. Convert the .crt file to .pem "openssl x509 -in DigiCertHighAssuranceEVRootCA.crt -out DigiCertHighAssuranceEVRootCA.pem -outform PEM"
  7. If above steps returns error, refer to For this certificate you need to pass "-inform der"
  8. Open the DigiCertHighAssuranceEVRootCA.pem, copy the contents and paste in cacerts.txt

Thursday, June 30, 2011

Install sun java jdk on Ubuntu 10.10 Maverick Meerkat


Sun Java has finally been uploaded to the Ubuntu 10.10 Maverick Meerkat official Partner repository.

From UI:
To install (Sun) Java in Ubuntu 10.10 Maverick Meerkat you'll have to enable the Partner repository: open Ubuntu Software Center and go to Edit > Software Sources, then on the "Other Software" enable the Partner repository (it should be the first on the list; it looks like this: " maverick partner".).

Then, click "Close" and when asked, click "Reload" and finally install Sun Java6 in Ubuntu 10.10 by searching for it in Ubuntu Software Center.

From command line:
sudo vim /etc/apt/sources.list
deb maverick partner
deb-src maverick partner
sudo apt-get update
sudo apt-get install sun-java6-jdk

sudo apt-get install sun-java6-plugin
sudo update-java-alternatives -s java-6-sun
You also need to setup JAVA_HOME and PATH variable. Open your $HOME/.bash_profile or /etc/profile (system wide) configuration. Open your .bash_profile file:
$ vi $HOME/.bash_profile

Append following line:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin