1
0

Improved robustness

This commit is contained in:
Jon Michael Aanes 2024-10-30 13:57:08 +01:00
parent bcae9edb18
commit dbc329f1a8

View File

@ -24,14 +24,19 @@ def write_pom(project_path: Path, pom: bs4.BeautifulSoup | str) -> None:
f.write(str(pom))
def check_for_dependencies_that_need_only_be_used_in_test_scope(pom, errors, project_path):
soup_deps = pom.select_one('dependencies')
if soup_deps is None:
logger.info('No dependencies... nothing to do.')
return
main_dependencies = []
for dep in pom.select('dependencies > dependency'):
for dep in soup_deps.select('dependency'):
if dep.scope is None:
main_dependencies.append(dep)
for dep in main_dependencies:
name = '{}:{}'.format(dep.groupId.get_text(), dep.artifactId.get_text())
logger.info('Checking %s', name)
logger.info('Checking %s for move to test scope', name)
dep.insert(-1, bs4.BeautifulSoup('<scope>test</scope>', 'lxml-xml'))
write_pom(project_path, pom)
@ -51,6 +56,9 @@ def check_for_unneeded_dependencies(pom, errors, project_path):
"""
soup_deps = pom.select_one('dependencies')
if soup_deps is None:
logger.info('No dependencies... nothing to do.')
return
test_dependencies = []
for dep in soup_deps.select('dependency'):
@ -59,7 +67,7 @@ def check_for_unneeded_dependencies(pom, errors, project_path):
for dep in test_dependencies:
name = '{}:{}'.format(dep.groupId.get_text(), dep.artifactId.get_text())
logger.info('Checking %s', name)
logger.info('Checking %s for removal', name)
dep.extract()
write_pom(project_path, pom)
@ -83,11 +91,15 @@ def main():
pom = bs4.BeautifulSoup(original_pom, 'lxml-xml')
errors = []
check_for_dependencies_that_need_only_be_used_in_test_scope(pom, errors, args.project)
check_for_unneeded_dependencies(pom, errors, args.project)
logger.info('Resetting POM')
write_pom(args.project, original_pom)
logger.info('Finished checks')
try:
check_for_dependencies_that_need_only_be_used_in_test_scope(pom, errors, args.project)
check_for_unneeded_dependencies(pom, errors, args.project)
except Exception:
logger.exception('Error occured while checking dependencies')
finally:
logger.info('Resetting POM')
write_pom(args.project, original_pom)
logger.info('Finished checks')
if errors:
logger.error('Encountered %d errors', len(errors))