Symlink Mirror – A Useful Python Script

Just wanted to share a useful little python script I crafted. I creates symlinks from the top level directories in a specified source folder to a target folder. Its a neat little tool for linking between two distant directories under UNIX. Share and enjoy!

#! /usr/bin/env python
"""    Symlink Mirror -----------------------------------------------------------
       Author: Dorian Pula    Version: 0.1    Date: 2008 May 29
       --------------------------------------------------------------------------
       Creates symlinks in the current directory to the top level folders inside
       a specified directory.  Its a great utility for linking a user's home
       directory with a directory holding files shared between users on the same
       system.

       Usage: python symlink_mirror.py [source of links] [target for links]
      --------------------------------------------------------------------------
       This program is free software: you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published by
       the Free Software Foundation, either version 3 of the License, or
       (at your option) any later version.

       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>"""

import re, os, sys, stat

verbose = "True"

def main():
    """ The main function of the program. """

    'Check if there are enough arguments supplied.'
    if checkNumberOfArgs():
         'Make the path directories.'
         sourceDir = os.path.abspath(sys.argv[1])
         targetDir = os.path.abspath(sys.argv[2])

         'Get all the names of the directories.
         sourceEntries = os.listdir(sourceDir)

         for toLink in sourceEntries:
             source = os.path.join(sourceDir, toLink)
             target = os.path.join(targetDir, toLink)
             dirCheck = os.path.isdir(source)

             if((os.path.isdir(source)) and not (os.path.islink(source)) and not (os.path.exists(target))):
                  os.symlink(source, target)
                  if(verbose == "True"):
                        print "Linking " + source + " to " + target + "."

def checkNumberOfArgs():
    """
    Checks if there are enough arguments to work on.  There should be two
    arguments, the directory from which we generate the links from, and
    the directory in which the links appear. """

    argNum = len(sys.argv)
    if argNum < 1:
        print("I need a source directory to mirror.")

    elif argNum < 2:
        print("I need the directory to store the created symlinks.")

    if argNum < 2:
        print("nUsage:")
        print("python symlink_mirror.py [source of links] [target for links]n")
        return False

    else:
        return True

if __name__ == "__main__": main()