From dbc329f1a8b6287abedfca2d5b6c0b7ec679bc5e Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Wed, 30 Oct 2024 13:57:08 +0100 Subject: [PATCH] Improved robustness --- maven_check_minimal_deps/__main__.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/maven_check_minimal_deps/__main__.py b/maven_check_minimal_deps/__main__.py index 13cf40f..68d15e6 100644 --- a/maven_check_minimal_deps/__main__.py +++ b/maven_check_minimal_deps/__main__.py @@ -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('test', '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))