<commit_before>import os.path
import sys
import cmd
from subprocess import call, check_output

csv_template = ["static content,your changes will be kept\n", "**********,**********\n", "autogenerated dynamic content,your changes will be deleted\n"]
index_template = ["The index will be auto-generated later when doing 'sr update'.\n\nAny changes made to this file manually will be overwritten.\n"]

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def create_components():
    print("Creating blank components directory.")

    # Create the components directory
    os.makedirs("./components")

    # Create the Python init file to make this a package
    index_file = open("./components/__init__.py", "w+")
    index_file.close()


def create_docs():
    print("Creating docs directory from template.")

    # Create the documentation directory
    os.makedirs("./docs")

    # Create a index.md file that will be autogenerated later
    index_file = open("./docs/index.md", "w+")
    index_file.writelines(index_template)
    index_file.close()


def create_readme():
    print("Creating README.md from template.")

    readme_template = ["# " + os.path.basename(os.path.normpath(os.getcwd())) + "\n\n", "## Introduction\n", "TODO: Write something about this project.\n\nDocumentation is [here](docs/index.md)\n\n", "## Contributing\n" "TODO: Add contributing guide.\n\n", "## License\n", "TODO: Add license information."]

    # Create the file and write a few template lines into it
    readme_file = open("./README.md", "w+")
    readme_file.writelines(readme_template)
    readme_file.close()


"""
Generates a BoM from scratch based on a template to show the user what they can enter manually, vs what is autogenerated
"""
def create_bom():
    print("Creating bill of materials (BoM) from template.")

    # Create the file and write a few template lines into it
    csv_file = open("./BoM.csv", "w+")
    csv_file.writelines(csv_template)
    csv_file.close()


"""
Initializes a directory as a Python package
"""
def init_python_package():
    print("Initializing directory as a Python package.")

    # Create an __init__.py file
    init_py = open("./__init__.py", "+w")
    init_py.close()


"""
Creates the directories and files that are required for the Sliderule project
"""
def initialize(git_init=True, url=None):
    print("Initializing current directory as a new Sliderule project.")
    print()

    # Check if the user has already initialized this directory as a git repo
    if os.path.exists("./.git"):
        print(bcolors.WARNING + "WARNING: This directory is already set up as a git repository. To change the remote, please do so directly with git commands." + bcolors.ENDC)
    else:
        if git_init:
            print("Initializing git repo.")

            if url == None:
                # The user will have to do the initial setup of their repos themselves
                main_repo = input("Enter URL of git repository: ")
            else:
                main_repo = url

            # Set up root git repo
            call(["git", "init"])
            call(["git", "remote", "add", "origin", main_repo])
            print("Done initializing root git repo")

    # If a BoM file doesn't already exist, create it
    if not os.path.isfile("./BoM.csv"):
        print()
        print("BoM does not exist.")

        create_bom()
    else:
        print()
        print(bcolors.WARNING + "WARNING: BoM.csv exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    if not os.path.isdir('./components'):
        print()
        print("components directory does not exist")

        create_components()
    else:
        print()
        print(bcolors.WARNING + "WARNING: components directory exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    # If a readme doesn't exist, create it
    if not os.path.isfile("./README.md"):
        print()
        print("README.md does not exist.")

        create_readme()
    else:
        print()
        print(bcolors.WARNING + "WARNING: README.md exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    # If the docs directory doesn't exist, created it and the index.md file
    if not os.path.isdir("./docs"):
        print()
        print("docs directory does not exist.")
        create_docs()
    else:
        print()
        print(bcolors.WARNING + "WARNING: docs directory exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    # If the directory has not been initialized as a Python package, create the init.py file
    if not os.path.isfile("./__init__.py"):
        print()
        print("The directory has not been initialized as a Python package.")

        init_python_package()

    print()
    print("Done initializing new Sliderule project.")


"""
Does a git clone step for the project at the given URL
"""
def clone(url):
    print("Recursively cloning Sliderule project from git.")

    call(["git", "clone", "--recurse-submodules", url])

    url_parts = url.split("/")
    dir_name = url_parts[len(url_parts) - 1].split('.')[0]

    os.chdir(dir_name)

    # Make sure that the project has all the latest Sliderule structure to it
    initialize(git_init=False)

"""
Does the git commit step for this project
"""
def commit(message=None):
    # Check git status to see if there are any changes to commit/push
    items = str(check_output(["git", "status"]))
    print(items)
    if "nothing to commit" not in items:
        print("Adding files to git.")
        call(["git", "add", "."])
        print("Creating commit with provided message.")

        # If a message has not been provided, prompt the user for one
        if message == None:
            message = input("Enter a message to go along with this commit: ")

        call(["git", "commit", "-m", message])
    else:
        print("Component does not need updated.")

    # push()


"""
Does the git push step for this project
"""
def push():
    print("Pushing to git.")
    call(["git", "push", "origin", "master"])


"""
Does a recursive git pull to grab all the changes to the root project 
and all its components.
"""
def pull():
    print("Pulling changes from git.")
    call(["git", "pull", "origin", "master"])
    call(["git", "submodule", "update", "--recursive", "--remote"])


"""
See what's changed
"""
def status():
    call(["git", "status"])


"""
Adds an existing component from git.
"""
def add_component(url):
    print("Adding existing component to this project.")

    component_names = url.split("/")
    component_name = component_names[len(component_names) - 1].split('.')[0]
    
    # Check to see if the component already exists
    if os._exists("./components/" + component_name):
        print("Component already exists.")
        return

    # os.mkdir("./components/" + component_name)
    os.chdir("./components/")

    clone(url)

    # call(["git", "subtree", "add", "--prefix", "./components/" + component_name, url, "master", "--squash"])

    # Make sure the root project and all the submodules are updated
    # pull()

    initialize(git_init=False)

    os.chdir("../../")


"""
Creates a new component.
"""
def create_component(url):
    print("Creating a new component.")

    component_names = url.split("/")
    component_name = component_names[len(component_names) - 1].split('.')[0]

    # Check to see if the component already exists
    if os._exists("./components/" + component_name):
        print("Component already exists.")
        return

    # Set up the component directory
    os.mkdir("./components/" + component_name)
    os.chdir("./components/" + component_name)

    initialize(git_init=True, url=url)

    # Push or initial changes to the server
    # commit("Initial commit")
    # push()

    # Change back to the root directory
    os.chdir("../../")


"""
Remove a git submodule
"""<commit_msg>Added a warning about using dashes in component package names.<commit_after>import os.path
import sys
import cmd
from subprocess import call, check_output

csv_template = ["static content,your changes will be kept\n", "**********,**********\n", "autogenerated dynamic content,your changes will be deleted\n"]
index_template = ["The index will be auto-generated later when doing 'sr update'.\n\nAny changes made to this file manually will be overwritten.\n"]

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def create_components():
    print("Creating blank components directory.")

    # Create the components directory
    os.makedirs("./components")

    # Create the Python init file to make this a package
    index_file = open("./components/__init__.py", "w+")
    index_file.close()


def create_docs():
    print("Creating docs directory from template.")

    # Create the documentation directory
    os.makedirs("./docs")

    # Create a index.md file that will be autogenerated later
    index_file = open("./docs/index.md", "w+")
    index_file.writelines(index_template)
    index_file.close()


def create_readme():
    print("Creating README.md from template.")

    readme_template = ["# " + os.path.basename(os.path.normpath(os.getcwd())) + "\n\n", "## Introduction\n", "TODO: Write something about this project.\n\nDocumentation is [here](docs/index.md)\n\n", "## Contributing\n" "TODO: Add contributing guide.\n\n", "## License\n", "TODO: Add license information."]

    # Create the file and write a few template lines into it
    readme_file = open("./README.md", "w+")
    readme_file.writelines(readme_template)
    readme_file.close()


"""
Generates a BoM from scratch based on a template to show the user what they can enter manually, vs what is autogenerated
"""
def create_bom():
    print("Creating bill of materials (BoM) from template.")

    # Create the file and write a few template lines into it
    csv_file = open("./BoM.csv", "w+")
    csv_file.writelines(csv_template)
    csv_file.close()


"""
Initializes a directory as a Python package
"""
def init_python_package():
    print("Initializing directory as a Python package.")

    # Create an __init__.py file
    init_py = open("./__init__.py", "+w")
    init_py.close()


"""
Creates the directories and files that are required for the Sliderule project
"""
def initialize(git_init=True, url=None):
    print("Initializing current directory as a new Sliderule project.")
    print()

    # Check if the user has already initialized this directory as a git repo
    if os.path.exists("./.git"):
        print(bcolors.WARNING + "WARNING: This directory is already set up as a git repository. To change the remote, please do so directly with git commands." + bcolors.ENDC)
    else:
        if git_init:
            print("Initializing git repo.")

            if url == None:
                # The user will have to do the initial setup of their repos themselves
                main_repo = input("Enter URL of git repository: ")
            else:
                main_repo = url

            # Set up root git repo
            call(["git", "init"])
            call(["git", "remote", "add", "origin", main_repo])
            print("Done initializing root git repo")

    # If a BoM file doesn't already exist, create it
    if not os.path.isfile("./BoM.csv"):
        print()
        print("BoM does not exist.")

        create_bom()
    else:
        print()
        print(bcolors.WARNING + "WARNING: BoM.csv exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    if not os.path.isdir('./components'):
        print()
        print("components directory does not exist")

        create_components()
    else:
        print()
        print(bcolors.WARNING + "WARNING: components directory exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    # If a readme doesn't exist, create it
    if not os.path.isfile("./README.md"):
        print()
        print("README.md does not exist.")

        create_readme()
    else:
        print()
        print(bcolors.WARNING + "WARNING: README.md exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    # If the docs directory doesn't exist, created it and the index.md file
    if not os.path.isdir("./docs"):
        print()
        print("docs directory does not exist.")
        create_docs()
    else:
        print()
        print(bcolors.WARNING + "WARNING: docs directory exists, refusing to overwrite to prevent loss of data." + bcolors.ENDC)

    # If the directory has not been initialized as a Python package, create the init.py file
    if not os.path.isfile("./__init__.py"):
        print()
        print("The directory has not been initialized as a Python package.")

        init_python_package()

    print()
    print("Done initializing new Sliderule project.")


"""
Does a git clone step for the project at the given URL
"""
def clone(url):
    print("Recursively cloning Sliderule project from git.")

    call(["git", "clone", "--recurse-submodules", url])

    url_parts = url.split("/")
    dir_name = url_parts[len(url_parts) - 1].split('.')[0]

    os.chdir(dir_name)

    # Make sure that the project has all the latest Sliderule structure to it
    initialize(git_init=False)

"""
Does the git commit step for this project
"""
def commit(message=None):
    # Check git status to see if there are any changes to commit/push
    items = str(check_output(["git", "status"]))
    print(items)
    if "nothing to commit" not in items:
        print("Adding files to git.")
        call(["git", "add", "."])
        print("Creating commit with provided message.")

        # If a message has not been provided, prompt the user for one
        if message == None:
            message = input("Enter a message to go along with this commit: ")

        call(["git", "commit", "-m", message])
    else:
        print("Component does not need updated.")

    # push()


"""
Does the git push step for this project
"""
def push():
    print("Pushing to git.")
    call(["git", "push", "origin", "master"])


"""
Does a recursive git pull to grab all the changes to the root project 
and all its components.
"""
def pull():
    print("Pulling changes from git.")
    call(["git", "pull", "origin", "master"])
    call(["git", "submodule", "update", "--recursive", "--remote"])


"""
See what's changed
"""
def status():
    call(["git", "status"])


"""
Adds an existing component from git.
"""
def add_component(url):
    print("Adding existing component to this project.")

    component_names = url.split("/")
    component_name = component_names[len(component_names) - 1].split('.')[0]
    
    # Check to see if the component already exists
    if os._exists("./components/" + component_name):
        print("Component already exists.")
        return

    # os.mkdir("./components/" + component_name)
    os.chdir("./components/")

    clone(url)

    # call(["git", "subtree", "add", "--prefix", "./components/" + component_name, url, "master", "--squash"])

    # Make sure the root project and all the submodules are updated
    # pull()

    initialize(git_init=False)

    os.chdir("../../")


"""
Creates a new component.
"""
def create_component(url):
    print("Creating a new component.")

    component_names = url.split("/")
    component_name = component_names[len(component_names) - 1].split('.')[0]

    # Warn the user if the component name has dashes in it, which could break things like the Python naming conventions
    if "-" in component_name:
        print(bcolors.WARNING + "WARNING: The component name has dashes in it, which can break some package naming conventions (Python)." + bcolors.ENDC)

    # Check to see if the component already exists
    if os._exists("./components/" + component_name):
        print("Component already exists.")
        return

    # Set up the component directory
    os.mkdir("./components/" + component_name)
    os.chdir("./components/" + component_name)

    initialize(git_init=True, url=url)

    # Push or initial changes to the server
    # commit("Initial commit")
    # push()

    # Change back to the root directory
    os.chdir("../../")


"""
Remove a git submodule
"""
{'fraction_non_alphanumeric': 0.13002306665047955, 'fraction_numerical': 0.005220347213791429, 'mean_word_length': 2.947999041456985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2856107', 'n_tokens_mistral': 4914, 'n_tokens_neox': 4668, 'n_words': 1666}