Thursday, May 8, 2008

The simplest (python based) pre-commit hook in Subversion

Hooks in Subversion are scripts or executables that are triggered by an event in the subversion version control life cycle. The following are the hooks supported by Subversion.

In the hooks directory of a repository we can find these template files:
post-commit.tmpl      pre-unlock.tmpl
post-lock.tmpl          pre-commit.tmpl
start-commit.tmpl     post-revprop-change.tmpl
pre-lock.tmpl          post-unlock.tmpl     pre-revprop-change.tmpl

start-commit
Before the commit transaction starts
pre-commit After the commit transaction starts but before the transaction is commited
post-commit After the commit transaction completes
pre-revprop-change Before a revision property is changed Repository Path,
post-revprop-change: After a revision property is changed Repository Path
pre-lock:  Before the lock being acquired
post-lock:  
After the lock being acquired

To test how a hook works, let’s create a repository in our Gnu/Linux Box.

svnadmin create hello

Now we have a repository called hello and it will contain files like
conf dav db format hooks joke.py locks pre-commit README.txt
Modify the conf/svnserve.conf to include passwd file and add users to the passwd file along with their passwords.

Then run the server as svnserve -d -r hello
To checkout :
svn co svn://127.0.0.1

Then lets move to the real work. We need to stop anybody who is trying to commit to our repository just because we are doing some important work on the server or we just dont want anybody to help us :)

To do that, let’s create a file called pre-commit in hooks folder in the repository (hello). The content of the pre-commit should be :

#!/usr/bin/env python
# ====================================================================
# The simplest pre-commit hook which prevents all the commits to the repository
# This can be useful(?) when you are doing the backup of your subversion repo.
# This small script will prevent the commit and will give a simple message
# to the person who is commiting the changes.
#
# USAGE:
# copy this script as pre-commit in the hooks directory of the svn server
#
# ====================================================================

import sys

MESSAGE=”"”
Dear Sir,

As we are doing some important work in the server,all those who are submitting
the changes will have to wait for some time.

Sorry for the inconvenience caused.

Regards,

Administrator.
“”"
# Messages written to the stderr will be shown to the person who is commiting.
sys.stderr.write(MESSAGE)

# Need to exit with anything other than 0 to fail the commit
sys.exit(2)

Let’s change the mode of the pre-commit to executable (chmod +x pre-commit)

To test this setup, lets create a sandbox and then try to commit our changes to the server.
svn co svn://127.0.0.1
cd 127.0.0.1
mkdir test
svn add test
svn commit -m “adding the test directory”

Now we will see the pre-commit hook in action:

Adding test
svn: Commit failed (details follow):
svn: ‘pre-commit’ hook failed with error output:

Dear Sir,

As we are doing some important work in the server,all those who are submitting
the changes will have to wait for some time.

Sorry for the inconvenience caused.

Regards,

Administrator.


This is the most simplest hook that can be implemented in Python. More complex hooks can be implemented in python using the svn module. More useful hooks examples are available from http://subversion.tigris.org/tools_contrib.html#hook_scripts

Posted by maxinbjohn at 05:39:11 | Permalink | Comments (2)

Saturday, June 2, 2007

Subversion setup in Mandriva 2007, Suse EL 10 and Fedora Core 6

Subversion setup is similar in Mandriva 2007, Suse EL 10 and Fedora. It is easier in Mandriva just because of urpmi. If you want to install a package , just type the urpmi packagename. It will get installed and urpmi will resolve all the dependency issues. In my experience, it is much better compared to apt-get ( I have installed apt-get for Suse 10) and yum (Fedora ) is not upto the mark yet when compared to urpmi (It is based on my experience with these distros.. your opinion may vary :-) )

Now let’s go to the installation saga…

Apache installation  

Most probably apache is pre installed in almost all the distros. But Suse Desktop edition is an exception.To install apache

  • Login or su as root
  • Run:
In Mandriva
urpmi apache

We don’t have to worry about setting up SSL for https in Mandriva as it is pre configured.

  • Restart httpd
/etc/init.d/httpd restart
  • HTTPS should work.

Try: https://hostname/

 

In Fedora,

Apache is a standard package. We can select that package and install it from the CD or DVD .. Same in the case of Mandriva
We don’t have to worry about setting up SSL for https in Mandriva as it is pre configured.

  • Restart httpd
/etc/init.d/httpd restart
  • HTTPS should work.

Try: https://hostname/

 

In Suse,

Webserver is not pre installed in Suse. We need to install it .

It is not preconfigured for https also. So we need to setup https by editing the configuration files and select the SSL certificate file in that configuration file. 

  • Restart httpd
/etc/init.d/apache restart
  • HTTPS should work.

Try: https://hostname/

 

Or the generic procedure…

Download Apache 2 latest Source disrtibution from:
http://httpd.apache.org/download.cgi
Unix Source :httpd-2.0.52.tar.gz

and unpack the package .

./configure –prefix=/usr/local/apache2 –with-mysql –with-susexec
–enable-mods-shared=all
make
make install 

Now for Subversion configuration ,

Install the packages

subversion
libapache2-svn
subversion-tools

Creating a repository

In Mandriva and Fedora
cd /home
mkdir  myrepo
chown apache myrepo
In Suse
cd /home
mkdir  myrepo
chown www-run myrepo

In  Mandirva and Fedora
su apache -c "svnadmin create myrepo/src"

Then

su -

and edit your httpd.conf

in the /etc/httpd/conf/httpd.conf 

 In VirtualHost declaration, add

 <Location /myrepo/src>
DAV svn
SVNPath /home/myrepo/src
    AuthType Basic
AuthName "Ushustech Repository"
AuthUserFile /home/myrepo/.dav_svn.passwd
Require valid-user
</Location>
  • Use this instead of “Require valid-user” if you don’t want to use passwords for read-only access
  <LimitExcept GET PROPFIND OPTIONS REPORT>
Require valid-user
</LimitExcept>

 Append this in the httpd.conf file.
LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule dav_svn_module modules/mod_dav_svn.so
make sure that mod_dav_svn.so ,mod_dav.so and mod_dav_fs.so are present in the httpd.conf .
Adding the Subversion users.
su apache -c "htpasswd -c -m /home/myrepo/.dav_svn.passwd maxin"
Give password for that user two times as usual .
Add another user. Now remember not to use the -c in httpasswd command or it will recreate the
file with just that user.
su apache -c "htpasswd -m /home/myrepo/.dav_svn.passwd sree"

Now let's test the repository
  1. Using your browser. Load https://127.0.0.1/myrepo/src/ in your browser. Replace hostname with your host name or ip. You will get something like:
Revision 0: /
Powered by Subversion version 1.1.3 (r12730).

Now start using subversion from the command line

svn co https://127.0.0.1/myrepo/src
This will prompt for username and password of a valid user.
This will create a directory src in your machine. Then
cd src
and add the directories
mkdir  branches
mkdir site
mkdir  tags
mkdir  trunk

Then type
svn add *
svn update
svn commit
 Test your subversion from browser again  mozilla https://127.0.0.1/myrepo/src
Revision 1: /
* branches/
* site/
* tags/
* trunk/
Powered by Subversion version 1.1.3 (r12730).
For Suse, the apache user is not present, So replace that apache with www-run. Also the htpasswd command is htpasswd2 in Suse. Other configurations are same .

Well that's all. Keep your subversion repo clean using my previous shell script :-). Happy Subversioning ..
Posted by maxinbjohn at 08:41:10 | Permalink | No Comments »

Wednesday, May 23, 2007

Cleaning up the Subversion

Subversion … A really wonderful tool for managing the the Software development for highly distributed programmers and tester. We can easily perform the Change Management (Source Code Management) using this tool.

A subversion cheat sheat is available at http://www.yolinux.com/TUTORIALS/Subversion.html . It’s really helpful for those who wants to know more about subversion.

It can be a messy tool if some of your team mates use it in the wrong way. You will need to remove those .o, .ko or .swp files from the subversion repository just because they have done the compilation from the local copy and forgot to run make clean and then committed that. Specially when you are a Configuration Controller (Unfortunately I am one of them and an programmer too..) . So here is the script to clean up your very own repository .

 

#!/bin/bash
# Usage: ./tidysvn.sh
# This script will clean the checked out copy of source code by removing the unnecessary files like .o , .ko and .cmd, etc
# Hacked by Maxin B. John from a shell script to generate the .m3u list

IFS=$’n’ # Input Field Separator
# Normally this internal bash variable includes
# any type of whitespace, but we want to avoid
# splitting up file names with spaces.

# Fuction to cleanup the unnecessary files.
cleanupDir ()
{
FileList=”" # initialize empty variable FileList
for FileTypes in “o” “cmd” “ko” “swp” “swo” # loop over file types.
# the user can add file types here.
# FileTypes is a variable that cycles

# inside the for loop.
do # the next line of a for loop is do

FindFiles=$(find $(pwd) -type f -iname “*.$FileTypes” | sort)
# $(command) is equivalent to `command` (another way to do command substitution)
# This is mainly useful because you can have nested command substitutions using $(),
# which does not work with backquotes.
FileList=$FileList$FindFiles
# After exiting the loop, you have a single list of files for all extensions.
# The find command above puts a n (new line) after each file, so you are
# building up a list of files with one file per line.
done # end the loop with done

if [ "${#FileList}" != "0" ] # do not remove the files if the file list is empty
then
# get the current directory
CurrDir=$(pwd)
# cleanup the *.o files from the current directory
svn rm *.o 2&> /dev/null
# cleanup the *.cmd files from the current directory
svn rm .*.cmd 2&> /dev/null
# cleanup the *.ko files from the current directory
svn rm *.ko 2&> /dev/null
# cleanup the *.swp files from the current directory
svn rm .*.swp 2&> /dev/null
# cleanup the *.swo files from the current directory
svn rm .*.swo 2&> /dev/null
fi
}

# This is the body of the script
AllDirs=$(find $(pwd) -type d | sort) # Get a complete list of full paths to subdirectories,
# including the top level directory. This list
for Directory in $AllDirs # loop over the top level directory and all subdirectories
# This didn’t have to be recursive because the find command does the work.
do
cd “$Directory” # cd to each directory
cleanupDir # The function cleanupDir is called
done

exit 0 # success !

Hmm. .. It works for me.

 

Posted by maxinbjohn at 12:15:13 | Permalink | No Comments »