<commit_before>from django.core import management

import argparse
import getpass
import os
import os.path
import stat
import shutil
import subprocess
import sys


parser = argparse.ArgumentParser(
    description="Start a new CMS project.",
)

parser.add_argument(
    "project_name",
    help="The name of the project to create.",
)

parser.add_argument(
    "dest_dir",
    default=None,
    nargs="?",
    help="The destination dir for the created project.",
)

# parser.add_argument(
#     "--noinput",
#     action="store_false",
#     default=True,
#     dest="interactive",
#     help="Tells Django to NOT prompt the user for input of any kind.",
# )


def git(*args):
    return subprocess.check_call(['git'] + list(args))


def make_executable(path):
    st = os.stat(path)
    os.chmod(path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)


def query_yes_no(question, default="yes"):
    """Ask a yes/no question via raw_input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is one of "yes" or "no".
    """
    valid = {"yes": True, "y": True, "ye": True,
             "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        sys.stdout.write(question + prompt)
        choice = raw_input().lower()
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' "
                             "(or 'y' or 'n').\n")


def configure_apps(path, apps, project):

    print 'Creating temporary folder...'
    temp_folder = os.path.join(path, 'apps', 'temp')

    for app in apps:
        if apps[app]:
            try:
                app_folder = os.path.join(temp_folder, app)

                git(
                    "clone",
                    "<EMAIL>:onespacemedia/cms-{}.git".format(app),
                    app_folder
                )

                for src_dir, dirs, files in os.walk(app_folder):
                    for d in dirs:
                        if d == 'apps':
                            shutil.move(
                                os.path.join(app_folder, d, app),
                                os.path.join(path, 'apps')
                            )
                        elif d == 'templates':
                            shutil.move(
                                os.path.join(app_folder, d, app),
                                os.path.join(path, 'templates')
                            )

            except Exception as e:
                print "Error: {}".format(e)

        else:
            f = open(os.path.join(path, 'settings', 'base.py'))
            lines = f.readlines()
            f.close()

            f = open(os.path.join(path, 'settings', 'base.py'), "w")
            for line in lines:
                if line.strip() != '"{}.apps.{}",'.format(project, app):
                    f.write(line)

            f.close()

    print 'Removing temporary folder...'
    shutil.rmtree(temp_folder)


def main():
    args = parser.parse_args()
    dest_dir = args.dest_dir or args.project_name
    # Create the project.
    try:
        os.makedirs(dest_dir)
    except OSError:
        pass
    management.call_command(
        "startproject",
        args.project_name,
        dest_dir,
        template=os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "project_template")),
        extensions=("py", "txt", "conf", "gitignore", "md", "css", "js"),
        user=getpass.getuser(),
        project_slug=args.project_name.replace("_", "-"),
    )<commit_msg>Fixed a bug where when no apps were selected the start_cms_project.py would throw an error trying to remove a temp folder that didn't exist<commit_after>from django.core import management

import argparse
import getpass
import os
import os.path
import stat
import shutil
import subprocess
import sys


parser = argparse.ArgumentParser(
    description="Start a new CMS project.",
)

parser.add_argument(
    "project_name",
    help="The name of the project to create.",
)

parser.add_argument(
    "dest_dir",
    default=None,
    nargs="?",
    help="The destination dir for the created project.",
)

# parser.add_argument(
#     "--noinput",
#     action="store_false",
#     default=True,
#     dest="interactive",
#     help="Tells Django to NOT prompt the user for input of any kind.",
# )


def git(*args):
    return subprocess.check_call(['git'] + list(args))


def make_executable(path):
    st = os.stat(path)
    os.chmod(path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)


def query_yes_no(question, default="yes"):
    """Ask a yes/no question via raw_input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is one of "yes" or "no".
    """
    valid = {"yes": True, "y": True, "ye": True,
             "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        sys.stdout.write(question + prompt)
        choice = raw_input().lower()
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' "
                             "(or 'y' or 'n').\n")


def configure_apps(path, apps, project):

    # Check to make sure all app choices aren't False
    if any(apps.values()):
        print 'Creating temporary folder...'
        temp_folder = os.path.join(path, 'apps', 'temp')

        for app in apps:
            if apps[app]:
                try:
                    app_folder = os.path.join(temp_folder, app)

                    git(
                        "clone",
                        "<EMAIL>:onespacemedia/cms-{}.git".format(app),
                        app_folder
                    )

                    for src_dir, dirs, files in os.walk(app_folder):
                        for d in dirs:
                            if d == 'apps':
                                shutil.move(
                                    os.path.join(app_folder, d, app),
                                    os.path.join(path, 'apps')
                                )
                            elif d == 'templates':
                                shutil.move(
                                    os.path.join(app_folder, d, app),
                                    os.path.join(path, 'templates')
                                )

                except Exception as e:
                    print "Error: {}".format(e)

            else:
                f = open(os.path.join(path, 'settings', 'base.py'))
                lines = f.readlines()
                f.close()

                f = open(os.path.join(path, 'settings', 'base.py'), "w")
                for line in lines:
                    if line.strip() != '"{}.apps.{}",'.format(project, app):
                        f.write(line)

                f.close()

        print 'Removing temporary folder...'
        shutil.rmtree(temp_folder)

        print 'Remember to update each apps models.py with the correct urlconf'
    else:
        pass


def main():
    args = parser.parse_args()
    dest_dir = args.dest_dir or args.project_name
    # Create the project.
    try:
        os.makedirs(dest_dir)
    except OSError:
        pass
    management.call_command(
        "startproject",
        args.project_name,
        dest_dir,
        template=os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "project_template")),
        extensions=("py", "txt", "conf", "gitignore", "md", "css", "js"),
        user=getpass.getuser(),
        project_slug=args.project_name.replace("_", "-"),
    )
{'fraction_non_alphanumeric': 0.1239390768515289, 'fraction_numerical': 0.0, 'mean_word_length': 1.6906474820143884, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1180250', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2443, 'n_words': 697}