1
0

Support removing deps

This commit is contained in:
Jon Michael Aanes 2024-10-30 13:35:01 +01:00
parent d89e7cd562
commit bcae9edb18

View File

@ -15,10 +15,62 @@ def can_compile(path: Path) -> bool:
process = subprocess.run(['mvn', 'clean', 'compile'], cwd=path, check=False,capture_output=True) process = subprocess.run(['mvn', 'clean', 'compile'], cwd=path, check=False,capture_output=True)
return process.returncode == 0 return process.returncode == 0
def can_test_compile(path: Path) -> bool:
process = subprocess.run(['mvn', 'clean', 'compile', 'test-compile'], cwd=path, check=False,capture_output=True)
return process.returncode == 0
def write_pom(project_path: Path, pom: bs4.BeautifulSoup | str) -> None: def write_pom(project_path: Path, pom: bs4.BeautifulSoup | str) -> None:
with open(project_path / 'pom.xml', 'w') as f: with open(project_path / 'pom.xml', 'w') as f:
f.write(str(pom)) f.write(str(pom))
def check_for_dependencies_that_need_only_be_used_in_test_scope(pom, errors, project_path):
main_dependencies = []
for dep in pom.select('dependencies > 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)
dep.insert(-1, bs4.BeautifulSoup('<scope>test</scope>', 'lxml-xml'))
write_pom(project_path, pom)
if can_compile(project_path):
errors.append(f'Could move {name} to test scope')
logger.error('%s', errors[-1])
else:
dep.scope.extract()
del dep, name
def check_for_unneeded_dependencies(pom, errors, project_path):
"""Assumes that
`check_for_dependencies_that_need_only_be_used_in_test_scope` have already
been run, such that we can skip main dependencies.
"""
soup_deps = pom.select_one('dependencies')
test_dependencies = []
for dep in soup_deps.select('dependency'):
if dep.scope and dep.scope.get_text().strip() == 'test':
test_dependencies.append(dep)
for dep in test_dependencies:
name = '{}:{}'.format(dep.groupId.get_text(), dep.artifactId.get_text())
logger.info('Checking %s', name)
dep.extract()
write_pom(project_path, pom)
if can_test_compile(project_path):
errors.append(f'Could remove {name} from dependencies')
logger.error('%s', errors[-1])
else:
soup_deps.insert(-1, dep)
del dep, name
def main(): def main():
logging.basicConfig() logging.basicConfig()
logger.setLevel('INFO') logger.setLevel('INFO')
@ -29,28 +81,10 @@ def main():
with open(args.project / 'pom.xml') as f: with open(args.project / 'pom.xml') as f:
original_pom = f.read() original_pom = f.read()
pom = bs4.BeautifulSoup(original_pom, 'lxml-xml') pom = bs4.BeautifulSoup(original_pom, 'lxml-xml')
main_dependencies = []
for dep in pom.select('dependencies > dependency'):
if dep.scope is None:
main_dependencies.append(dep)
errors = [] errors = []
for dep in main_dependencies: check_for_dependencies_that_need_only_be_used_in_test_scope(pom, errors, args.project)
name = '{}:{}'.format(dep.groupId.get_text(), dep.artifactId.get_text()) check_for_unneeded_dependencies(pom, errors, args.project)
logger.info('Checking %s', name)
dep.insert(-1, bs4.BeautifulSoup('<scope>test</scope>', 'lxml-xml'))
write_pom(args.project, pom)
if can_compile(args.project):
errors.append(f'Could move {name} to test scope')
logger.error('%s', errors[-1])
else:
dep.scope.extract()
del dep, name
logger.info('Resetting POM') logger.info('Resetting POM')
write_pom(args.project, original_pom) write_pom(args.project, original_pom)
logger.info('Finished checks') logger.info('Finished checks')