Skip to content

Commit

Permalink
Fixed Issue69 and a Unit test bug(#69)
Browse files Browse the repository at this point in the history
Fixed bug where a folder mentioned in .ebignore file was being included in the final code.zip on running eb deploy. This was due to the presence of a symlink within that folder.
  • Loading branch information
yohanthshetty authored Sep 23, 2022
1 parent a4d0066 commit 7b71bbc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
15 changes: 12 additions & 3 deletions ebcli/core/fileoperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def _get_option(config, section, key, default):
def is_git_directory_present():
return os.path.isdir('.git')

def is_parent_directory_in_ignore_list(filepath, ignore_list):
for directory in ignore_list:
if os.path.abspath(filepath).startswith(os.path.abspath(directory)):
return True
return False

def clean_up():
cwd = os.getcwd()
Expand Down Expand Up @@ -423,7 +428,6 @@ def zip_up_project(location, ignore_list=None):
finally:
os.chdir(cwd)


def _zipdir(path, zipf, ignore_list=None):
if ignore_list is None:
ignore_list = {'.gitignore'}
Expand All @@ -447,14 +451,18 @@ def _zipdir(path, zipf, ignore_list=None):
zipInfo.external_attr = 2716663808
else:
zipInfo.external_attr = long(2716663808)
zipf.writestr(zipInfo, os.readlink(cur_dir))
if not is_parent_directory_in_ignore_list(cur_dir, ignore_list):
zipf.writestr(zipInfo, os.readlink(cur_dir))
else:
io.log_info(' -skipping: {}'.format(cur_dir))
for f in files:
cur_file = os.path.join(root, f)

if (
cur_file.endswith('~')
or cur_file in ignore_list
or not _validate_file_for_archive(cur_file)
or is_parent_directory_in_ignore_list(cur_file, ignore_list)
):
# Ignore editor backup files (like file.txt~)
# Ignore anything in the .ebignore file
Expand Down Expand Up @@ -812,7 +820,8 @@ def get_ebignore_list():
with codecs.open(location, 'r', encoding='utf-8') as f:
spec = PathSpec.from_lines('gitwildmatch', f)

ignore_list = {f for f in spec.match_tree(get_project_root())}
matches = [f for f in spec.match_tree_entries(get_project_root())]
ignore_list = {match.path for match in matches}
ignore_list.add('.ebignore')

return ignore_list
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ botocore>1.23.41,<1.27.80
cement==2.8.2
colorama>=0.2.5,<0.4.4 # use the same range that 'docker-compose' uses
future>=0.16.0,<0.17.0
pathspec==0.9.0
pathspec==0.10.1
python-dateutil>=2.1,<3.0.0 # use the same range that 'botocore' uses
requests>=2.20.1,<=2.26
setuptools >= 20.0
Expand Down
20 changes: 15 additions & 5 deletions tests/unit/core/test_fileoperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,24 +717,33 @@ def test_delete_app_file(self):
def test_zip_up_project(self, _validate_file_for_archive_mock):
_validate_file_for_archive_mock.side_effect = lambda f: not f.endswith('.sock')
shutil.rmtree('home', ignore_errors=True)
os.mkdir('ignore-me')
os.mkdir('linkme')
os.mkdir('src')
os.mkdir(os.path.join('src', 'lib'))
open(os.path.join('src', 'lib', 'app.py'), 'w').write('import os')
open(os.path.join('src', 'lib', 'app.py~'), 'w').write('import os')
open(os.path.join('src', 'lib', 'ignore-this-file.py'), 'w').write('import os')
open(os.path.join('src', 'lib', 'test.sock'), 'w').write('mock socket file')
open(os.path.join('ignore-me', 'text.txt'), 'w').write('import os')
open(os.path.join('linkme', 'foobar.txt'), 'w').write('import os linkme')

os.symlink(
os.path.join('src', 'lib', 'app.py'),
os.path.join('src', 'lib', 'app.py-copy')
os.path.abspath(os.path.join('src', 'lib', 'app.py')),
os.path.abspath(os.path.join('src', 'lib', 'app.py-copy'))
)

os.mkdir(os.path.join('src', 'lib', 'api'))

if sys.version_info > (3, 0):
os.symlink(
os.path.join('src', 'lib', 'api'),
os.path.join('src', 'lib', 'api-copy'),
os.path.abspath(os.path.join('src', 'lib', 'api')),
os.path.abspath(os.path.join('src', 'lib', 'api-copy')),
target_is_directory=True
)
os.symlink(
os.path.abspath(os.path.join('linkme')),
os.path.abspath(os.path.join('ignore-me','linkme')),
target_is_directory=True
)
else:
Expand All @@ -747,7 +756,7 @@ def test_zip_up_project(self, _validate_file_for_archive_mock):

fileoperations.zip_up_project(
'app.zip',
ignore_list=[os.path.join('src', 'lib', 'ignore-this-file.py')]
ignore_list=[os.path.join('src', 'lib', 'ignore-this-file.py'), os.path.join('ignore-me/')]
)

os.mkdir('tmp')
Expand All @@ -760,6 +769,7 @@ def test_zip_up_project(self, _validate_file_for_archive_mock):
self.assertFalse(os.path.exists(os.path.join('tmp', 'src', 'lib', 'app.py~')))
self.assertFalse(os.path.exists(os.path.join('tmp', 'src', 'lib', 'ignore-this-file.py')))
self.assertFalse(os.path.exists(os.path.join('tmp', 'src', 'lib', 'test.sock')))
self.assertFalse(os.path.exists(os.path.join('tmp','ignore-me', 'link-me')))

def test_delete_app_versions(self):
os.mkdir(os.path.join('.elasticbeanstalk', 'app_versions'))
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/operations/test_ebignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def test_get_ebignore_list__includes_directories_in_the_ebignore_list(
self.assertEqual(
{
'directory_1{}.gitkeep'.format(os.path.sep),
'directory_1',
'directory_2{}.gitkeep'.format(os.path.sep),
'.ebignore'
},
Expand Down Expand Up @@ -206,7 +207,7 @@ def test_get_ebignore_list__includes_filenames_with_spaces_in_ignore_list(
open('file 1', 'w').close()

with open('.ebignore', 'w') as file:
ebignore_file_contents = """
ebignore_file_contents = r"""
file\ 1
"""

Expand Down Expand Up @@ -234,7 +235,7 @@ def test_get_ebignore_list__includes_filenames_with_exclamations_in_ignore_list(
open('!file_1', 'w').close()

with open('.ebignore', 'w') as file:
ebignore_file_contents = """
ebignore_file_contents = r"""
\!file_1
"""

Expand Down Expand Up @@ -265,7 +266,7 @@ def test_get_ebignore_list__includes_files_with_unicode_names(
open('ändrar något i databasen', 'w').close()

with open('.ebignore', 'wb') as file:
ebignore_file_contents = """
ebignore_file_contents = r"""
哈哈
昨夜のコンサートは最高でした。
ändrar\ något\ i\ databasen
Expand Down

0 comments on commit 7b71bbc

Please sign in to comment.