Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/softprops/action…
Browse files Browse the repository at this point in the history
…-gh-release-2
  • Loading branch information
ademariag authored Jul 8, 2024
2 parents 89c46f2 + 14e0fcb commit 2d58efe
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.10', '3.11']
python-version: ['3.10', '3.11', '3.12']
steps:
- name: Checkout kapitan recursively
uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def build_parser():
compile_parser.add_argument(
"--reveal",
help="reveal refs (warning: this will potentially write sensitive data)",
action="store_true",
action=argparse.BooleanOptionalAction,
default=from_dot_kapitan("compile", "reveal", False),
)
compile_parser.add_argument(
Expand Down Expand Up @@ -662,8 +662,8 @@ def main():
parser.print_help()
sys.exit(1)

cached.args = args
cached.args = args

if hasattr(args, "verbose") and args.verbose:
logging_level = logging.DEBUG
elif hasattr(args, "quiet") and args.quiet:
Expand Down
6 changes: 3 additions & 3 deletions kapitan/dependency_manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import multiprocessing
import os
from collections import defaultdict, namedtuple
from distutils.dir_util import copy_tree
from functools import partial
from mimetypes import MimeTypes
from shutil import copyfile, rmtree
Expand All @@ -17,6 +16,7 @@
from kapitan.errors import GitSubdirNotFoundError, GitFetchingError, HelmFetchingError
from kapitan.helm_cli import helm_cli
from kapitan.utils import (
copy_tree,
make_request,
unpack_downloaded_file,
safe_copy_tree,
Expand Down Expand Up @@ -129,7 +129,7 @@ def fetch_git_dependency(dep_mapping, save_dir, force, item_type="Dependency"):
"{} {}: subdir {} not found in repo".format(item_type, source, sub_dir)
)
if force:
copied = copy_tree(copy_src_path, output_path, verbose=0)
copied = copy_tree(copy_src_path, output_path)
else:
copied = safe_copy_tree(copy_src_path, output_path)
if copied:
Expand Down Expand Up @@ -245,7 +245,7 @@ def fetch_helm_chart(dep_mapping, save_dir, force):
os.makedirs(parent_dir, exist_ok=True)

if force:
copied = copy_tree(cached_repo_path, output_path, verbose=0)
copied = copy_tree(cached_repo_path, output_path)
else:
copied = safe_copy_tree(cached_repo_path, output_path)

Expand Down
2 changes: 1 addition & 1 deletion kapitan/initialiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import logging
import os
from distutils.dir_util import copy_tree
from kapitan.utils import copy_tree

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion kapitan/inputs/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import os
import shutil
from distutils.dir_util import copy_tree
from kapitan.utils import copy_tree

from kapitan.inputs.base import InputType

Expand Down
2 changes: 1 addition & 1 deletion kapitan/inputs/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import os
import shutil
from distutils.dir_util import copy_tree
from kapitan.utils import copy_tree

from kapitan.inputs.base import InputType

Expand Down
2 changes: 1 addition & 1 deletion kapitan/inputs/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import os
import shutil
from distutils.dir_util import copy_tree
from kapitan.utils import copy_tree

from kapitan.inputs.base import InputType

Expand Down
44 changes: 30 additions & 14 deletions kapitan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
from __future__ import print_function

import collections
import glob
import json
import logging
import magic
import math
import os
import re
import shutil
import stat
import sys
import tarfile
import traceback
from collections import Counter, defaultdict
from distutils.dir_util import mkpath
from distutils.errors import DistutilsFileError
from distutils.file_util import _copy_file_contents
from functools import lru_cache, wraps
from hashlib import sha256
from zipfile import ZipFile
Expand Down Expand Up @@ -560,19 +559,23 @@ def unpack_downloaded_file(file_path, output_path, content_type):
return is_unpacked


class SafeCopyError(Exception):
"""Raised when a file or directory cannot be safely copied."""


def safe_copy_file(src, dst):
"""Copy a file from 'src' to 'dst'.
Similar to distutils.file_util.copy_file except
Similar to shutil.copyfile except
if the file exists in 'dst' it's not clobbered
or overwritten.
returns a tupple (src, val)
returns a tuple (src, val)
file not copied if val = 0 else 1
"""

if not os.path.isfile(src):
raise DistutilsFileError("Can't copy {}: doesn't exist or is not a regular file".format(src))
raise SafeCopyError("Can't copy {}: doesn't exist or is not a regular file".format(src))

if os.path.isdir(dst):
dir = dst
Expand All @@ -583,29 +586,31 @@ def safe_copy_file(src, dst):
if os.path.isfile(dst):
logger.debug("Not updating %s (file already exists)", dst)
return (dst, 0)
_copy_file_contents(src, dst)
shutil.copyfile(src, dst)
logger.debug("Copied %s to %s", src, dir)
return (dst, 1)


def safe_copy_tree(src, dst):
"""Recursively copies the 'src' directory tree to 'dst'
Both 'src' and 'dst' must be directories.
similar to distutil.dir_util.copy_tree except
it doesn't overwite an existing file and doesn't
copy any file starting with "."
Both 'src' and 'dst' must be directories. Similar to copy_tree except
it doesn't overwite an existing file and doesn't copy any file starting
with "."
Returns a list of copied file paths.
"""
if not os.path.isdir(src):
raise DistutilsFileError("Cannot copy tree {}: not a directory".format(src))
raise SafeCopyError("Cannot copy tree {}: not a directory".format(src))
try:
names = os.listdir(src)
except OSError as e:
raise DistutilsFileError("Error listing files in {}: {}".format(src, e.strerror))
raise SafeCopyError("Error listing files in {}: {}".format(src, e.strerror))

mkpath(dst)
try:
os.makedirs(dst, exist_ok=True)
except FileExistsError:
pass
outputs = []

for name in names:
Expand All @@ -624,3 +629,14 @@ def safe_copy_tree(src, dst):
outputs.append(dst_name)

return outputs


def copy_tree(src, dst):
"""Recursively copy a given directory from `src` to `dst`.
Returns a list of the copied files.
"""
before = set(glob.iglob("*", recursive=True))
shutil.copytree(src, dst, dirs_exist_ok=True)
after = set(glob.iglob("*", recursive=True))
return list(after - before)
97 changes: 49 additions & 48 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d58efe

Please sign in to comment.