Django "Stack" Install with ActivePython on Win/Mac

A current project involves Django, PostgreSQL, and git; three softwares I was pretty much unfamiliar with as a LAMP developer. After some poking around and initial love affair with Python and the idea of Django...well, Django is super-sweet on *nix OSs like Ubuntu, but not so fun to install and use on Windows and the "unix-like" Darwin-based Mac OS. So here's the start of my install guide for Windows. It can easily be applied to Mac since ActivePython is available for Mac as well.

So, the requirements: Python is easy to install; Django is easy to install; MySQL is generally pretty easy to install; the adapters that allow Python to talk to MySQL? Not so much. And what if you want to manage all those python modules you install? Or have virtual environments that match your target deployment environment? Enter ActivePython. ActivePython is essentially a distribution of Python and a number of other programs for managing python and python modules/package. I highly recommend you start with that:

Install ActivePython: http://www.activestate.com/activepython/

I just installed to C:\Python (/Python on Mac).

Now, the creators of the code I'm working with for this project use an application called "pip" (bundled with ActivePython) that allows you to install a list of dependancies from a standard requirements file. You can "install" packages, svn repositories, git repositories, and more. This is easily one of the coolest ways to distribute code I've ever seen. I fully expect this to be a standard development method in the next few years. Anyway, to import these files into pip require require you to have a command line version of git and subversion, so:

Install Git for windows

Options for the install:
- Install git as cmd line tool (not in bash shell)
- Checkout as-is, commit as Unix-style endings

Install Git for OSX

Install Subversion Command line client (CollabNet Subversion Command-Line Client v1.6.11 (for Windows))

On OSX you should have svn already and you shouldn't need to worry too much about line endings. Feel free to upgrade to the latest and greatest.

Now we have what we need to get going: from ActivePython; virtualenv, pip, and pypm; svn; and git.

Make sure you can run both. Open up a cmd prompt or Terminal on OSX:

$ git --version
git version 1.7.0.2.mysysgit.0

$ svn --version
svn, version 1.6.11

Ok, we're good to go. Switch to your drive root or wherever you want to create your virtual environments:

$ mkdir virtualenvs
$ cd virtualenvs

# create a virtual enviroment called "myenv"
$ virtualenv myenv

# OR, if you want the env to not inherit your globally installed python site packages
# which you probably want since installing mysql and postgre plugins in a virtual
# environment is tough since you have to compile them
$ virtualenv --no-site-packages myenv

# activate the environment (windows):
$ myenv\Scripts\activate.bat
(myenv)D:\virtualenvs>

# activate the environment (mac):
# note that scripts are created in "bin" instead of "Scripts"
$ source myenv/bin/activate
(myenv)virtualenvs $

Now running pip will use the pip created when the virtual environment was created, installing packages only in that working environment. So you can have different environments with different packages installed.

Let's install Django as a test first.

# latest
(myenv)virtualenvs $ pip install Django

# or install a specific version
(myenv)virtualenvs $ pip install Django==1.1
Downloading/unpacking Django==1.1
  Downloading Django-1.1.tar.gz (5.6Mb): 5.6Mb downloaded
  Running setup.py egg_info for package Django
Installing collected packages: Django
  Running setup.py install for Django
Successfully installed Django

Next step in my install process was getting the codebase from the client. They used github for version control (replacing client name with dummy copy):

# clone the git repository
(myenv)virtualenvs $ cd D:\Projects\Client\
(myenv)virtualenvs $ git clone git@github.com:client/repository.git

The client's repo had a requirments formatted file that I can then use to install with pip.

# back in our virtual environment, now install the requirements
# the -E flag tells pip which environment to install to.
(myenv)virtualenvs $ pip install -E myenv -r D:\Projects\Client\repository\requirements.txt

This is neat because pip's requirements format allows svn, git, and pypm (pythom package manager) hints in this file. It'll automatically pull in the dependancies using the method specified. Here's an example:

Django==1.1
-e svn+http://django-mptt.googlecode.com/svn/trunk@121#egg=django_mptt-0.3_pre-py2.5-dev
django-flatcontent==0.1.2
-e git://github.com/bartTC/django-frontendadmin.git@53d8ed1fdcd1ef466fb9c8b38ccb2abb77978b1e#egg=django_frontendadmin-0.5-py2.6-dev
django-markitup==0.5.2
django-haystack==1.0.1-final
Whoosh==0.3.9
textile

So we're installing Django 1.1, copying down an svn repo from googlecode, cloning a git repo from github and installing a number of python mods using PyPm. Pretty badass for one command!

If we were on unix or mac (with xcode installed) we could also use pip to compile the mysqldb module, but we have some binary install options on windows. This will install it for all python environments. Not sure how to use this to install to a specific virtual environment: Download MySQL-python-1.2.2.win32-py2.6.exe (1,022.8 KiB). Run the installer and select your ActivePython install when prompted. To test if it works, open cmd prompt and type

$ python
>>> import MySQLdb 

If there's no errors; hey, it worked. On MacOS, it's not quite as fun. I used Macports to download the MySQL5 headers, then I did the following:

# only download the package
(myenv)virtualenvs $ pip install --no-install MySQL-python

Then open the downloaded package and edit site.cfg and uncomment line 13 and add the path to your mysql_config (for me this ended up pointing to the bin folder and mysql_config I downloaded with Macports). After than you can run:

python setup.py build
python setup.py install

There's also a Windows port of the psycopg2 module (PostgreSQL for Python). We happen to be using this for the client project. You'll need to install Postgre 8.x first. Then install this guy. Not going to go into Postgre installation and whatnot since I only know a bit of the basics myself. It was enough to get my Django project up and running:

cd /Projects/Client/repository/web
python manage.py syncdb
pyhton manage.py runserver

Command line is fun. Now, getting WSGI to work with MAMP will be the real challenge.

May 14th, 2010 | Permalink

PHPUnit Follow-up: Code Coverage with Xdebug on Mac

It took me considerable amount of time to figure out the correct file to edit in order to have Xdebug work from the command line with PHPUnit's --report flag. This flag generates HTML files that look something like the following image:

code coverage of ASRA 2.0 with Xdebug

This displays the number of lines that have been run in the process of unit testing; and in a lovely readable format (see Xdebug's normal format to see how much more useful this is). To get PHPUnit and Xdebug working, start by following the tutorial in the previous article. Then to get Xdebug running, check out this post by Felx Geisendörfer. Follow steps 1 + 2. If you are using regular-old MAMP, 3 + 4 should get you going just fine. The problem I had was I am using MAMP Pro, so I was editing my "template" .ini file thinking that was where the lines needed to go. Xdebug was loaded as a browser module, but was not loaded when I ran the coverage report in PHPUnit. So, in Pro, you have to add the config options to /Applications/MAMP/conf/php5. This is the ini file that the CLI version of PHP uses.

Here are the lines I added to get it working. Note that you may also need to disable the ZendExtensionManager and the zend_optimizer extension for it to work..in MAMP just uncheck the box. Check the compatibility section on the install page for more.

[Zend]
;zend_optimizer.optimization_level=15
;zend_extension_manager.optimizer=/Applications/MAMP/bin/php5/zend/lib/Optimizer-3.3.3
;zend_optimizer.version=3.3.3
;zend_extension=/Applications/MAMP/bin/php5/zend/lib/ZendExtensionManager.so

[xdebug]
zend_extension=/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20050922/xdebug.so
xdebug.remote_enable = 1

If you completed the other article, PHPUnit should be using MAMP's php, which should now have Xdebug rocking and you can do something like:

# 'report' is the name of the folder the html will output to
$ phpunit --report report My_Test 
November 15th, 2009 | Permalink

Installing and Using PHPUnit with MAMP

Unit testing is becoming more and more popular in every day web development. I decided to see what all the fuss was about. So here's a quick tutorial on getting PHPUnit up and running with MAMP on OSX.

First you need to grab a PHPUnit release. You need a command line utility called pear-phpunit to run tests and also the PHP files that make up the framework. I recommend first doing an svn export somewhere to get the command line script and then doing another export to just export the framework library.

MAMP's default include path is /Applications/MAMP/bin/php5/lib/php and the Pear installation is there already so that's where you should install PHPUnit. So, let's start by grabbing the PHPUnit Framework:

$ cd /Applications/MAMP/bin/php5/lib/php
$ svn export svn://svn.phpunit.de/phpunit/phpunit/branches/release/3.3/PHPUnit
# ...
# Exported revision 4387.

You can use their Trac Browser to decide which release you want to work with. If you ever want to upgrade, you can just delete this folder and export a different release.

In the resulting folder, open PHPUnit/Util/Fileloader.php and do a find and replace for @php_bin@ with the path to your PHP command-line interpreter which for php5 in MAMP should be /Applications/MAMP/bin/php5/bin/php.

Next export or download the command line script:

svn export svn://svn.phpunit.de/phpunit/phpunit/branches/release/3.3/pear-phpunit

Open pear-phpuint and replace @php_bin@ with /Applications/MAMP/bin/php5/bin/php (same as the earlier file).

Next, copy the pear-phpunit executable to a location that exists in your unix PATH environment variable. My PATH already included a /usr/local/bin and I had installed a different version of Pear there earlier so I went ahead and moved it there (you may have to create a directory or put it in another directory in your path). At this point you should rename the file to "phpunit" as well. You can accomplish this via command line or (as I like to sometimes do) go to Finder > Go > Go To Folder... and type in /usr. This will allow you to use the OS GUI to copy the file over and rename it (providing you enter your password). Now you have to make the file executable to be able to run it:

sudo chmod +x /usr/local/bin/phpunit

So at this point you should have:

  • a) The PHPUnit framework in /Applications/MAMP/bin/php5/lib/php/PHPUnit
  • b) The phpunit executable moved, renamed and chmod'ed in /usr/local/bin/phpunit

If everything worked correctly, you should be able to type "phpunit" anywhere and see the "useage" information. I used the following link to set this up, so refer to that if you have any other troubles.

Here's my first couple tests for testing the Json export class in the in-progress Asra 2.0. Though I would not see myself doing this for "normal" websites, I can see how useful it is in application development. I already found a missing required class and a possible bug in just creating this simple test case.

<?php

/**
* test setup ~ /tests/Tests.php
*/
require_once dirname(__FILE__) . '/../../../Tests.php';

/**
* @see Asra.Export
*/
require_once 'Asra/Export/Data.php';
require_once 'Asra/Export/Format/Json.php';

/**
* @see PHPUnit_Framework
*/
require_once 'PHPUnit/Framework.php';

class Asra_Export_Format_JsonTest extends PHPUnit_Framework_TestCase
{
    /**
     * assert that export returns json and is valid json
     */
    public function testExportData()
    {
        $array = array("hello, world!", "hello, again!");
        $data = new Asra_Export_Data($array);
        $format = new Asra_Export_Format_Json($data);
        $this->assertNotNull($format->export());
        $this->assertNotNull(json_decode($format->export()));
    }

    /**
     * assert that passing a non-array to format throws an exception
     */
    public function testDataNotArray()
    {
        //$this->setExpectedException('Asra_Export_Exception');
        $data = "This is not an array!";
        try
        {
            $format = new Asra_Export_Format_Json($data);
        }
        catch (Asra_Export_Exception $e)
        {
            return;
        }
        $this->fail("An expected exception has not been raised.");
    }
}
July 20th, 2009 | Permalink

Silverlight Development on Mac OSX: My First "App"

I'll be honest. I don't really want to learn Silverlight all that badly. I cannot get excited about technology that forces me to develop on a specific platform. So, I found it pretty exciting to hear recently that there's quite a modest effort being put forth to creating tools to facilitate cross-platform development.

Enter the Silverlight Dynamic Languages SDK. The SDL is a bridge between Silverlight and the Dynamic Language Runtime of .NET (which includes IronRuby, IronPython and others). What this means is you can develop Silverlight apps in Ruby or other languages without needing all that zany Visual Studio/C# stuff and Microsoft Technology.

What follows is my first foray into Silverlight development on a Mac - a quick sample that plays a video. To recap before I start...it kinda sucks :P There's no development tools - cool, I can use TextMate, but creating XAML by hand would definitely suck. Maybe this is where Expression Studio (seems to be available for Mac) is really going to be necessary.

The other huge detriment to developing with SDL/IronRuby is the fact that documentation is extremely limited (please let me know if you know where I can find the IronRuby/Python Silverlight documentation!). I managed to get by by looking at some samples and at the C# MSDN documentation for Silverlight. For example, it took me awhile (not being a Ruby master) to figure out how to change the source of my MediaElement:

XAML (easy):

<MediaElement source="test.wmv"/>

C# (doesn't seem too bad...from the MSDN)

mediaElement = new MediaElement();
mediaElement.Source = new Uri("test.wmv", UriKind.Relative);

IronRuby (looks a lot like the C#)

media = MediaElement.new
media.source = Uri.new "test.wmv", UriKind.Relative

I first tried setting source = "text.wmv" and it failed because it's expecting a Uri object. One thing that has been difficult is the difference between setting properties in XAML and in the code-behind. For example, the Margin parameter:

<MediaElement source="test.wmv" Margin="5"/>

Sweet, that's easy.

media.margin = Thickness.new 5

Hmm, not so easy. Objects are nice though ;) Guess I'm just used to the Flex style where the parameter names are the same (case-wise) and value wise in both MXML and Actionscript.

Silverlight on a Mac

Step 1: Install Silverlight
It's currently at release Silverlight 2 RTW (2.0.31005.0). Download that sucker and install it.

Step 2: Download SDL SDK
Next you need the dynamic language SDK. This allows you to write apps in IronRuby and IronPython. I downloaded sdlsdk-0.4.0 (Everything) and unpacked it in /Applications/Silverlight/sdl-sdk.

Step 3: Mono
Mono is an open-source version of .NET (sweeeet). You'll need it to use all those .dlls in the SDK. I installed the Mono.framework 2.0.1_1 package for Mac OS.

Step 4: Create the skeleton
The SDL SDK includes tools for creating applications. All you need to do open Terminal and:

# go to where you unpacked the sdl sdk
cd /Applications/Silverlight/sdl-sdk
# create a "rubylight" app called "SilverlightApp"
script/sl ruby SilverlightApp
# cd into the new dir
cd SilverlightApp

Now if you open that folder with your favorite Text Editor you should see this structure:

To start the development server, run the following when you're inside SilverlightApp. The /b option starts the server with the current directory as root and then opens it in the browser (http://localhost:2060)

# start the chiron server through mono
mono /Applications/Silverlight/sdl-sdk/bin/Chiron.exe /b

You might want to add aliases to your bash_profile so you can quickly type these paths in the future:

# ~/.bash_profile
# silverlight aliases

alias chiron='mono /Applications/Silverlight/sdl-sdk/bin/Chiron.exe'
alias sl='/Applications/Silverlight/sdl-sdk/script/sl'
alias slserver='/Applications/Silverlight/sdl-sdk/script/server'

Once mono is running Chiron, you can go to the development link and you should see:

Click on "index.html" to see the app (not sure how to add indexes to chiron yet :P)

Just to see how the app is made, open up SilverlightApp/ruby/app.rb and change the the string in the following line:

message.text = "Welcome to Ruby and Silverlight!"

Now, refresh the development server and you should see the different text string. All this is doing is initializing the TextBlock control in SilverlightApp/ruby/app.xaml with the x:Name of 'message' to a string.

That's about it. You're now developing Silverlight on a Mac with Ruby. Let me know if you kick ass with this and want to give me help ;) or if you find any good "RubyLight" docs.

To deploy the app to a server, you need to compile your "ruby" folder into a .xap (a fancy .zip) folder. You can use chiron again to do this:

# change to the ruby dir of SilverlightApp
cd ruby
# running with /z flag creates a XAP file
# with dynamic language dependancies
chiron /z:ruby.xap

Now when you FTP it your app, you move up ruby.xap instead of the ruby folder

Sample

Here's the silly thing I made while setting this up and playing with it. It's a button that loads a wmv into a MediaElement with a play/pause button.

/tests/silverlight/ruby

And here's the source:

/tests/silverlight/ruby/app.xap

You can check it out by renaming the .xap to .zip and unpacking it (xap is just a zip afterall).

Overall

I think what's lacking in Silverlight currently is high-end design. Sure .NET guys can jump right into it, but Microsoft will never get to the level of Flash without designers getting involved. Plus, I think that more "design-centric" developers use Mac and Linux these days. Sure, you can do much of what Flash does with this technology but it's moot if it looks like shit because some Microsoft Certified guy made it who can't design a button.

I'm excited to try Expression Studio. If it is as great for designing XAML as they purport, then ES combined with the Dynamic Language SDK could be the way to go for Mac. I just hope that Microsoft doesn't forget about the "non-proprietary" community on this.

Sources

November 9th, 2008 | Permalink