Skip to content

Commit

Permalink
envoy.base.utils: Add utils.is_tarlike (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax authored Aug 24, 2021
1 parent 02975eb commit 95115e5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion envoy.base.utils/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2-dev
0.0.2
19 changes: 18 additions & 1 deletion envoy.base.utils/envoy/base/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
from configparser import ConfigParser
from contextlib import (
ExitStack, contextmanager, redirect_stderr, redirect_stdout)
from typing import Callable, Iterator, List, Optional, Union
from typing import Callable, Iterator, List, Optional, Set, Union

import yaml


# See here for a list of known tar file extensions:
# https://en.wikipedia.org/wiki/Tar_(computing)#Suffixes_for_compressed_files
# not all are listed here, and some extensions may require additional software
# to handle. This list can be updated as required
TAR_EXTS: Set[str] = {"tar", "tar.gz", "tar.xz", "tar.bz2"}


class ExtractError(Exception):
pass

Expand Down Expand Up @@ -136,3 +143,13 @@ def to_yaml(
path = pathlib.Path(path)
path.write_text(yaml.dump(data))
return path


def is_tarlike(path: Union[pathlib.Path, str]) -> bool:
"""Returns a bool based on whether a file looks like a tar file depending
on its file extension.
This allows for a provided path to save to, to dynamically be either
considered a directory (to create) or a tar file (to create).
"""
return any(str(path).endswith(ext) for ext in TAR_EXTS)
12 changes: 12 additions & 0 deletions envoy.base.utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,15 @@ def test_util_to_yaml(patches):
assert (
list(m_plib.Path.call_args)
== [("PATH", ), {}])


@pytest.mark.parametrize(
"path",
["x.foo", "x.bar", "x.tar", "x.tar.xz", "x.xz"])
def test_is_tarlike(patches, path):
matches = False
for ext in utils.TAR_EXTS:
if path.endswith(ext):
matches = True
break
assert utils.is_tarlike(path) == matches

0 comments on commit 95115e5

Please sign in to comment.