#!/usr/bin/env python

import argparse
import sys
import os
import os.path
import shutil

abs_file_path = os.path.abspath(__file__)

# Do the right thing with symlinks
while os.path.islink(abs_file_path):
    abs_file_path = os.readlink(abs_file_path)

root_dir = os.path.split(abs_file_path)[0]
print "__file__: ", __file__
print "root_dir: ", root_dir

parser = argparse.ArgumentParser(description = 'Jetpack deploy script')
parser.add_argument('command', type=str)
parser.add_argument('--project', type=str)

# TODO: Figure out how to make this arg optional
parser.add_argument('--scaffold-repo', type=str, default = 'https://github.com/phenguin/jetpack')

def setup_links(source_base_dir, link_base_dir):
    print "\nLinking scaffolding files to project dir."
    for dirpath, dirnames, filenames in os.walk(source_base_dir):
        if not dirpath.startswith(os.path.join(source_base_dir, '.git')):

            link_base_path = dirpath.replace(source_base_dir, link_base_dir)

            try:
                os.mkdir(link_base_path)
            except OSError:
                pass
            
            for fn in filenames:
                if fn == 'README.md': continue
                source_path = os.path.join(dirpath, fn)
                link_path = os.path.join(link_base_path, fn)

                if os.path.exists(link_path):
                    if os.path.islink(link_path):
                        os.remove(link_path)
                    else:
                        print >>sys.stderr, "Aborting.. Not overwriting non-link file: %s" % (link_path,)
                        sys.exit(1)

                print "Linking %s --> %s" % (source_path, link_path)
                os.symlink(source_path, link_path)

def relink_scaffolding():
    link_path = os.getcwd()
    source_path = os.path.join(link_path, 'scaffolding')
    setup_links(source_path, link_path)

def update_scaffolding():
    cwd = os.getcwd()
    os.chdir('scaffolding')
    res = os.system('git pull --ff-only')
    os.chdir(cwd)

    if res != 0:
        print "Failed to pull changes.. updating links anyways"

    relink_scaffolding()

def init_project(project_name, scaffold_repo = None):
    cwd = os.getcwd()
    project_path = os.path.join(cwd, project_name)

    print "Initializing new project in %s" % (project_path,)

    scaffold_repo = scaffold_repo or 'https://github.com/phenguin/jetpack'

    # Try to get the scaffolding code
    scaffolding_dir = os.path.join(project_path, 'scaffolding')

    try:
        os.mkdir(project_path)
    except OSError:
        print >>sys.stderr, "Error: Directory %s already exists" % (project_name,)
        sys.exit(1)

    print "\nTrying to clone scaffolding from %s" % (scaffold_repo,)
    clone_exit_code = os.system("git clone %s %s" % (scaffold_repo,scaffolding_dir))
    clone_succeded = clone_exit_code == 0 and True or False

    if clone_succeded:
        setup_links(scaffolding_dir, project_path)

def main():
    args = parser.parse_args()
    if args.command == 'init':
        init_project(project_name = args.project)
    elif args.command == 'relink':
        relink_scaffolding()
    elif args.command == 'update':
        update_scaffolding()
    else:
        print "Unrecognized command: %s" % (args.command,)
        sys.exit(1)


if __name__ == '__main__':
    main()

@@ -1166,16 +1166,39 @@
 ADME.md'
+ or fn.startswith('.') 
 : contin
@@ -1984,16 +1984,17 @@
 %0A    res
+1
  = os.sy
@@ -2007,22 +2007,67 @@
 git 
-pull --ff-only
+submodule init')%0A    res2 = os.system('git submodule update
 ')%0A 
@@ -2097,16 +2097,29 @@
 f res !=
+ 0 or res2 !=
  0:%0A    
@@ -2734,24 +2734,237 @@
 ys.exit(1)%0A%0A
+    os.chdir(project_path)%0A    git_init_res = os.system(%22git init%22)%0A    if git_init_res != 0:%0A        print %3E%3Esys.stderr, %22Abort: Could not instantiate git repository in %25s%22 %25 (project_path,)%0A        sys.exit(1)%0A%0A
     print %22%5C
@@ -3057,21 +3057,29 @@
 em(%22git 
-clone
+submodule add
  %25s %25s%22 
@@ -3113,16 +3113,95 @@
 g_dir))%0A
+    os.system(%22git commit -am 'Add scaffolding submodule'%22)%0A    os.chdir(cwd)%0A%0A
     clon
@@ -3322,24 +3322,206 @@
 roject_path)
+%0A        print%0A        os.chdir(project_path)%0A        os.system(%22git add .%22)%0A        os.system(%22git commit -m 'Link in scaffolding files'%22)%0A        os.chdir(cwd)%0A%0A    print %22%5CnDone!%22
 %0A%0Adef main()

Add relink and update commands to script