Example #1
0
    def test_get_project_files_gitignore(self):
        orig_cwd = os.getcwd()
        os.chdir(os.path.dirname(os.path.realpath(__file__)))
        os.makedirs("file_globs_gitignore_testfiles", exist_ok=True)
        os.chdir("file_globs_gitignore_testfiles")

        with open(".gitignore", "w") as f:
            f.write("""
# Start of gitignore
build
ignore.c
/tests
/upload.c
/*.py
*.pyc
__pycache__
# End of gitignore""")

        files = [os.path.join("src", "main.c"),
                 os.path.join("src", "main.h"),
                 os.path.join("src", "lib", "ssl.c"),
                 os.path.join("src", "tests", "main.c"),
                 os.path.join("src", "main.py"),
                 os.path.join("src", "upload.c"),
                 ".coafile"]
        ignored_files = [os.path.join("build", "main.c"),
                         os.path.join("tests", "run.c"),
                         os.path.join("src", "build", "main.c"),
                         "ignore.c",
                         os.path.join("src", "ignore.c"),
                         "globexp.py",
                         "upload.c",
                         os.path.join("src", "main.pyc"),
                         "run.pyc"]

        for file in files + ignored_files:
            os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True)
            open(file, "w").close()
        files += [".gitignore"]

        globs = list(get_gitignore_glob(os.getcwd()))
        returned_files = collect_files(
            [os.path.join(os.getcwd(), "**")],
            self.log_printer,
            ignored_file_paths=globs)
        files = [os.path.normcase(os.path.abspath(file)) for file in files]
        ignored_files = [os.path.abspath(file) for file in ignored_files]
        self.maxDiff = None
        self.assertEqual(sorted(files), sorted(returned_files))

        with suppress_stdout():
            self.assertEqual(
                sorted(get_project_files(self.log_printer,
                                         self.printer,
                                         os.getcwd(),
                                         self.file_path_completer)[0]),
                sorted(files))

        os.remove(".gitignore")
        os.chdir(orig_cwd)
def get_project_files(log_printer,
                      printer,
                      project_dir,
                      file_path_completer,
                      non_interactive=False):
    """
    Gets the list of files matching files in the user's project directory
    after prompting for glob expressions.

    :param log_printer:
        A ``LogPrinter`` object.
    :param printer:
        A ``ConsolePrinter`` object.
    :param file_path_completer:
        A ``file_path_completer`` object.
    :param non_interactive
        Whether coala-quickstart is in non-interactive mode
    :return:
        A list of file paths matching the files.
    """
    file_globs = ['**']

    ignore_globs = None
    if os.path.isfile(os.path.join(project_dir, '.gitignore')):
        printer.print(
            'The contents of your .gitignore file for the project '
            'will be automatically loaded as the files to ignore.',
            color='green')
        ignore_globs = get_gitignore_glob(project_dir)
    if non_interactive and not ignore_globs:
        ignore_globs = []

    if ignore_globs is None:
        printer.print(GLOB_HELP)
        file_path_completer.activate(seed_dir=project_dir)
        ignore_globs = ask_question(
            'Which files do you want coala to ignore inside the '
            'project directory?',
            printer=printer,
            typecast=list)
        file_path_completer.deactivate()
    printer.print()

    ignore_globs = list(ignore_globs)
    escaped_project_dir = glob_escape(project_dir)
    file_path_globs = [
        os.path.join(escaped_project_dir, glob_exp) for glob_exp in file_globs
    ]
    ignore_path_globs = [
        os.path.join(escaped_project_dir, glob_exp)
        for glob_exp in ignore_globs
    ]

    ignore_path_globs.append(os.path.join(escaped_project_dir, '.git/**'))

    file_paths = collect_files(file_path_globs,
                               log_printer,
                               ignored_file_paths=ignore_path_globs)

    return file_paths, ignore_globs
Example #3
0
def get_project_files(log_printer, printer, project_dir):
    """
    Gets the list of files matching files in the user's project directory
    after prompting for glob expressions.

    :param log_printer:
        A ``LogPrinter`` object.
    :param printer:
        A ``ConsolePrinter`` object.
    :return:
        A list of file paths matching the files.
    """
    file_globs = ["**"]

    ignore_globs = None
    if os.path.isfile(os.path.join(project_dir, ".gitignore")):
        printer.print(
            "The contents of your .gitignore file for the project "
            "will be automatically loaded as the files to ignore.",
            color="green")
        ignore_globs = get_gitignore_glob(project_dir)

    if ignore_globs is None:
        printer.print(GLOB_HELP)
        ignore_globs = ask_question("Which files do you want coala to ignore?",
                                    printer=printer,
                                    typecast=list)
    printer.print()

    escaped_project_dir = glob_escape(project_dir)
    file_path_globs = [
        os.path.join(escaped_project_dir, glob_exp) for glob_exp in file_globs
    ]
    ignore_path_globs = [
        os.path.join(escaped_project_dir, glob_exp)
        for glob_exp in ignore_globs
    ]

    ignore_path_globs.append(os.path.join(escaped_project_dir, ".git/**"))

    file_paths = collect_files(file_path_globs,
                               log_printer,
                               ignored_file_paths=ignore_path_globs)

    return file_paths, ignore_globs
def get_project_files(log_printer,
                      printer,
                      project_dir,
                      file_path_completer,
                      non_interactive=False):
    """
    Gets the list of files matching files in the user's project directory
    after prompting for glob expressions.

    :param log_printer:
        A ``LogPrinter`` object.
    :param printer:
        A ``ConsolePrinter`` object.
    :param file_path_completer:
        A ``file_path_completer`` object.
    :param non_interactive
        Whether coala-quickstart is in non-interactive mode
    :return:
        A list of file paths matching the files.
    """
    file_globs = ['**']

    ignore_globs = None
    gitignore_dir_list = []
    for dir_name, subdir_name, file_list in os.walk(project_dir):
        if os.path.isfile(os.path.join(dir_name, '.gitignore')):
            gitignore_dir_list += [dir_name]

    if gitignore_dir_list:
        printer.print('The contents of your .gitignore file for the project '
                      'will be automatically loaded as the files to ignore.',
                      color='green')
        ignore_globs = get_gitignore_glob(project_dir, gitignore_dir_list)

    if non_interactive and not ignore_globs:
        ignore_globs = []

    if ignore_globs is None:
        printer.print(GLOB_HELP)
        file_path_completer.activate(seed_dir=project_dir)
        ignore_globs = ask_question(
            'Which files do you want coala to ignore inside the '
            'project directory?',
            printer=printer,
            typecast=list)
        file_path_completer.deactivate()
    printer.print()

    ignore_globs = list(ignore_globs)
    escaped_project_dir = glob_escape(project_dir)
    file_path_globs = [os.path.join(
        escaped_project_dir, glob_exp) for glob_exp in file_globs]
    ignore_path_globs = [os.path.join(
        escaped_project_dir, glob_exp) for glob_exp in ignore_globs]

    ignore_path_globs.append(os.path.join(escaped_project_dir, '.git/**'))

    file_paths = collect_files(
        file_path_globs,
        log_printer,
        ignored_file_paths=ignore_path_globs)

    return file_paths, ignore_globs
    def test_get_project_files_gitignore(self):
        orig_cwd = os.getcwd()
        os.chdir(os.path.dirname(os.path.realpath(__file__)))
        os.makedirs("file_globs_gitignore_testfiles", exist_ok=True)
        os.chdir("file_globs_gitignore_testfiles")

        with open(".gitignore", "w") as f:
            f.write("""
# Start of gitignore
build
ignore.c
/tests
/upload.c
/*.py
*.pyc
__pycache__
# End of gitignore""")

        os.makedirs("another_folder", exist_ok=True)
        os.chdir("another_folder")
        with open('.gitignore', 'w') as f:
            f.write("""
# Start of gitignore
*.js
# End of gitignore""")
        os.chdir(os.path.dirname(os.path.realpath(__file__)))
        os.chdir("file_globs_gitignore_testfiles")
        os.makedirs("data", exist_ok=True)
        os.chdir("data")
        os.makedirs("sample", exist_ok=True)
        os.chdir("sample")
        with open('.gitignore', 'w') as f:
            f.write("""
# Start of gitignore
*.html
# End of gitignore""")
        os.chdir(os.path.dirname(os.path.realpath(__file__)))
        os.chdir("file_globs_gitignore_testfiles")
        files = [os.path.join("src", "main.c"),
                 os.path.join("src", "main.h"),
                 os.path.join("src", "lib", "ssl.c"),
                 os.path.join("src", "tests", "main.c"),
                 os.path.join("src", "main.py"),
                 os.path.join("src", "upload.c"),
                 os.path.join("another_folder", "another_file.c"),
                 os.path.join("data", "sample", "index.css"),
                 os.path.join("data", "example.py"),
                 ".coafile"]
        ignored_files = [os.path.join("build", "main.c"),
                         os.path.join("tests", "run.c"),
                         os.path.join("src", "build", "main.c"),
                         "ignore.c",
                         os.path.join("src", "ignore.c"),
                         "globexp.py",
                         "upload.c",
                         os.path.join("src", "main.pyc"),
                         os.path.join("another_folder", "script.js"),
                         os.path.join("data", "sample", "index.html"),
                         "run.pyc"]

        for file in files + ignored_files:
            os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True)
            open(file, "w").close()
        files += [".gitignore"]
        files += [os.path.join("another_folder", ".gitignore")]
        files += [os.path.join("data", "sample", ".gitignore")]

        gitignore_dir_list = [os.getcwd(),
                              os.path.join(os.getcwd(), "another_folder"),
                              os.path.join(os.getcwd(), "data", "sample")]
        globs = list(get_gitignore_glob(os.getcwd(), gitignore_dir_list))
        returned_files = collect_files(
            [os.path.join(os.getcwd(), "**")],
            self.log_printer,
            ignored_file_paths=globs)
        files = [os.path.normcase(os.path.abspath(file)) for file in files]
        ignored_files = [os.path.abspath(file) for file in ignored_files]
        self.maxDiff = None
        self.assertEqual(sorted(files), sorted(returned_files))

        with suppress_stdout():
            self.assertEqual(
                sorted(get_project_files(self.log_printer,
                                         self.printer,
                                         os.getcwd(),
                                         self.file_path_completer)[0]),
                sorted(files))

        os.remove(os.path.join("another_folder", ".gitignore"))
        os.remove(os.path.join("data", "sample", ".gitignore"))
        os.remove(".gitignore")
        os.chdir(orig_cwd)