Using Fabric to Deploy CaltrainJS

I wrote earlier about different options to deploy applications using Python, and one of the simplest tools is Fabric. While Fabric might not be enough for my work projects, it worked great on my Caltrain schedules application.

My project is really simple, but once I started looking at running Javascript compression and checking tools I realized I needed to add some automation to the process. I considered three options: Makefiles, SCons and Fabric. In the end I chose Fabric because I didn’t need to do much in the build steps, but I needed a quick and painless way to upload my changes to a staging site for tests. I think Fabric handles the last step best.

My build step is mostly copying files around. I have two Javascript files, one of which is produced by CaltrainPy. The format is pretty compressed already, so I just run sed on it to shorten AM to A, PM to P and take out – from certain fields. Previously I was doing this in the online application, but it makes sense to do this in the build step. I run the ShrinkSafe Javascript compression tool from the dojo project for the second file and combine the two scripts into a single file. I find it somewhat odd that a Javascript compression tool uses Java, but whatever works… The last mildly interesting part is the generation of a sitemap XML file, for which I just wrote a simple Python function. In the Fabric build step this becomes:

def build():
    "Build locally"
    # schedule.js is already compact, just minor tweak
    local("""sed -e 's/ AM"/ A"/g' -e 's/ PM"/ P"/g' -e 's/"-"/""/g' schedule.js >../stage/caltrain.js""")
    # our main script has a lot to compress
    local('java -jar custom_rhino.jar -w -fatal-warnings -c caltrain.js >>../stage/caltrain.js')
    sitemap()

Deploying to staging is even simpler:

def stage():
    "Deploy into staging environment on server"
    put('../stage/index.html', 'staging/path/on/server/index.html')
   ...

The one gotcha I run into with staging is that current version of Fabric does not seem to support wildcards. I first tried to put('*.html', '/path/on/server/') but that wouldn’t work. It seems like such a basic feature that I wouldn’t be surprised if it will be included soon.

For CSS compression I just used cleancss manually. I also looked at cssoptimizer but it was a bit too extreme to my taste. cleancss is based on csstidy so I could possibly integrate that into my build.

For validation I have used W3C validators, but it would be nice to be able to integrate validators in the build step as well. For Javascript it seems JSLint would be a good option. I haven’t spent any time thinking about HTML and CSS validation during the build step, but obviously this would be a nice thing to have as well.

Similar Posts:

    None Found