def _check_file_ignored(file: str, gitignores=None):
        """
        Check if a file is ignored, going throw all provided gitignore files
        :param file:
        :param gitignores:
        :return:
        """
        if not gitignores:
            gitignores = UpdateGitignoreAction._find_gitignores(file)

        for gitignore in gitignores:
            block_lines, before_lines, after_lines = UpdateGitignoreAction._read_and_split_gitignore_file(
                gitignore)

            gitignore_content = before_lines + block_lines + after_lines

            inversed_gitignore_content = []
            for pattern in gitignore_content:
                if pattern.startswith("!"):
                    inversed_gitignore_content.append(pattern[1:])

            relative_file = UpdateGitignoreAction._get_relative_path(
                file, gitignore)

            zgitignore_helper = zgitignore.ZgitIgnore(gitignore_content)
            inversed_zgitignore_helper = zgitignore.ZgitIgnore(
                inversed_gitignore_content)
            if zgitignore_helper.is_ignored(
                    relative_file) or inversed_zgitignore_helper.is_ignored(
                        relative_file):
                return True, relative_file, gitignore, block_lines, before_lines, after_lines
        return False, None, None, None, None, None
def apply_diff_to_shell(shell: ShellIntegration,
                        source_environment: dict,
                        target_environment: dict,
                        envignore=None) -> Iterable[str]:
    """
    Compute and apply environment diff for given shell, returning a list of shell instruction to run.
    """
    variables = []

    for (action, source, dest_items) in diff(source_environment, target_environment):
        if action == 'add':
            for (key, value) in dest_items:
                variables.append((key, value))
        if action == 'change':
            variables.append((source, dest_items[1]))
        if action == 'remove':
            for (key, value) in dest_items:
                variables.append((key, None))

    envignore_helper = None
    if envignore:
        envignore_helper = zgitignore.ZgitIgnore(envignore)

    for key, value in sorted(variables, key=lambda variable: variable[0]):
        if not envignore_helper or not envignore_helper.is_ignored(key):
            if value is None:
                yield from shell.remove_environment_variable(key)
            else:
                yield from shell.set_environment_variable(key, value)
Esempio n. 3
0
def main():
    parser = argparse.ArgumentParser(description="Get all the required files")
    parser.add_argument(
        "--path",
        required=True,
        help="The docker path",
    )
    parser.add_argument(
        "--replace-pattern",
        nargs="*",
        default=["\\.mako$/", "\\.jinja$/"],
        help=
        "The replace pattern for interpreted files, default: '\\.mako$/ \\.jinja$/'",
    )
    options = parser.parse_args()

    # Should depends on the Dockerfile
    print(os.path.join(options.path, "Dockerfile"))

    docker_ignore_path = os.path.join(options.path, ".dockerignore")
    if os.path.isfile(docker_ignore_path):
        # Should depends on the Docker ignore file
        print(docker_ignore_path)

        with open(docker_ignore_path) as f:
            ignores = zgitignore.ZgitIgnore(f.read().splitlines(), docker=True)
    else:
        ignores = zgitignore.ZgitIgnore([])

    replaces = []
    for replace_pattern in options.replace_pattern:
        pattern, replacement = replace_pattern.split("/")
        replaces.append((re.compile(pattern), replacement))

    path = os.path.realpath(options.path) + "/"
    for dir_path, _, file_names in os.walk(path):
        for filename in file_names:
            for pattern, replacement in replaces:
                filename = pattern.sub(replacement, filename)
            file_path = os.path.join(dir_path, filename)
            if not ignores.is_ignored(file_path[len(path):],
                                      check_parents=True):
                print(file_path)
Esempio n. 4
0
def get_ignorer(path, additional_exclusions=None):
    """Create ignorer with directory gitignore file."""
    ignorefile = zgitignore.ZgitIgnore()
    gitignore_file = os.path.join(path, '.gitignore')
    if os.path.isfile(gitignore_file):
        with open(gitignore_file, 'r') as fileobj:
            ignorefile.add_patterns(fileobj.read().splitlines())

    if additional_exclusions is not None:
        ignorefile.add_patterns(additional_exclusions)

    return ignorefile