Display file permissions in octal

stat -f '%A %N' <file>

%A: display permissions in octal

%N: display file name

Paste Mode: Pasting text (and indenting it) in Vim

To paste text into vim, use paste mode to turn off the auto indentation:

  • Enable paste mode with “:set paste”  
  • Disable paste mode “:set nopaste” 

Replacing text in files from the command line

sed "s/old-word/new-word/g" org.txt > new.txt 

Javascript CI: JsLint recipe

Prerequisite: 

Download jslint4java from https://code.google.com/p/jslint4java/ and extract the archive

To lint your code:

find . -iname "*.js" | xargs java -jar jslint4java.jar

What this command does is it

  1. uses the find command for collecting JS files recursively starting in the current directory:
    find . -iname "*.js"
  2. pipes the resulting list of js files to xargs which will then run jslint4java on those files:
     | xargs java -jar jslint4java.jar
  3. For CI you will also need to generate an xml report. This is easily done by using the —report xml flag:
    java -jar jslint4java.jar --report xml > jslint.xml

In Hudson / Jenkins

  1. Add a new build step:
    find . -iname "*.js" | xargs java -jar jslint4java-2.0.2/jslint4java-2.0.2.jar --report xml > jslint.xml
  2. Visualize the report with help of the Violations plugin

Additional notes:

  1. Use find -print0 together with xargs -0 if you have any directory names containing spaces:
    find . -iname "*.js" -print0 | xargs -0 java -jar jslint4java.jar
  2. Use true or echo “lint completed” to prevent lint errors from breaking your build; it seems that otherwise Hudson / Jenkins will stop at that build step and not even create the violations report.

use hg strip for removing revisions

hg strip <rev>

removes a revisions together with all its descendants. To use hg strip you need to activate the MqExtension by adding

[extensions]
mq =

to the hgrc file of your repo:

vim path/to/repo/.hg/hgrc

Oh My Zsh: .zshrc template location

The template file is found here:

 ~/.oh-my-zsh/templates

After installation of Oh My Zsh make sure to copy the template zshrc.zsh-template to the home directory and rename it to .zshrc, e.g. like suggested in the README:

cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

To start using your new zsh config, either open a new terminal or source the .zshrc file:

source ~/.zshrc

Open files with sublime from command line

Add

alias sublime='open -a "Sublime Text 2"'

to your .zshrc:

chobit% vim ~/.zshrc

Reload your .zshrc:

chobit% source ~/.zshrc

Done

How to include retweets into tumblr’s recent tweet section

As I retweets more than write tweets of my own, I had some problems getting my Twitter timeline to show up in tumblr.

In tumblr recent tweets are fetched with a script called tweets.js which calls https://twitter.com/statuses/user_timeline/:screen_name.json?callback=recent_tweets&count=5.

Unfortunately that API call does not return retweets but counts them in anyway. Which means that if your last original tweet is more than 5 tweets away, the API call returns an empty list.

In the Twitter API documentation I found an alternative API which accepts a parameter include_rts for controlling whether or not retweets should be included in the response: https://dev.twitter.com/docs/api/1/get/statuses/user_timeline

To use that API instead I made two modifications to my tumblr page (Customize theme > Edit HTML):

    1. I removed the import of tweets.js and
    2. created a custom script block for fetching my tweets:
<script type="text/javascript">
        var script = document.createElement('script');
        script.type="text/javascript";
        script.src = "https://api.twitter.com/1/statuses/user_timeline.json?include_rts=1&screen_name={TwitterUsername}&count=5&callback=recent_tweets";
        document.getElementsByTagName('head')[0].appendChild(script);
</script>

I am still using the original callback function  (recent_tweets) even if I made some minor modifications, like writing out a default text if the API should ever return an empty list again.

The complete API call:

https://api.twitter.com/1/statuses/user_timeline.json?include_rts=1&screen_name={TwitterUsername}&count=5&callback=recent_tweets

Before:

{block:Twitter}
    <div id="twitter" style="display:none;">
        <h3><a href="http://twitter.com/{TwitterUsername}">Latest Tweets</a></h3>
        <div id="tweets"></div>
    </div>

    <script type="text/javascript">
        function recent_tweets(data) {
            for (i=0; i<data.length; i++) {
                document.getElementById("tweets").innerHTML =
                    document.getElementById("tweets").innerHTML +
                    '<a href="http://twitter.com/{TwitterUsername}/status/' +
                    (data[i].id_str ? data[i].id_str : data[i].id) +
                    '"><div class="content">' + data[i].text +
                    '</div></a>';
            }
            document.getElementById("twitter").style.display = 'block';
        }
    </script>
{/block:Twitter}

<!-- Put this at the bottom of the page -->
{block:Twitter}
    <script type="text/javascript" src="/tweets.js"></script>
{/block:Twitter}

After:

<script type="text/javascript">
    function recent_tweets(data) {
        var tweets = document.getElementById("tweets");
            if (data.length !== 0) {
                for (i=0; i<data.length; i++) {
    		    tweets.innerHTML = 
    		    tweets.innerHTML + '<a href="http://twitter.com/{TwitterUsername}/status/' + (data[i].id_st r ? data[i].id_str : data[i].id) + '"><div class="content">' + data[i].text + '</div></a>';
    		}
        }
        else {
            tweets.innerHTML = "Keep calm and carry on. There's nothing to see here."
         }
	 document.getElementById("twitter").style.display = 'block';
    } 
    var script = document.createElement('script'); 
    script.type="text/javascript";
    script.src = "https://api.twitter.com/1/statuses/user_timeline.json?include_rts=1&screen_name={TwitterUsername}&count=5&callback=recent_tweets";
    document.getElementsByTagName('head')[0].appendChild(script); 
</script>

Getting Started with Python on Heroku/Cedar

Tutorial on how to create and deploy a simple Python web app using Flask, virtualenv and the Heroku Toolbelt

Mercurial: Getting all heads on a certain branch

hg log -r "branch('changeset|branchname') and head()"

Replace <changeset> with a changeset number from the branch you’re interested in.

High level acceptance testing in your PHP applications using Python, Lettuce and Selenium

How to acceptance test web apps with a combination of selenium and lettuce

Note: When working with selenium 2.0

  • replace
    from selenium import get_driver
    with
    from selenium import webdriver
  • replace
    from selenium import FIREFOX
    and
    world.browser = get_driver(FIREFOX)
    with
    world.browser = webdriver.Firefox()

Lettuce introduction

How to get started with Lettuce, a BDD tool for Python based on Cucumber

Installing Selenium

melanie@mipu:~$ sudo pip install selenium 
melanie@mipu:~$ python 
Python 2.7.2+ (default, Oct 4 2011, 20:03:08) 
[GCC 4.6.1] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from selenium import webdriver 
>>> driver = webdriver.Firefox() 
>>> driver.get("http://www.svtplay.se") 
>>> assert "SVT Play" in driver.title

Installing pymetrics

melanie@mipu:~$ sudo apt-get install pymetrics
melanie@mipu:~/pyProject$ find -iname "*.py" | xargs pymetrics -q --nosql
melanie@mipu:~/pyProject$ ls
metricData.csv  src  tests