Thursday, August 25, 2011

how to check a file digest on OSX


openssl sha1 /path/to/my/file | grep -c "the_digest_to_check_against"
sould print "1".

Wednesday, August 10, 2011

OpenCV with Python on WSGI on CentOS with Plesk10

Well it all comes together rather nicely...
My old-ish saying that "If I can make it work on OSX I'll make it work anywhere!" proved true again!
In the past I managed to make Python+WSGI+OpenCV work on Snow Leopard with MacPorts. You can find the articles related to that on this very blog. It was quite a ride.
But the final goal was to get it working on a bad ass CentOS hosting server we run at the company. The problem was it came with Plesk 10. Now I won't go into a rant about Plesk. Suffice to say it's 10 because the update of 9 to 10 went horribly wrong and ended with a server move :-D
Luckily the guys at Rackspace I'm such a big fan of made the IUS Repos available for the very purpose of providing "A better way to upgrade RHEL". And that it is.

I did not update PHP using IUS because amazingly Parallels came up with a way to do that and now you can have 5.3 rather easy and not break Plesk. But Python 2.6 on CentOS is a different story.
Suffice to say that following the IUS articles I manage to have Python 2.6 and mod_wsgi quickly and painlessly running on the box.

The big problem was compiling OpenCV2 and after yumming all the required packages like python26-numpy, python26-distribute, a compiler, some image libs and make as per the OpenCV docs, the magical command was:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=YES -D PYTHON_EXECUTABLE=/usr/bin/python26 -D CMAKE_MAKE_PROGRAM=/usr/bin/make ..
After that I had to copy the cv.so and cv2.so into the python26 site-packages because the install put them into the dist-packages folder. Then I yummed python-setuptools and used easy_install-26 to install Werkzeug. Jaaaa!

The only remained glitch was the mod_wsgi setup with Plesk.
Plesk comes with mod_python and it acts like that's a major achievement. I simply commented everything in /etc/httpd/conf.d/python.conf and added a vhost.conf file in the mydomain.com/conf Plesk domain folder. In there I pasted the basic WSGI setup including explicitly specifying the user I want the process to run under and that is the user you defined in Plesk at domain creation and who owns that folder.
The setup looks something like this:

WSGIDaemonProcess mydomain.com home=/var/www/vhosts/mydomain.com/httpdocs user=YOUR_DEFINED_PLESK_USER processes=2 threads=5
WSGIScriptAlias / "/var/www/vhosts/mydomain.com/httpdocs/application.wsgi"

<Directory "/var/www/vhosts/mydomain.com/httpdocs">
	WSGIProcessGroup mydomain.com
	WSGIApplicationGroup %{GLOBAL}
	Order deny,allow
	Allow from all
</Directory>

After nailing the Plesk extra config file with a
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain mydomain.com
all was up and running.

And it sure is great when that happens ;-)

Facebook Page Tab Canvas IFrame Application with CakePHP

Ahhh, the joys of Facebook development!

I remember back in 2007 when they launched _THE_ Platform. Man was it a weird thing. At the time I worked on a Flash prototype but quickly gave up. If only I would've persevered maybe Farmville would have a different owner :D
Last year I tried again. FBML, page scraping, localhost dev with port forwarding, lakes of tears...

Now it's take 3.
It definitely looks easier.
Especially after this year's shift to IFrame apps. If you dont want to look under the hood you don't. If you want to or have to, the only two not-so-easy parts are dealing with callbacks and OAuth2.0.
And as always when it comes to PHP it's time to put CakePHP to work.

But I have to admit that the Cake did it to me again...
The Session component clears the existing session when the Session.start is true in config. And it was.
So the Facebook user id was there only on IFrame load and it was deleted immediately after by the SessionComponent. Setting Session.start to false fixed it but I don't want to do that.
The solution that works is ditching the facebook.php and extending FacebookBase into a CakePHP Facebook Component. Biggest downside is the Security.level set to low so that CakeSession doesn't insist on a session.referer_check which breaks the FB flow.
The whole thing is server side only. This is mainly because at the time of the coding the PHP SDK docus clearly yelled that it is not compatible with the the to-be-updated client side JS SDK.
But it works great. Unltil they change something on FB. Btw, did anyone see the Test users tab in the Roles section that I used a while ago? Ah, there it is! Back again after a week in the countryside :-D

In the code you can also find some hints at the "Publishing to Page feed from Facebook Application" problem.
The issue was grabbing an offline auth_token for the Facebook page so that my application can make posts to the Page wall at any time. This is achieved by having a FB permission section in the admin where the admin who also is an admin of the FB page can initiate the access_token request. The component's updatePageAccessToken method is called and that redirects the user to the FB login and then App permissions pages so that after a successful flow Facebook calls back the UsersController admin_facebookPermissions method that checks the component getPageAccessToken() and stores it if it's there.

Although the component is not a Swiss Army knife for Cake Facebook development I think it's worth sharing. It's not a plugin so not as plug and play as some may desire but it can be used without major hassles.

Questions are as always welcomed.