Skip to content

Commit c055444

Browse files
1. changing check_and_yield_file() to _check_and_yield_file() (#399)
2. opening file with encoding='utf8' 3. expecting UnicodeDecodeError when encoding a file Signed-off-by: haim-kermany <[email protected]> Signed-off-by: haim-kermany <[email protected]> Co-authored-by: Ziv Nevo <[email protected]>
1 parent ccf2b82 commit c055444

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

nca/FileScanners/DirScanner.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def __init__(self, fs_path, rt_load=False):
2020
HelmScanner.__init__(self)
2121
self.fs_path = fs_path
2222

23-
def check_and_yield_file(self, file_path):
23+
def _check_and_yield_file(self, file_path):
2424
"""
2525
checks if the given file is yaml file and yield its components
2626
:param str file_path: path of file to check and yield
2727
"""
2828
if GenericTreeScanner.is_yaml_file(file_path):
29-
file_stream = open(file_path)
29+
file_stream = open(file_path, encoding='utf8')
3030
yield from self._yield_yaml_file(file_path, file_stream)
3131
file_stream.close()
3232

@@ -35,7 +35,7 @@ def get_yamls(self):
3535
Call this function to get a generator for all yaml files
3636
"""
3737
if os.path.isfile(self.fs_path):
38-
yield from self.check_and_yield_file(self.fs_path)
38+
yield from self._check_and_yield_file(self.fs_path)
3939
return
4040

4141
if self.fs_path.endswith('**'):
@@ -46,18 +46,21 @@ def get_yamls(self):
4646
def _scan_dir_for_yamls(self, dir_path, recursive):
4747
for root, sub_dirs, files in os.walk(dir_path):
4848
for file in files:
49-
if self.is_helm_chart(file):
50-
file_name, file_content = self.parse_chart(root)
51-
file_stream = io.StringIO(file_content)
52-
yield from self._yield_yaml_file(file_name, file_stream)
53-
file_stream.close()
54-
else:
55-
full_path = os.path.abspath(os.path.join(root, file))
56-
# skip if file was resolved by HELM or Helm template
57-
if self.is_yaml_file(full_path) and not self.is_resolved_template(full_path):
58-
if self.is_template(full_path):
59-
print('Warning: Skipping templated yaml file:', full_path, file=stderr)
60-
else:
61-
yield from self.check_and_yield_file(os.path.join(root, file))
49+
try:
50+
if self.is_helm_chart(file):
51+
file_name, file_content = self.parse_chart(root)
52+
file_stream = io.StringIO(file_content)
53+
yield from self._yield_yaml_file(file_name, file_stream)
54+
file_stream.close()
55+
else:
56+
full_path = os.path.abspath(os.path.join(root, file))
57+
# skip if file was resolved by HELM or Helm template
58+
if self.is_yaml_file(full_path) and not self.is_resolved_template(full_path):
59+
if self.is_template(full_path):
60+
print('Warning: Skipping templated yaml file:', full_path, file=stderr)
61+
else:
62+
yield from self._check_and_yield_file(os.path.join(root, file))
63+
except UnicodeDecodeError as decode_err:
64+
print(f'Parse Error: While scanning {dir_path}, failed to decode {file}. error:\n{decode_err.reason}')
6265
if not recursive:
6366
break

nca/FileScanners/HelmScanner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def is_template(file):
5959
"""
6060
if not file:
6161
return False
62-
with open(file, "r") as f:
62+
with open(file, "r", encoding='utf8') as f:
6363
return '{{' in f.read()
6464

6565
@staticmethod

nca/NetworkConfig/ResourcesHandler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ def _parse_resources_path(self, resource_list, resource_flags):
262262
print(
263263
f'{prs_err.problem_mark.name}:{prs_err.problem_mark.line}:{prs_err.problem_mark.column}:',
264264
'Parse Error:', prs_err.problem, file=stderr)
265+
except UnicodeDecodeError as decode_err:
266+
print(f'Parse Error: Failed to decode {yaml_file.path}. error:\n{decode_err.reason}')
265267

266268
self.policies_finder.parse_policies_in_parse_queue()
267269

0 commit comments

Comments
 (0)