Beispiel #1
0
def _archive_file_recursively(tar, name, arcname, parent_ignored,
                              parent_matching_rule_index, ignore_check):
    # create a TarInfo object from the file
    tarinfo = tar.gettarinfo(name, arcname)

    if tarinfo is None:
        raise CLIError("tarfile: unsupported type {}".format(name))

    # check if the file/dir is ignored
    ignored, matching_rule_index = ignore_check(tarinfo, parent_ignored,
                                                parent_matching_rule_index)

    if not ignored:
        # append the tar header and data to the archive
        if tarinfo.isreg():
            with bltn_open(name, "rb") as f:
                tar.addfile(tarinfo, f)
        else:
            tar.addfile(tarinfo)

    # even the dir is ignored, its child items can still be included, so continue to scan
    if tarinfo.isdir():
        for f in os.listdir(name):
            _archive_file_recursively(
                tar,
                os.path.join(name, f),
                os.path.join(arcname, f),
                parent_ignored=ignored,
                parent_matching_rule_index=matching_rule_index,
                ignore_check=ignore_check)
Beispiel #2
0
    def add(tarfile, name, arcname, recursive=True):
        if arcname is None:
            arcname = name

        tarinfo = tarfile.gettarinfo(name, arcname)

        if tarinfo is None:
            return

        tarinfo.uid = 0
        tarinfo.gid = 0
        tarinfo.uname = ''
        tarinfo.gname = ''

        # Append the tar header and data to the archive.
        if tarinfo.isreg():
            with bltn_open(name, "rb") as f:
                tarfile.addfile(tarinfo, f)
        elif tarinfo.isdir():
            tarfile.addfile(tarinfo)
            if recursive:
                for f in sorted(listdir(name)):
                    SafeTar.add(tarfile, joinpath(name, f),
                                joinpath(arcname, f), recursive)
        else:
            tarfile.addfile(tarinfo)
Beispiel #3
0
def _archive_file_recursively(tar, name, arcname, parent_ignored, parent_matching_rule_index, ignore_check):
    # create a TarInfo object from the file
    tarinfo = tar.gettarinfo(name, arcname)

    if tarinfo is None:
        raise CLIError("tarfile: unsupported type {}".format(name))

    # check if the file/dir is ignored
    ignored, matching_rule_index = ignore_check(
        tarinfo, parent_ignored, parent_matching_rule_index)

    if not ignored:
        # append the tar header and data to the archive
        if tarinfo.isreg():
            with bltn_open(name, "rb") as f:
                tar.addfile(tarinfo, f)
        else:
            tar.addfile(tarinfo)

    # even the dir is ignored, its child items can still be included, so continue to scan
    if tarinfo.isdir():
        for f in os.listdir(name):
            _archive_file_recursively(tar, os.path.join(name, f), os.path.join(arcname, f),
                                      parent_ignored=ignored, parent_matching_rule_index=matching_rule_index,
                                      ignore_check=ignore_check)
Beispiel #4
0
def _pack_source_code(source_location, tar_file_path, docker_file_path, docker_file_in_tar):
    logger.warning("Packing source code into tar to upload...")

    ignore_list, ignore_list_size = _load_dockerignore_file(source_location)
    common_vcs_ignore_list = {'.git', '.gitignore', '.bzr', 'bzrignore', '.hg', '.hgignore', '.svn'}

    def _ignore_check(tarinfo, parent_ignored, parent_matching_rule_index):
        # ignore common vcs dir or file
        if tarinfo.name in common_vcs_ignore_list:
            logger.warning("Excluding '%s' based on default ignore rules", tarinfo.name)
            return True, parent_matching_rule_index

        if ignore_list is None:
            # if .dockerignore doesn't exists, inherit from parent
            # eg, it will ignore the files under .git folder.
            return parent_ignored, parent_matching_rule_index

        for index, item in enumerate(ignore_list):
            # stop checking the remaining rules whose priorities are lower than the parent matching rule
            # at this point, current item should just inherit from parent
            if index >= parent_matching_rule_index:
                break
            if re.match(item.pattern, tarinfo.name):
                logger.debug(".dockerignore: rule '%s' matches '%s'.",
                             item.rule, tarinfo.name)
                return item.ignore, index

        logger.debug(".dockerignore: no rule for '%s'. parent ignore '%s'",
                     tarinfo.name, parent_ignored)
        # inherit from parent
        return parent_ignored, parent_matching_rule_index

    with tarfile.open(tar_file_path, "w:gz") as tar:
        # need to set arcname to empty string as the archive root path
        _archive_file_recursively(tar,
                                  source_location,
                                  arcname="",
                                  parent_ignored=False,
                                  parent_matching_rule_index=ignore_list_size,
                                  ignore_check=_ignore_check)

        # Add the Dockerfile if it's specified.
        # In the case of run, there will be no Dockerfile.
        if docker_file_path:
            docker_file_tarinfo = tar.gettarinfo(
                docker_file_path, docker_file_in_tar)
            with bltn_open(docker_file_path, "rb") as f:
                tar.addfile(docker_file_tarinfo, f)
Beispiel #5
0
def file_contents1(filepath):
    with bltn_open(filepath) as f:
        return f.read()