Most Recent Blog

King's View of the Battle

 Open GL model of a chess board.

April 29, 2011

Creating an SVN repository on Bluehost

Motivation:
There are many free svn repository providers.  If you are an individual developer, you may choose to use one of those.  Some have the restriction that repository is publicly accessible such as google code and sourceforge, while others keep the repository private such as projectlocker and sliksvn.  After reading the license agreements and contracts, I decided to maintain my own repository.

Warning:
This discusses how to set up and configured a svn repository on BlueHost using a windows 7 as the client machine: This setup is not for the faint of heart.  It requires modifying configuration settings, compiling source code with multiple complex config options, generating ssh keys, setting up ssh, and properly configuring the windows environment.  (NOTE:  All of these steps can be followed for linux/bsd/mac.  Slight modifications to the commands may be required, such as using forward-slashes for paths instead of back-slashes; using : for path separators instead of ';'; and other OS specific idiosyncrasies)

Quick Overview:
  1. Install SSH on the client
  2. Setup SSH Keys (you will have to contact bluehost directly to get permission to use SSH to access your server)
  3. Install an SVN Client
  4. Compile and Install the SVN Server from source on your bluehost box.
  5. Setup an ssh-svn service bridge (sshsvnserve)
  6. Instruct svn to use the service bridge when connecting to your repo.
  7. Using the repository.
  8. Properly backing up the repository (don't trust bluehost to back up the repository for you).
Getting Started:
Install SSH On Windows 7 (or any other version of windows)
I use mingw (which is minimal gnu for windows).  This is an excellent package, and does not have all the complexities of cygwin.  However, cygwin might work just fine.  I don't use it because it tends to mess up gmake/gnu's make program.
  • Download and install mingw from http://www.mingw.org/
  • Assuming you installed at c:\MinGW, then add C:\MinGW\msys\1.0\bin to your system path environment variable.
  • Confirm all is working by typing svn -v from any directory on your box, you should get:

    • C:\>ssh -v
      
      OpenSSH_5.4p1, OpenSSL 1.0.0 29 Mar 2010
      usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
                 [-D [bind_address:]port] [-e escape_char] [-F configfile]
                 [-I pkcs11] [-i identity_file]
                 [-L [bind_address:]port:host:hostport]
                 [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
                 [-R [bind_address:]port:host:hostport] [-S ctl_path]
                 [-W host:port] [-w local_tun[:remote_tun]]
                 [user@]hostname [command]
      
  • Congrats! step 1 is done
Setup SSH Keys
SSH keys allow you to login to your account on BlueHost without the need for a password.  It works by storing a private key on your computer, and you upload a public key to your account on the BlueHost box.  You can then login using ssh, or interact using scp without specifying a password, by specifying the ssh private key file.  The public and  private keys are asymmetric keys.  This means that which ever key is used to encrypt the data, the other key must be used to decrypt data.  Authentication is accomplished by providing a challenge to the requesting client.
Assuming Step 1 was completed successfully, the ssh-keygen program should be in your path.
  • pick a directory to store your public and private key, and switch to that directory
    A good choice would be -> c:\Users\[Windows User]\.ssh
  • Generate the public and private key, there are many options, choosing all defaults (with the exception of the file name) is typically good.

C:\Users\[My Account]\.ssh>ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (//.ssh/id_rsa): id_rsa_svn
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_svn.
Your public key has been saved in id_rsa_svn.pub.
The key fingerprint is:
04:a3:1a:b7:5d:6f:29:39:bc:30:a3:c7:c6:b0:2f:49 [My Account]@[mycomputer's name]
The key's randomart image is:
+--[ RSA 2048]----+
|      o          |
|     . o         |
|  . o   o        |
|   + o + o .     |
|  . o = S +      |
|    E* + =       |
|   .o.= .        |
|    o+           |
|     ..          |
+-----------------+

Test out your SSH Key
  • upload the public key to the bluehost server, KEEP the private key private! This is important because this protects the bluehost account from unauthorized access.
  • Install the key in your authorized_keys file
  • Test the access.
From your windows box:
C:\Users\[Windows User]\.ssh>ls
id_rsa  id_rsa.pub  id_rsa_svn  id_rsa_svn.pub

C:\Users\[Windows User]\.ssh>scp id_rsa_svn.pub [bluehost account]@[bluehost machine]:~/.ssh
[bluehost account]@[bluehost machine]'s password:
id_rsa_svn.pub                                                                                            100%  401     0.4KB/s   00:00

You will now need to log into the bluehost box, and manually add the public key to the authorized_keys file. To do this, log on to your bluehost account, and follow the following steps.
[bluehost account]@[bluehost machine] [~]# cd .ssh
[bluehost account]@[bluehost machine] [~/.ssh]# dir
./  ../  authorized_keys2  environment  id_rsa_svn.pub
[bluehost account]@[bluehost machine] [~/.ssh]# cat id_rsa_svn.pub >> authorized_keys2

NOTE to see the changed file:
[~/.ssh]# cat authorized_keys2

Finally From the windows box, test out the key to make sure that it works:
C:\>ssh -i "c:\Users\[Windows User]\.ssh\id_rsa_svn" [bluehost account]@[bluehost machine]
Last login: Thu Apr 28 17:08:06 2011 from 32.178.80.204
[bluehost account]@[bluehost machine] [~]#


Install An SVN client
Any svn client should do the job, SlikSVN seems to be a good distribution.  However, many others will do the job.  SlikSvn comes as a standard windows installer, it will typically add the subversion client tools to your path.  Once it is installed on your windows box, test to make sure the svn.exe file is executable from any directory.

C:\>svn --version
svn, version 1.6.16 (SlikSvn/1.6.16) X64
   compiled Mar  3 2011, 22:35:47

Copyright (C) 2000-2009 CollabNet.
Subversion is open source software, see http://subversion.apache.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository access (RA) modules are available:

* ra_neon : Module for accessing a repository via WebDAV protocol using Neon.
  - handles 'http' scheme
  - handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
  - with Cyrus SASL authentication
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme
* ra_serf : Module for accessing a repository via WebDAV protocol using serf.
  - handles 'http' scheme
  - handles 'https' scheme

Compiling SVN Server on BlueHost
  1. Download the latest version of Subversion from sourceforge.
  2. copy it up to the BlueHost Account
  3. configure the compilation process for BlueHost
  4. make the components
  5. install the svn server and libraries
I will outline these steps in the following shell capture.  Note that config and compiling take several minutes each.
[~]# cd src
[~/src]# cd subversion-1.6.16
[~/src/subversion-1.6.16]# ./configure --prefix=$HOME/svn/bin --without-apxs --exec-prefix=$HOME/svn/lib --disable-mod-activation
[~/src/subversion-1.6.16]# nohup make &
[~/src/subversion-1.6.16]# tail -f nohup.out
[~/src/subversion-1.6.16]# make install

Subversion server is now installed on BlueHost. At this point, it must be tested.
The following shell snippet shows the testing expectation:
[~]# cd svn/lib/bin
[~/svn/lib/bin]# ./svnserve --version
svnserve, version 1.6.16 (r1073529)
   compiled Apr 27 2011, 17:27:09

Copyright (C) 2000-2009 CollabNet.
Subversion is open source software, see http://subversion.apache.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository back-end (FS) modules are available:

* fs_fs : Module for working with a plain file (FSFS) repository.

Cyrus SASL authentication is available.

Setting up an ssh-svn service bridge:
In this step a bridge will be set up.  The purpose of this bridge will be two things:
  • Set up enough of an environment to execute svnserve command (by default and for security purposes bluehost disables shells and environments for remote logins)
  • Instruct ssh to invoke a temporary service immediately on connect.  (this service will of course be the svnserve command which serves content from an svn repository)
Log into your BlueHost machine, change to the .ssh directory, open the authorized_keys2 file with a text editor, vi or pico should do. Find the public key entry that was added to this file in the step above.  This key is going to be modified by adding some instructions to the front of the key.  the instructions to be added are: command="/home1/[your bluehost account]/sshsvnserve -t" see the code snippet below for an example of what things should look like:
command="/home1/[your bluehost account]/sshsvnserve -t" ssh-rsa AAAABCCCCCCDDDDDDDD3NzaC1yc2EAAAADAQABAAABAQDJezPXNZb8rls8GrxH46zZwUO6vm1bkgxD2jma4RJMlXH0ADTDsQ2PGZAW80hVamojBsKGUN8zYH2i3LiEYiNSqL2d6mvFZAxIGH4mxhQQZdImyIWEnAK5a9HL9SQlIRnKa0ZEEsi/KWmiyB/gOXtKxxW7aU9KIsk2mjG0UZfmRHE4OhvXQqguAZCScOHh/WDRXiC2t2WbmfSXJK7KNz2sjla8Y62Z+u3Lg0m4SlmNqLgllh+tY0Nbxd623sJSQVqNwutPPMd1IhybjQskXUv8OlSmVepkhnGpSteRIzI99MocX8LzrZefHSaPsyMeHri52VzTb0
3 [windows usr]@[windows machine name]

The only thing added is the first command=..... up to the 'ssh-rsa' of course modified to match your account.

Now create the sshsvnserve shell script.  This is a very simple shell script.  Switch back to the home directory on the bluehost machine, and create a file with the following content.  Note that you will have to change this file to executable before it can be used.

#!/bin/sh
/home1/[your bluehost account]/svn/lib/bin/svnserve $*

Instruct svn to use the service bridge when connecting to your repo.
This is done by adding an ssh directive to your tunnels command.  On the windows box, create a subversion config file as bellow.

C:\Users\[Windows Account]\.subversion>cat config


[tunnels]
ssh = ssh -i "c:\\Users\\[Windows Account]\\.ssh\\id_rsa_svn"


USING your new repository:
C:\repos>svn --config-dir="c:\Users\[Your Windows Account]\.subversion" co svn+ssh://[your bluehost account]@[your bluehost machine/ip]/home1/[your bluehost account]/repos


Using the repository
The repository can be used by simply making sure that the --config-dir option is specified, and points to the directory that contains your config file with the [tunnels] entry.

Backing up the Repository:
Assuming that you are using BlueHost for a single user or very small number of users, you can simply create a synchronized repository on your local machine. The steps will be outlined in a code snippet block, with comments.

#  First create the repository.
C:\repos>svnadmin create .

#  Modify the hooks so the repos can accept
#  backup requests from other repositories.
C:\repos>cd hooks
C:\repos\hooks>echo exit 0 > pre-revprop-change.bat
C:\repos\hooks>cat pre-revprop-change.bat
exit 0

# 
C:\repos>svnsync initialize file:///repos svn+ssh://[bluehost caccount]@[bluehost machine]/home1/[bluehost account]/repos --config-dir="c:\Users\[Windows Account]\.subversion"
Copied properties for revision 0.

C:\repos>svnsync synchronize file:///repos --config-dir="c:\Users\[Windows Account]\.subversion"
Committed revision 1.
Copied properties for revision 1.
Committed revision 2.
Copied properties for revision 2.
Transmitting file data .......................................................................
..............................................................................................
...........
Committed revision 3.
Copied properties for revision 3.
Transmitting file data .......................................................................
..............................................................................................
...
Committed revision 4.
Copied properties for revision 4.
Transmitting file data .


April 3, 2011

Python - Insertion Sort Impl

This article will discuss a simple insertion sort algorithm for python.
See ALL Python postings  |  Insertion Sort By Language Java Scala Go C++ Python Perl

Overview of the Insertion Sort Algorithm
Insertion sort is one of the most natural wayTs of sorting a collection of comparable items.  The reason it is one of the most natural ways is because it is the way most people will sort physical items.   For example each year (about this time) I will sort all 12 of my previous years phone bills, gas bills, etc...  The algorithm used to sort them is to place the first one in stack.  Then the second one is compared to the first one to see if it goes in front of or behind the first one.  The third one is then compared to each if the two sorted items to determine where it fits in.  This procedure continues until all 12 bills are in the pile.  Once all 12 bills are in the pile it is sorted. I happen to sort form oldest to newest, my spouse does it the other way newest to oldest.

Applying this concept to a collection of objects in a computer is fundamentally the same.  The algorithm dictates that one will iterate over a collection of elements in an unsorted list 1 at a time, and place that item into it's correct location in a sorted list.

If the collection of objects to sort is n, insertion sort will take at most $O(n^2)$ operations to perform the sort, it will take on average $\frac{1}{2} O(n^2) = O(n^2)$, and in the best case senario will take $O(n)$ operations.


Questions:  


1.  What would be the initial characteristics of a list that took the worst case order $O(n^2)$ to sort?
2.  What would be the initial characteristics of a list that took the best case order $O(n)$ to sort?


About the Python Solution:
In python this solution relied a bit on creativity.  The solution is clearly not $O(n^2)$,  however it may take a bit to understand why.  The reason lies in the fact that python does not have a linked list that allows insert at a given point in the list.  Therefore, to simulate that behavior, this solution rotates the list until it cat append to the left side of the list, and then rotates back.  This ensures that the order is correct.  What would be the big O of this solution assuming that each rotation is O(n)? Some additional inline comments have been included.

If you have a question about the solution (or feel it should be improved) please leave a comment.  All comments are moderated and posted with your name/Identification so please be professional and straight forward. Python Solution:

#!/opt/python
from collections import deque
import random

dq = deque()
for i in range(20):
    dq.append(random.randrange(1001))

for elem in dq:
    print(elem)

sortedQueue = deque()
j=int(0)
k=int(0)
for elem in dq:


    for sortedElem in sortedQueue :
        if elem>sortedElem:
            sortedQueue.rotate(-j)
            sortedQueue.appendleft(elem)
            sortedQueue.rotate(j)
            k=1+k
            break
        j=j+1
    if (k == 0):
        sortedQueue.append(elem)
    else:
        k=0
    j=0

print (sortedQueue)
Answers: 1.  The best case senario would be order: $O(n)$ 2.  The worst case senario would be order: $O(n^{2})$golang postings

April 1, 2011

C++ - Insertion Sort Implementation w/ Make file Introduction

This article will discuss a simple insertion sort algorithm in C++. Unlike any of the other languages in this series on the insertion sort algorithm. C++ has for more caveats and options. While the algorithm is exactly the same the implementation details are filled with pitfalls.
See ALL C++ postings   |  Insertion Sort By Language Java Scala Go C++ Python Perl

Overview of the Insertion Sort Algorithm
Insertion sort is one of the most natural ways of sorting a collection of comparable items.  The reason it is one of the most natural ways is because it is the way most people will sort physical items.   For example each year (about this time) I will sort all 12 of my previous years phone bills, gas bills, etc...  The algorithm used to sort them is to place the first one in stack.  Then the second one is compared to the first one to see if it goes in front of or behind the first one.  The third one is then compared to each if the two sorted items to determine where it fits in.  This procedure continues until all 12 bills are in the pile.  Once all 12 bills are in the pile it is sorted. I happen to sort form oldest to newest, my spouse does it the other way newest to oldest.

Applying this concept to a collection of objects in a computer is fundamentally the same.  The algorithm dictates that one will iterate over a collection of elements in an unsorted list 1 at a time, and place that item into it's correct location in a sorted list.

If the collection of objects to sort is n, insertion sort will take at most $O(n^2)$ operations to perform the sort, it will take on average $\frac{1}{2} O(n^2) = O(n^2)$, and in the best case senario will take $O(n)$ operations.


Questions:  


1.  What would be the initial characteristics of a list that took the worst case order $O(n^2)$ to sort?
2.  What would be the initial characteristics of a list that took the best case order $O(n)$ to sort?


About the C++ Solution:
There are several caveats to the C++ solution.  Therefore the source file will simply be discussed from top to bottom, with each decision discussed in turn.

  1. Imports: The first thing to notice is the choice of the imports, this solution imports the iostream, list, and cstdlib.  
    1. The iostream should be familiar as it is used for I/O.  
    2. The list chosen here is from the STL of C++ and is included with the #import <list> directive.
    3. The cstdlib is imported to expose the rand function which is a sudo random number generator.
  2. using namespace std - this allows reference without the need for adding std:: to methods or objects included from std libraries.
  3. Forward Declarations- C++ requires knowledge of all methods and classes before they are used.  That means that the compiler must know what each class/methods signature is before first use.  (see inline comment)
  4. Pointers Vs Automatics - In this simple example, two lists of ints are created, the unsorted list, and the sorted list.  For pedagogical purposes, one was created on the heap, and the other as an "automatic" on the stack.  Heap allocated objects must be explicitly freed, or there might be a memory leak.  In C++ malloc and free go together, and new/delete go together, where delete releases the memory back to the operating system.  Objects that are stack allocated (created without a call to new or malloc/calloc) will be automatically released once the object goes out of scope.  Note that it might be possible to blow your stack if you allocate to many automatics.
  5. Iterators - This code example is using the concept of a C++ STL iterator.  The concept of an iterator actually warrants multiple posts.  Basically from a very simplistic point of view, an iterator will visit each member of the list in turn.

The code has comments that are non-verbose, and useful.  Reading the code and comments along with compiling and running the code will help in the understanding of this exercise.  If you have a question about the solution (or feel it should be improved) please leave a comment.  All comments are moderated and posted with your name/Identification so please be professional and straight forward.
C++ Solution:



The make file:
The make file is presented twice, once with inline comments, and once without.  Make files have many interesting caveats that may make direct cutting and pasting of the file difficult, but I will explain.
in a make file, targets are the commands that can be executed.  Each target line contains instructions to be carried out.  The instruction lines are on the line(s) immediately following the target line.  Each target knows it's instructions ONLY by looking for the consecutive lines that immediately follow it, AND are indented with a single TAB '\t' character.  Therefore, if you text editor converts tabs to spaces or something like that, it will mangle the make file because the tab characters are significant and have meaning.

first without comments, then with comments for reference:
#--------------------------------------
#
#  Copyright (c) 2011 EcoCrypt.com 
# 
#--------------------------------------

CPP=g++

MAIN=InsertionSort

.SUFFIXES : .exe .cpp

ifeq (${OS},Windows_NT)
EXES=${MAIN}.exe
RM=del /S 
else
EXES=${MAIN}
RM=rm -f # implicitly defined on unix
endif

ALL: ${EXES}

.cpp.exe: 
 echo ${CPP} ${DIRECTIVES} $? -o $@ 
 ${CPP} ${DIRECTIVES} $? -o $@ 

clean:
 ${RM} ${EXES}



Find below the make file with comments:
#--------------------------------------
#
#  Copyright (c) 2011 EcoCrypt.com 
# 
#--------------------------------------

#---------------------------------------
#  Set your compiler
#  On Windows Microsoftt compiler:
#  (i.e. CPP=cl)
#  On Linux, GUN compiler:
#  CPP=g++
#  On Windows MinGW (Minimal GNU for Windows):
#  CPP=g++.exe
#

CPP=g++
#CPP=cl.exe

#  The name of the main output
MAIN=InsertionSort

#  WE have to pass in VC98 and -GX if the compiler
#  is the cl compiler from Microsoft.
ifeq ($(CPP),cl.exe)
DIRECTIVES= -DVC98 -GX
else
endif

.SUFFIXES : .exe .cpp
#---------------------------------------
#  The windows version will contain a .exe
#  extention for each file.
#
#  List the executables.
#  here we have 2 mains that we 
#  will make: test and greetings.
#
#  Also redefine the implicit RM var.
ifeq (${OS},Windows_NT)
EXES=${MAIN}.exe
RM=del /S 
else
EXES=${MAIN}
RM=rm -f # implicitly defined on unix
endif

#---------------------------------------
#  The first target will create all
#  the EXES and recreate any that are 
#  out of Date.
#
#  This is also important because it is 
#  the default target, if you just invoke
#  make from the command like, the first 
#  dependency line becomes the default target.
#
ALL: ${EXES}

#---------------------------------------
#  Suffix rules are powerful and cryptic.
#  This states, for any target with no  (or .exe on windows)
#  suffix, look for a like named file with
#  an extension of .cpp, and perform the command
#  
#  (e.g.  for a file/dependecy named 'test.exe'
#         look for test.cpp, and perform
#         g++ test.cpp -o test.exe)
.cpp.exe: 
 echo ${CPP} ${DIRECTIVES} $? -o $@ 
 ${CPP} ${DIRECTIVES} $? -o $@ 

#---------------------------------------
#  clean the targets so you can force a 
#  rebuild

clean:
 ${RM} ${EXES}



Common Issues:
Forgetting to add the "using namespace std"
This is a common issue because older C++ books did not discuss this as it was added to C++ some time after the ansi standard was initially finalized.
Typically Error:
EcoCryptCppExamples\src\InsertionSort % make
echo g++  InsertionSort.cpp -o InsertionSort.exe
g++  InsertionSort.cpp -o InsertionSort.exe
g++  InsertionSort.cpp -o InsertionSort.exe
InsertionSort.cpp: In function 'void printList(std::list)':
InsertionSort.cpp:57:48: error: 'endl' was not declared in this scope
make: *** [InsertionSort.exe] Error 1

Leaving off one of the import statements. This demonstrates the error if the cstdlib is not imported, which defines the rand and srand functions.
EcoCryptCppExamples\src\InsertionSort % make
echo g++  InsertionSort.cpp -o InsertionSort.exe
g++  InsertionSort.cpp -o InsertionSort.exe
g++  InsertionSort.cpp -o InsertionSort.exe
InsertionSort.cpp: In function 'void printList(std::list)':
InsertionSort.cpp:57:48: error: 'endl' was not declared in this scope
make: *** [InsertionSort.exe] Error 1

The make file is missing key tabs:
C:\47906Proxy\EcoCryptCppExamples\src\InsertionSort>make
Makefile:70: *** missing separator.  Stop.


Answers: 1.  The best case senario would be order: $O(n)$ 2.  The worst case senario would be order: $O(n^{2})$golang postings