def main(argv):
    """main function."""

    instance_type = None
    heroku_app = None
    manage_py = script_utils.manage_py_command()
    manage_command = "python " + manage_py
    fixture_path = script_utils.manage_py_dir() + "fixtures"

    try:
        opts, args = getopt.getopt(argv, "t:r:h:d", ["type=", "heroku=", "help", "data"])
    except getopt.GetoptError:
        script_utils.exit_with_help(__doc__)

    if not opts:
        script_utils.exit_with_help(__doc__)

    for opt in opts:
        if opt[0] == "-h" or opt[0] == "--help":
            script_utils.exit_with_help(__doc__)
        if opt[0] == "-t" or opt[0] == "--type":
            instance_type = opt[1]
        if opt[0] == "-r" or opt[0] == "--heroku":
            heroku_app = opt[1]
            manage_command = "heroku run --app %s python makahiki/manage.py" % heroku_app
        if opt[0] == "-d" or opt[0] == "--data":
            data_only = True

    if not instance_type in ("default", "demo", "test", "uh12"):
        script_utils.exit_with_help(__doc__)

    _ = args

    if not data_only:
        if not heroku_app:
            script_utils.install_requirements()
        else:
            script_utils.create_heroku_app(heroku_app)
            script_utils.push_to_heroku(heroku_app)

        script_utils.reset_db(heroku_app)

    script_utils.syncdb(manage_command)

    #script_utils.copy_static_media(heroku_app)

    script_utils.load_data(manage_command, instance_type, fixture_path)

    print "initialize_instance completed."
def main(argv):
    """main function."""

    instance_type = None
    heroku_app = None
    manage_py = script_utils.manage_py_command()
    manage_command = "python " + manage_py
    fixture_path = "fixtures"

    try:
        opts, args = getopt.getopt(argv, "t:r:h", ["type=", "heroku=", "help"])
    except getopt.GetoptError:
        script_utils.exit_with_help(__doc__)

    if not opts:
        script_utils.exit_with_help(__doc__)

    for opt in opts:
        if opt[0] == "-h" or opt[0] == "--help":
            script_utils.exit_with_help(__doc__)
        if opt[0] == "-t" or opt[0] == "--type":
            instance_type = opt[1]
        if opt[0] == "-r" or opt[0] == "--heroku":
            heroku_app = opt[1]
            manage_command = "heroku run --app %s python makahiki/manage.py" % heroku_app

    if not instance_type in ("default", "demo", "test", "uh12"):
        script_utils.exit_with_help(__doc__)

    _ = args

    if not heroku_app:
        script_utils.install_requirements()
    else:
        script_utils.create_heroku_app(heroku_app)
        script_utils.push_to_heroku(heroku_app)

    script_utils.reset_db(heroku_app)

    script_utils.syncdb(manage_command)

    script_utils.copy_static_media(heroku_app)

    script_utils.load_data(manage_command, instance_type, fixture_path)
def run(logfile):
    """
    Initializes the Makahiki database with default options and logs the 
    output to a file. This should only be used to initialize local 
    installations.
    """
    now = datetime.datetime.now()
    time = now.strftime("%Y-%m-%d %H:%M:%S")
    start_time = "Makahiki instance initialization script started at %s\n" % time
    logfile.write(start_time)
    print start_time

    try:
        # Retrieve the user's home directory
        USER_HOME = subprocess.check_output(["echo $HOME"], stderr=subprocess.STDOUT, shell=True) 
        # Remove newline from expected "/home/<username>\n"
        USER_HOME = USER_HOME[:-1]
        USER_PROJECT_HOME = USER_HOME + os.sep + "makahiki"
        # cd to makahiki directory
        os.chdir(USER_PROJECT_HOME)

        # Capture console output from script_utils functions:
        normal_stdout = sys.stdout
        output_capturer = StringIO.StringIO()
        sys.stdout = output_capturer

        # Runs the initialization scripts in same order as 
        # makahiki/makahiki/scripts/initialize_instance.py
        instance_type = None
        heroku_app = None
        manage_py = script_utils.manage_py_command()
        manage_command = "python " + manage_py
        fixture_path = "makahiki" + os.sep + "fixtures"

        # Install requirements
        script_utils.install_requirements()

        # Switch back to standard I/O
        sys.stdout = normal_stdout
        output = output_capturer.getvalue()
        logfile.write(output)
        print(output)
        # Clear the logfile buffer.
        logfile.flush()
        os.fsync(logfile)
        
        # Reset the database 
        reset_db_result = local_reset_db(logfile)
        # If successful, write the output of local_reset_db to a logfile
        logfile = reset_db_result[0]
        local_reset_db_cancel = reset_db_result[1]
        if local_reset_db_cancel:
            logfile.write("Makahiki instance initialization was cancelled by the user.")
            print "Makahiki instance initialization was cancelled by the user."
            end_time = termination_string()
            logfile.write(end_time)
            print end_time
            return logfile
        else:
            # Resume capturing I/O
            normal_stdout = sys.stdout
            output_capturer = StringIO.StringIO()
            sys.stdout = output_capturer            
            
            # Sync the database
            script_utils.syncdb(manage_command)

            # Switch I/O back, write output to logfile
            sys.stdout = normal_stdout
            output = output_capturer.getvalue()
            logfile.write(output)
            print(output)
            # Clear the logfile buffer.
            logfile.flush()
            os.fsync(logfile)

            # Resume capturing I/O
            normal_stdout = sys.stdout
            output_capturer = StringIO.StringIO()
            sys.stdout = output_capturer            

            # Copy static files
            script_utils.copy_static_media(heroku_app)
            
            # Switch I/O back, write output to logfile
            sys.stdout = normal_stdout
            output = output_capturer.getvalue()
            logfile.write(output)
            print(output)
            # Clear the logfile buffer.
            logfile.flush()
            os.fsync(logfile)

            # Resume capturing I/O
            normal_stdout = sys.stdout
            output_capturer = StringIO.StringIO()
            sys.stdout = output_capturer 

            # Load data
            script_utils.load_data(manage_command, instance_type, fixture_path)
            
            # Switch I/O back, write output to logfile
            sys.stdout = normal_stdout
            output = output_capturer.getvalue()
            logfile.write(output)
            print(output)          
            # Clear the logfile buffer.
            logfile.flush()
            os.fsync(logfile)

            # Print a closing message
            closing = "\nMakahiki initialization script has completed.\n"
            logfile.write(closing)
            print closing
            end_time = termination_string()
            logfile.write(end_time)
            print end_time
            return logfile        

    except subprocess.CalledProcessError as cpe:
        logfile.write("CalledProcessError: ")
        print "CalledProcessError: "
        logfile.write(cpe.output)
        print cpe.output
        logfile.write("Warning: Makahiki initialization did not complete successfully.")
        print "Warning: Makahiki initialization did not complete successfully."
        end_time = termination_string()
        logfile.write(end_time)
        print end_time
        return logfile
    except OSError as ose:
        logfile.write("OSError: ")
        print "OSError: "
        oserror_output = " errno: %s\n filename: %s\n strerror: %s\n" % (ose.errno, ose.filename, ose.strerror) 
        logfile.write(oserror_output)
        print oserror_output
        logfile.write("Warning: Makahiki initialization did not complete successfully.")
        print "Warning: Makahiki initialization did not complete successfully."
        end_time = termination_string()
        logfile.write(end_time)
        print end_time
        return logfile