Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
#27: Implement additional switches to JINJA2; --exclude-pattern and -…
Browse files Browse the repository at this point in the history
…-copy-not-matching-files
  • Loading branch information
blackandred committed Jul 16, 2020
1 parent 19e5f7d commit ff6b040
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/rkd/standardlib/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def execute(self, context: ExecutionContext) -> bool:
target_root = context.get_arg('--target')
delete_source_files = context.get_arg('--delete-source-files')
pattern = re.compile(context.get_arg('--pattern'))
exclude_pattern = re.compile(context.get_arg('--exclude-pattern')) if context.get_arg('--exclude-pattern') else None
copy_not_matched = context.get_arg('--copy-not-matching-files')

self.io().info_msg('Pattern is `%s`' % context.get_arg('--pattern'))

Expand All @@ -83,8 +85,18 @@ def execute(self, context: ExecutionContext) -> bool:
if target_full_path.endswith('.j2'):
target_full_path = target_full_path[:-3]

if exclude_pattern and self._is_file_matching_filter(exclude_pattern, source_full_path):
self.io().info_msg('Skipping file "%s" - (filtered out by --exclude-pattern)' % source_full_path)
continue

if not self._is_file_matching_filter(pattern, source_full_path):
self.io().info_msg('Skipping file "%s" (filtered out)' % source_full_path)
if copy_not_matched:
self.io().info_msg('Copying "%s" regular file' % source_full_path)
self._copy_file(source_full_path, target_full_path)

continue

self.io().info_msg('Skipping file "%s" (filtered out by --pattern)' % source_full_path)
continue

self.io().info_msg('Rendering file "%s" into "%s"' % (source_full_path, target_full_path))
Expand All @@ -98,6 +110,10 @@ def execute(self, context: ExecutionContext) -> bool:

return True

def _copy_file(self, source_full_path: str, target_full_path: str):
self.sh('mkdir -p "%s"' % os.path.dirname(target_full_path))
self.sh('cp -p "%s" "%s"' % (source_full_path, target_full_path))

def _render(self, source_path: str, target_path: str) -> bool:
try:
self.rkd([':j2:render', '--source="%s"' % source_path, '--output="%s"' % target_path], verbose=True)
Expand All @@ -121,6 +137,9 @@ def configure_argparse(self, parser: ArgumentParser):
action='store_true')
parser.add_argument('--pattern', '-p', help='Optional regexp pattern to match full paths',
default='(.*).j2')
parser.add_argument('--exclude-pattern', '-xp', help='Optional regexp for a pattern exclude, to exclude files')
parser.add_argument('--copy-not-matching-files', '-c', help='Copy all files that are not matching the pattern' +
' instead of skipping them')


def imports():
Expand Down
32 changes: 31 additions & 1 deletion test/test_standardlib_jinja_render_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ def _execute_mocked_task(params: dict) -> tuple:
calls = []
deletions = []

# mocks _render() method
def mock__render(source_path: str, target_path: str) -> bool:
calls.append('"%s" -> "%s"' % (source_path, target_path))

return True

def mock__sh(*args, **kwargs):
calls.append('sh(' + ' '.join(args) + ')')

task._render = mock__render
task.sh = mock__sh
task._delete_file = lambda file: deletions.append(file)

# run task
Expand Down Expand Up @@ -52,6 +55,33 @@ def test_iterates_over_subdirectories_including_depth_and_pattern(self):
self.assertNotIn('"../src/" -> "/tmp/src/"', renderings)
self.assertNotIn('"../src" -> "/tmp/src"', renderings)

def test_files_are_copied_when_not_matching_pattern_but_switch_was_used(self):
"""Test --copy-not-matching-files switch that adds a possibility to copy all files from SOURCE to DESTINATION
The difference is that those files that does not match PATTERN will be copied without rendering.
Additionally uses "--exclude-pattern" to exclude redundant files
"""

renderings, deletions = self._execute_mocked_task({
'source': '../test',
'target': '/tmp',
'delete_source_files': False,
'pattern': '(.*).j2',
'--exclude-pattern': '(.*).pyc',
'--copy-not-matching-files': True
})

flatten_list_as_str = ' '.join(renderings)

with self.subTest('Check --copy-not-matching-files - the non (.*).j2 files should be just copied ' +
'instead of rendered'):

self.assertIn('sh(cp -p "../test/test_standardlib_jinja_render_directory.py" ' +
'"/tmp//test_standardlib_jinja_render_directory.py")', renderings)

with self.subTest('Check --exclude-pattern'):
self.assertNotIn('.pyc', flatten_list_as_str)

def test_without_pattern(self):
"""What happens if we specify no pattern?
"""
Expand Down

0 comments on commit ff6b040

Please sign in to comment.