Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Add remove modification
Browse files Browse the repository at this point in the history
This modification can be used to remove files from the distgit repo:

```yaml
content:
  source:
    modifications:
    - action: remove
      glob: **/*.txt  # remove all .txt files from the distgit repo
```
  • Loading branch information
vfreex committed Jul 6, 2022
1 parent 3c510ea commit b450b7b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
38 changes: 38 additions & 0 deletions doozerlib/source_modifications.py
Expand Up @@ -4,6 +4,7 @@

import requests
import yaml
from pathlib import Path

from doozerlib.exceptions import DoozerFatalError
from doozerlib.exectools import cmd_assert
Expand Down Expand Up @@ -192,3 +193,40 @@ def act(self, *args, **kwargs):


SourceModifierFactory.MODIFICATIONS["command"] = CommandModifier


class RemoveModifier(object):
""" A source modifier that supports removing files from the distgit repository.
"""

def __init__(self, *args, **kwargs):
""" Initialize CommandModifier
:param command: a `str` or `list` of the command with arguments
"""
self.glob = kwargs["glob"]

def act(self, *args, **kwargs):
""" Run the command
:param context: A context dict. `context.set_env` is a `dict` of env vars to set for command (overriding existing).
"""
context = kwargs["context"]
component = context["component_name"]
distgit_path = Path(context['distgit_path'])
ceiling_dir = Path(kwargs["ceiling_dir"])

LOGGER.info("Distgit repo %s: Running 'remove' modification action...", component)
removed = []
for path in distgit_path.rglob(self.glob):
if not is_in_directory(path, ceiling_dir):
raise PermissionError("Removing a file from a location outside of directory {} is not allowed.".format(ceiling_dir))
relative_path = str(path.relative_to(distgit_path))
LOGGER.info("Distgit repo %s: Removing %s", component, relative_path)
path.unlink()
removed.append(relative_path)
if len(removed):
LOGGER.info("Distgit repo %s: %s files have been removed:\n%s", component, len(removed), "\n".join(removed))
else:
LOGGER.warning("Distgit repo %s: No files matching glob `%s`", component, self.glob)


SourceModifierFactory.MODIFICATIONS["remove"] = RemoveModifier
41 changes: 40 additions & 1 deletion tests/test_source_modifications.py
Expand Up @@ -8,7 +8,8 @@
import mock
import yaml.scanner

from doozerlib.source_modifications import AddModifier, SourceModifierFactory
from doozerlib.source_modifications import (AddModifier, RemoveModifier,
SourceModifierFactory)


class SourceModifierFactoryTestCase(unittest.TestCase):
Expand Down Expand Up @@ -90,5 +91,43 @@ def test_act_failed_with_invalid_yaml(self):
modifier.act(ceiling_dir=self.temp_dir, session=session, context=context)


class TestRemoveModifier(unittest.TestCase):
def test_remove_file(self):
distgit_path = pathlib.Path("/path/to/distgit")
modifier = RemoveModifier(glob="**/*.txt", distgit_path=distgit_path)
context = {
"component_name": "foo",
"kind": "Dockerfile",
"content": "whatever",
"distgit_path": distgit_path,
}
with mock.patch.object(pathlib.Path, "rglob") as rglob, mock.patch.object(pathlib.Path, "unlink") as unlink:
rglob.return_value = map(lambda path: distgit_path.joinpath(path), [
"1.txt",
"a/2.txt",
"b/c/d/e/3.txt",
])
modifier.act(context=context, ceiling_dir=str(distgit_path))
unlink.assert_called()

def test_remove_file_outside_of_distgit_dir(self):
distgit_path = pathlib.Path("/path/to/distgit")
modifier = RemoveModifier(glob="**/*.txt", distgit_path=distgit_path)
context = {
"component_name": "foo",
"kind": "Dockerfile",
"content": "whatever",
"distgit_path": distgit_path,
}
with mock.patch.object(pathlib.Path, "rglob") as rglob:
rglob.return_value = map(lambda path: pathlib.Path("/some/other/path").joinpath(path), [
"1.txt",
"a/2.txt",
"b/c/d/e/3.txt",
])
with self.assertRaises(PermissionError):
modifier.act(context=context, ceiling_dir=str(distgit_path))


if __name__ == "__main__":
unittest.main()

0 comments on commit b450b7b

Please sign in to comment.