Author: xdissent March 15th 2009 1:41 pm
Keywords: HTML, OSX
uTidyLib is a Python wrapper for the HTML Tidy Library. It’s a pretty handy library and is dead simple to use, but unfortunately it does not compile on Leopard out of the box. I wrote a quick patch to fix it, and will maintain a vendor branch on GitHub since development seems to have been abandoned years ago.
The other day I wrote a tiny Django middleware that alerts me if a response contains markup errors (debug only of course). uTidyLib looked promising, but I couldn’t get pip to successfully install the damn thing. My deployment environment needs to be able to use pip so I had to patch setup.py to be setuptools friendly.
xdissent@stewart:~$ mkvirtualenv tidy_fix
New python executable in tidy_fix/bin/python
Installing setuptools............done.
(tidy_fix)xdissent@stewart:~$ easy_install pip
Searching for pip
Reading http://pypi.python.org/simple/pip/
[..]
Finished processing dependencies for pip
(tidy_fix)xdissent@stewart:~$ pip install http://download.berlios.de/utidylib/uTidylib-0.2.zip
Downloading/unpacking http://download.berlios.de/utidylib/uTidylib-0.2.zip
Downloading uTidylib-0.2.zip
Running setup.py egg_info for package from http://download.berlios.de/utidylib/uTidylib-0.2.zip
Installing collected packages: uTidylib
Running setup.py install for uTidylib
usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: -c --help [cmd1 cmd2 ...]
or: -c --help-commands
or: -c cmd --help
error: option --single-version-externally-managed not recognized
Complete output from command /Users/xdissent/.virtualenvs/tidy_fix/bin/python -c "import setuptools; __file__='/var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-1c9Slw-build/setup.py'; execfile('/var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-1c9Slw-build/setup.py')" install --single-version-externally-managed --record /var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-SEIOXm-record/install-record.txt --install-headers /var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/lib/include:
usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: -c --help [cmd1 cmd2 ...]
or: -c --help-commands
or: -c cmd --help
error: option --single-version-externally-managed not recognized
----------------------------------------
Command /Users/xdissent/.virtualenvs/tidy_fix/bin/python -c "import setuptools; __file__='/var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-1c9Slw-build/setup.py'; execfile('/var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-1c9Slw-build/setup.py')" install --single-version-externally-managed --record /var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-SEIOXm-record/install-record.txt --install-headers /var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/lib/include failed with error code 1
Storing complete log in ./pip-log.txt
The message error: option --single-version-externally-managed not recognized means setup.py is subclassing distutils.command.install.install, which sucks because we want to use setuptools. The fix is easy though: just change the import statement in setup.py to from setuptools.command.install import install. I got it all patched up and into my vendor SVN repository, but ran across another problem when trying to use pip.
(tidy_fix)xdissent@stewart:~$ pip install http://code.hartzogcreative.com/svn/vendor/utidylib/current
Downloading/unpacking http://code.hartzogcreative.com/svn/vendor/utidylib/current
Downloading current
Checking out svn repository http://code.hartzogcreative.com/svn/vendor/utidylib/current to /var/folders/LU/LUPONriBGYepwd3i9FaBB++++TI/-Tmp-/pip-mOglX5-build
Running setup.py egg_info for package from http://code.hartzogcreative.com/svn/vendor/utidylib/current
Installing collected packages: uTidylib
Running setup.py install for uTidylib
*** This library requires that you have two libraries ***
*** installed: ctypes and libtidy. ***
*** Please make sure they are installed correctly ***
*** before reporting a bug. ***
*** See: ***
*** http://starship.python.net/crew/theller/ctypes/ ***
*** and http://tidy.sourceforge.net ***
*** (or consult your vendor documentation for binary ***
*** packages.) ***
Successfully installed uTidylib
Things look normal here, but uTidyLib thinks differently.
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tidy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/xdissent/.virtualenvs/tidy_fix/lib/python2.5/site-packages/tidy/__init__.py", line 43, in <module>
from tidy.lib import parse, parseString
File "/Users/xdissent/.virtualenvs/tidy_fix/lib/python2.5/site-packages/tidy/lib.py", line 33, in <module>
raise OSError("Couldn't find libtidy, please make sure it is installed.")
OSError: Couldn't find libtidy, please make sure it is installed.
>>> ^D
uTidyLib looks for libtidy in a few predefined locations, and Leopard’s libtidy is not in one of those places. The author doesn’t take into consideration the possibility of a library having the extension dylib so I patched my vendor repository to correctly find libtidy. The source is available at http://github.com/xdissent/utidylib/.