Skip to content

Commit

Permalink
Added --files-only option to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Jul 11, 2020
1 parent f80922f commit 8192816
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
19 changes: 18 additions & 1 deletion osxmetadata/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ def get_help(self, ctx):
multiple=False,
required=False,
)
FILES_ONLY_OPTION = click.option(
"--files-only",
help="Do not apply metadata commands to directories themselves, only files in a directory.",
is_flag=True,
default=False,
required=False,
)


@click.command(cls=MyClickCommand)
Expand All @@ -289,6 +296,7 @@ def get_help(self, ctx):
@RESTORE_OPTION
@VERBOSE_OPTION
@COPY_FROM_OPTION
@FILES_ONLY_OPTION
@click.pass_context
def cli(
ctx,
Expand All @@ -310,6 +318,7 @@ def cli(
restore,
verbose,
copyfrom,
files_only,
):
""" Read/write metadata from file(s). """

Expand Down Expand Up @@ -412,6 +421,7 @@ def cli(
backup,
restore,
walk,
files_only,
)

if walk and os.path.isdir(filename):
Expand All @@ -434,7 +444,8 @@ def cli(
copyfrom,
backup,
restore,
walk
walk,
files_only,
)


Expand All @@ -456,6 +467,7 @@ def process_files(
backup,
restore,
walk,
files_only,
):
""" process list of files, calls process_single_file to process each file
options processed in this order: wipe, copyfrom, clear, set, append, remove, mirror, get, list
Expand All @@ -465,6 +477,11 @@ def process_files(
fpath = pathlib.Path(filename).resolve()
backup_file = pathlib.Path(pathlib.Path(filename).parent) / _BACKUP_FILENAME

if files_only and fpath.is_dir():
if verbose:
click.echo(f"Skipping directory: {fpath}")
continue

if verbose:
click.echo(f"Processing file: {fpath}")

Expand Down
2 changes: 1 addition & 1 deletion osxmetadata/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.99.7"
__version__ = "0.99.8"
98 changes: 98 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
]


def create_file(filepath):
""" create an empty file at filepath """
fd = open(filepath, "w+")
fd.close()


@pytest.fixture(params=["file", "dir"])
def temp_file(request):

Expand All @@ -66,6 +72,18 @@ def temp_file(request):
tempdir.cleanup()


@pytest.fixture
def temp_dir():
# TESTDIR for temporary files usually defaults to "/tmp",
# which may not have XATTR support (e.g. tmpfs);
# manual override here.
TESTDIR = None
tempdir = TemporaryDirectory(dir=TESTDIR)
tempdirname = tempdir.name
yield tempdirname
tempdir.cleanup()


def parse_cli_output(output):
""" helper for testing
parse the CLI --list output and return value of all set attributes as dict """
Expand Down Expand Up @@ -773,3 +791,83 @@ def test_cli_downloadeddate(temp_file):
meta.tz_aware = True
assert meta.get_attribute(kMDItemDownloadedDate) == [utc_time]
assert meta.downloadeddate == [utc_time]


def test_cli_walk(temp_dir):
""" test --walk """
import os
import pathlib
from osxmetadata import OSXMetaData, Tag
from osxmetadata.__main__ import cli

dirname = pathlib.Path(temp_dir)
os.makedirs(dirname / "temp" / "subfolder1")
os.makedirs(dirname / "temp" / "subfolder2")
create_file(dirname / "temp" / "temp1.txt")
create_file(dirname / "temp" / "subfolder1" / "sub1.txt")

runner = CliRunner()
result = runner.invoke(cli, ["--set", "tags", "FOO", "--walk", temp_dir])
assert result.exit_code == 0

md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.txt")
assert md.tags == [Tag("FOO")]

md = OSXMetaData(dirname / "temp" / "subfolder2")
assert md.tags == [Tag("FOO")]


def test_cli_walk_files_only(temp_dir):
""" test --walk with --files-only """
import os
import pathlib
from osxmetadata import OSXMetaData, Tag
from osxmetadata.__main__ import cli

dirname = pathlib.Path(temp_dir)
os.makedirs(dirname / "temp" / "subfolder1")
os.makedirs(dirname / "temp" / "subfolder2")
create_file(dirname / "temp" / "temp1.txt")
create_file(dirname / "temp" / "subfolder1" / "sub1.txt")

runner = CliRunner()
result = runner.invoke(
cli, ["--set", "tags", "FOO", "--walk", "--files-only", temp_dir]
)
assert result.exit_code == 0

md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.txt")
assert md.tags == [Tag("FOO")]

md = OSXMetaData(dirname / "temp" / "subfolder2")
assert not md.tags


def test_cli_files_only(temp_dir):
""" test --files-only without --walk """
import glob
import os
import pathlib
from osxmetadata import OSXMetaData, Tag
from osxmetadata.__main__ import cli

dirname = pathlib.Path(temp_dir)
os.makedirs(dirname / "temp" / "subfolder1")
os.makedirs(dirname / "temp" / "subfolder2")
create_file(dirname / "temp" / "temp1.txt")
create_file(dirname / "temp" / "subfolder1" / "sub1.txt")

files = glob.glob(str(dirname / "temp" / "*"))

runner = CliRunner()
result = runner.invoke(cli, ["--set", "tags", "FOO", "--files-only", *files])
assert result.exit_code == 0

md = OSXMetaData(dirname / "temp" / "temp1.txt")
assert md.tags == [Tag("FOO")]

md = OSXMetaData(dirname / "temp" / "subfolder1")
assert not md.tags

md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.txt")
assert not md.tags

0 comments on commit 8192816

Please sign in to comment.