Thursday, June 26, 2008

Show Tables equivalent in Postgresql

The equivalent of mysql 'show tables' command in postgresql:

\dt



To see more information about the table, do \dt with the table name:

\dt table_name



If you want to see more than just tables (eg. sequences), use:

\d



To see all databases, like 'show databases' in mysql:

select datname from pg_database;

Wednesday, June 25, 2008

Printing Syntax Highlighted Source Code

Sometimes, reading code from paper can be easier than reading it from the screen. However, if you printed it straight with the 'lpr' command, you'll lose all your syntax highlighting. There is a way of solving this problem by using GNU 'enscript'.

For example, if you wanted to print syntax highlighted ruby code:


enscript --color=1 -Eruby your_source_code.rb


The '-E' flag tells enscript that the code is ruby, while '-color' is self-explanatory. If you wanted to find out what other syntax highlightings are availabe, use this command:


enscript --help-highlight


However enscript is more than just that. For example, if I wanted it with line numbering, landscaped and in two-columned format (thus saving space), I can do this:


enscript --color=1 -Eruby -c2 -C1 -r -j your_source_code.rb


where:
-c2: 2 columns
-C1: start line numbering from 1 onwards
-r: print in landscape
-j: print border around the columns

It can even generate syntax highlighted code in html, which makes it useful when you want to blog about source code:


enscript --color=1 -w html -Eruby your_source_code.rb


Its 'man page' describes it as "convert text files to PostScript, HTML, RTF, ANSI, and overstrikes" probably doesn't do it much justice, given that it's capable of doing much more; my examples have barely skimmed its surface, so I highly recommend you read more into it to find out enscript's full capabilities.
Saturday, June 14, 2008

Installing OpenGrok On Ubuntu Linux

I am really impressed with OpenGrok, a web-based source code search engine that I've found while I was trying to look up OpenJDK's source code. It is pretty cool as OpenGrok allows you to point your browser into an exact line of source code in your respository, allowing citations directly for discussion using hyperlinks, rather than cutting and pasting chunks of code. I find useful for annotating code, like when I'm using a wiki in conjunction with it to document design considerations for the source.

I'm only supplementing OpenGrok documentation because there were some parts of it which were less clear, taking longer than I expected to get it running. Hopefully, these instructions will help you cut down your setup time.

The assumption is that you're installing from a bare-bones Ubuntu system, and all commands here assume that you are root, which if you're like me, coming from a Gentoo background and sick of typing sudo all the time, you stay rooted all the time by using:


sudo -i


The next thing to do is to get all the relevant software via aptitude. I'll be using Apache Tomcat as my application server:


aptitude install sun-java6-jdk tomcat5.5 exuberant-ctags


Before we set up OpenGrok, we need to create the directory structure to store the files. For the sake of brevity, I'll use the same directory structure from OpenGrok's EXAMPLE.txt:


/opengrok
|-- bin
|-- data
`-- source


Download the tar.gz archive from its website, unpack and extract it. Copy the OpenGrok binaries into the /opengrok/bin:


# cp -r run.sh opengrok.jar lib /opengrok/bin


Edit run.sh and setup up the following parameters:


SRC_ROOT=/opengrok/source
DATA_ROOT=/opengrok/data
EXUB_CTAGS=/usr/bin/ctags


Note that I have put in the default location for the installed ctags for Ubuntu, you may have different locations/application names depending on your Linux distro. You'll then have to configure the web application. Go to the directory where you've downloaded your files, and unzip source.war to make modifications:


# mkdir source
# cd source
# unzip /path/to/opengrok-release/source.war


And make changes into WEB-INF/web.xml. The completed changes look like this:


<context-param>
<param-name>DATA_ROOT</param-name>
<param-value>/opengrok/data</param-value>
<description>REQUIRED: Full path of the directory where data files generated by OpenGrok are stored</description>
</context-param>

<context-param>
<param-name>SRC_ROOT</param-name>
<param-value>/opengrok/src</param-value>
<description>REQUIRED: Full path to source tree</description>
</context-param>

<context-param>
<param-name>SCAN_REPOS</param-name>
<param-value>false</param-value>
<description>Set this variable to true if you would like the web application to scan for external repositories (Mercurial)</description>
</context-param>


The text coloured red are the parts where you need to make modifications. The tags in blue indicate where the XML has originally been commented out by <!-- and --> you'll have to take them away.

Once that's done, you'll have to rezip the .war file back in place, and put it into Tomcat's webapps directory:


# zip -r source.war ./
# mv source.war /usr/share/tomcat5.5/webapps


After which, we'll need to configure our source code for OpenGrok to use, and set it up:


# cd /opengrok/source
# cp -r /your/source/code/ .
# java -Xmx1524m -jar opengrok.jar -W /opengrok/configuration.xml -P -S -v -s /opengrok/source -d /opengrok/data


This will generate the list of indices that allows OpenGrok to cross reference your source code. With that done, the final task is to setup Tomcat so that it has correct privileges. Append the following lines to /etc/tomcat5.5/04webapps.policy:


grant codeBase "file:${catalina.home}/webapps/source/-" {
permission java.security.AllPermission;
};

grant codeBase "file:${catalina.home}/webapps/source/WEB-INF/lib/-" {
permission java.security.AllPermission;
};


I'm just being cavalier here by giving full security access to OpenGrok, which should be safe. But I only doing it given because my application is firewalled from the outside world, so do make your own security adjustments appropriately! Once that's done, restart Tomcat:


# /etc/init.d/tomcat5.5 restart


You should now have a functioning own OpenGrok respository to play with! However if you get an error with the stack trace showing Apache Lucene not being able to create a file, grant full permissions to the data directory:


# chmod -R 777 /opengrok/data/


 
Monday, June 02, 2008

The Four-Letter Word of Independence

For a long time I've been rather vague in describing my ultimate goal - the attainment of freedom - only because the definition of freedom itself is rather broad and I had found it difficult to frame it in my own way. But I've actually found the version of 'freedom' that matches closely to what I'm thinking of from Nassim Taleb's book 'The Black Swan' (I'm retyping from his book, so there might be inaccuracies from me. Also the emphasis in bold is mine):

... It was hard to tell my friends, all hurt in some manner by the crash[1], about this feeling of vindication. Bonuses at the time were a fraction of what they are today, but if my employer, First Boston, and the financial system survived until year-end, I would get the equivalent of a fellowship. This is sometimes called "f*** you money," which, in spite of its coarseness, means that it allows you to act like a Victorian gentleman, free from slavery. It is a psychological buffer: the capital is not so large as to make you spoiled-rich, but large enough to give you the freedom to choose a new occupation without excessive consideration of the financial rewards. It shields you from prostituting your mind and frees you from outside authority--any outside authority. (Independence is person-specific: I have always been taken aback at the high number of people in whom an astonishingly high income led to additional sycophancy as they became more dependent on their clients and employers and more addicted to making even more money.) While not substantial by some standards, it literally cured me of all financial ambition--it made me feel ashamed whenever I diverted time away from the study for the pursuit of material wealth. Note that the designation f*** you corresponds to the exhilarating ability to pronounce that compact phrase before hanging up the phone. ...

Yes, I'm looking for that same psychological buffer to ease me from having to worry about the vicissitudes of everyday life, and to attain the ultimate goal of being the master of my own fate, rather than a serf of others. I'm not sure if this applies to any of you out there, but the very quality of independence is one that I see as keystone - nothing else ranks higher (except good health maybe), given most other things in life can be subsequently attained once you have the freedom to seek them out.

And like how Taleb describes the rich, I do not understand why are there people out there who, even after having an obscene amount of money, will still need to prostate themselves in the never-ending quest to seek out more. I have a target, and its reasonably modest compared to most people - a number of people I know had a definition of wealth that's in the range of $6m and above (don't ask me how they came up with that figure - ask them!), the ability to own at least a few Lamborghini-type cars, a private yacht and a Gulfstream jet.

Well, I'll be quite happy if I have the capability to pay for my own place to live in, a recurring income stream that satisfies my daily expenditure (which is modest, compared to most people), and possibly some excess money left to afford a yearly vacation (business-class tickets are nice, but not necessary!). Given I'm not the kind of person who needs to rinse my mouth with 'Chateau Lafite' every morning, mandate my meals to be caviar and foie gras every day, I hope it is a goal that I can attain at a reasonable young age, so that I can become a flâneur - someone who has attained the freedom to seek out whatever experiences he desires and being able to live by his own rules.


[1] Referring to the stock market crash of 1987 ( Black Monday )