-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ben Einaudi
committed
Jul 11, 2019
1 parent
ec8343d
commit fa479de
Showing
4 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import fnmatch | ||
import logging | ||
import os | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class CfIgnore(object): | ||
def __init__(self, application_path): | ||
ignore_file_path = os.path.join(application_path, '.cfignore') | ||
self.ignore_items = [] | ||
if os.path.isfile(ignore_file_path): | ||
with open(ignore_file_path, 'r') as ignore_file: | ||
for line in ignore_file.readlines(): | ||
self.ignore_items.extend(self._pattern(line.strip(' \n'))) | ||
|
||
def is_entry_ignored(self, relative_file): | ||
def is_relative_file_ignored(cf_ignore_entry): | ||
_logger.debug('is_relative_file_ignored - %s - %s', cf_ignore_entry, relative_file) | ||
file_path = '/%s' % relative_file \ | ||
if cf_ignore_entry.startswith('/') and not relative_file.startswith('/') \ | ||
else relative_file | ||
return fnmatch.fnmatch(file_path, cf_ignore_entry) | ||
|
||
return any([is_relative_file_ignored(ignore_item) | ||
for ignore_item in self.ignore_items]) | ||
|
||
@staticmethod | ||
def _pattern(pattern): | ||
if pattern.find('/') < 0: | ||
return [pattern, os.path.join('**', pattern)] | ||
elif pattern.endswith('/'): | ||
return [os.path.join('/', pattern, '*'), | ||
os.path.join('/', pattern, '**', '*'), | ||
os.path.join('/', '**', pattern, '*'), | ||
os.path.join('/', '**', pattern, '**', '*')] | ||
else: | ||
return [os.path.join('/', pattern), os.path.join('/', '**', pattern)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
from unittest import TestCase | ||
|
||
from cloudfoundry_client.operations.push.cf_ignore import CfIgnore | ||
from imported import patch, mock_open, built_in_entry | ||
|
||
|
||
class TestCfIgnore(TestCase): | ||
|
||
def test_open_cfignore_file(self): | ||
with patch("%s.open" % built_in_entry, mock_open(read_data="*.log")) as mock_file, \ | ||
patch('os.path.isfile', create=True) as mock_isfile: | ||
mock_isfile.__return_value__ = True | ||
application_path = '/some/path' | ||
CfIgnore(application_path) | ||
|
||
mock_file.assert_called_with(os.path.join(application_path, '.cfignore'), 'r') | ||
|
||
def test_ignore_wildcard_resources(self): | ||
with patch("%s.open" % built_in_entry, mock_open(read_data="*.log")), \ | ||
patch('os.path.isfile', create=True) as mock_isfile: | ||
mock_isfile.__return_value__ = True | ||
cf_ignore = CfIgnore('/some/path') | ||
|
||
self.assertTrue(cf_ignore.is_entry_ignored("toto.log")) | ||
self.assertTrue(cf_ignore.is_entry_ignored("/some/other/path/toto.log")) | ||
|
||
def test_ignore_directory(self): | ||
with patch("%s.open" % built_in_entry, mock_open(read_data="ignored/directory/")), \ | ||
patch('os.path.isfile', create=True) as mock_isfile: | ||
mock_isfile.__return_value__ = True | ||
cf_ignore = CfIgnore('/some/path') | ||
|
||
self.assertTrue(cf_ignore.is_entry_ignored("ignored/directory/resource.file")) | ||
self.assertTrue(cf_ignore.is_entry_ignored("/ignored/directory/resource.file")) | ||
self.assertTrue(cf_ignore.is_entry_ignored("/some/sub/directory/containing/ignored/directory/resource.file")) | ||
# File in fact | ||
self.assertFalse(cf_ignore.is_entry_ignored('/ignored/directory')) | ||
|
||
def test_ignore_file_with_directory(self): | ||
with patch("%s.open" % built_in_entry, mock_open(read_data="ignored/directory/resource.file")), \ | ||
patch('os.path.isfile', create=True) as mock_isfile: | ||
mock_isfile.__return_value__ = True | ||
cf_ignore = CfIgnore('/some/path') | ||
|
||
self.assertTrue(cf_ignore.is_entry_ignored("ignored/directory/resource.file")) | ||
self.assertTrue(cf_ignore.is_entry_ignored("/ignored/directory/resource.file")) | ||
self.assertTrue(cf_ignore.is_entry_ignored("/some/sub/directory/containing/ignored/directory/resource.file")) | ||
# File in fact | ||
self.assertFalse(cf_ignore.is_entry_ignored('ignored/resource.file')) | ||
|
||
|