Skip to content

Commit

Permalink
Fix file exists check (#39)
Browse files Browse the repository at this point in the history
* update isfile check in copy_dir_json to check the correct file path

* update get_code_file to use the correct package name

* update passed arg to Unifier in copy_content_yml

* revert get_code_file function

* remove 'test_.*\.py' from ignore regex
  • Loading branch information
avidan-H authored Dec 1, 2019
1 parent 87bfbd6 commit 265dc5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions demisto_sdk/yaml_tools/content_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def copy_content_yml(self, path, out_path, yml_info):
script_obj = yml_info['script']
with io.open(path, mode='r', encoding='utf-8') as file_:
yml_text = file_.read()
unifier = Unifier(path, parent_dir_name, out_path)
unifier = Unifier(os.path.dirname(path), parent_dir_name, out_path)
out_map = unifier.write_yaml_with_docker(yml_text, yml_info, script_obj)
if len(out_map.keys()) > 1:
print(" - yaml generated multiple files: {}".format(out_map.keys()))
Expand Down Expand Up @@ -166,8 +166,10 @@ def copy_dir_json(self, dir_path, bundle):
new_path = dpath
if dir_name == 'IndicatorFields' and not dpath.startswith('incidentfield-indicatorfield-'):
new_path = dpath.replace('incidentfield-', 'incidentfield-indicatorfield-')
if os.path.isfile(new_path):
raise NameError('Failed while trying to create {}. File already exists.'.format(new_path))
if os.path.isfile(os.path.join(bundle, new_path)):
raise NameError(
f'Failed while trying to create {os.path.join(bundle, new_path)}. File already exists.'
)
dpath = new_path

if len(dpath) >= self.file_name_max_size:
Expand Down
18 changes: 10 additions & 8 deletions demisto_sdk/yaml_tools/unifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,22 @@ def get_data(self, extension):

def get_code_file(self, script_type):
"""Return the first code file in the specified directory path
:param script_type: script type: .py or .js
:type script_type: str
:return: path to found code file
:rtype: str
"""

# We assume here the code file has the same name as the integration/script name, plus the addition of the type
package_name = os.path.basename(os.path.dirname(self.package_path))
code_file_path = self.package_path + package_name + script_type
if not os.path.isfile(code_file_path):
raise Exception('Code file does not exists or has different name than {}'
.format(package_name + script_type))
return code_file_path
ignore_regex = (r'CommonServerPython\.py|CommonServerUserPython\.py|demistomock\.py|_test\.py'
r'|conftest\.py')
if not self.package_path.endswith('/'):
self.package_path += '/'
if self.package_path.endswith('Scripts/CommonServerPython/'):
return self.package_path + 'CommonServerPython.py'

script_path = list(filter(lambda x: not re.search(ignore_regex, x),
glob.glob(os.path.join(self.package_path, '*' + script_type))))[0]
return script_path

def insert_script_to_yml(self, script_type, yml_text, yml_data):
script_path = self.get_code_file(script_type)
Expand Down

0 comments on commit 265dc5f

Please sign in to comment.