|
| 1 | +import os |
| 2 | +import xml.etree.ElementTree as ET |
| 3 | +import urllib.request |
| 4 | +import urllib.error |
| 5 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 6 | + |
| 7 | +def get_tag_text(element, tag, namespaces): |
| 8 | + found = element.find(tag, namespaces) |
| 9 | + return found.text if found is not None else None |
| 10 | + |
| 11 | +def is_deploy_skipped(root, namespaces): |
| 12 | + props = root.find('mvn:properties', namespaces) |
| 13 | + if props is None: |
| 14 | + props = root.find('properties', {}) |
| 15 | + |
| 16 | + if props is not None: |
| 17 | + for prop in props: |
| 18 | + # Check for both maven.deploy.skip and deploy.skip |
| 19 | + if prop.tag.endswith('maven.deploy.skip') or prop.tag.endswith('deploy.skip'): |
| 20 | + if prop.text and prop.text.strip().lower() == 'true': |
| 21 | + return True |
| 22 | + return False |
| 23 | + |
| 24 | +def check_maven_central(group_id, artifact_id, version): |
| 25 | + group_path = group_id.replace('.', '/') |
| 26 | + url = f"https://repo1.maven.org/maven2/{group_path}/{artifact_id}/{version}/{artifact_id}-{version}.pom" |
| 27 | + try: |
| 28 | + req = urllib.request.Request(url, method='HEAD') |
| 29 | + with urllib.request.urlopen(req, timeout=10) as response: |
| 30 | + if response.getcode() == 200: |
| 31 | + return True, group_id, artifact_id, version, url |
| 32 | + except urllib.error.HTTPError: |
| 33 | + return False, group_id, artifact_id, version, url |
| 34 | + except Exception: |
| 35 | + return False, group_id, artifact_id, version, url |
| 36 | + return False, group_id, artifact_id, version, url |
| 37 | + |
| 38 | +def main(): |
| 39 | + print("Finding pom.xml files...") |
| 40 | + pom_files = [] |
| 41 | + for root, dirs, files in os.walk('.'): |
| 42 | + if '.github' in dirs: |
| 43 | + dirs.remove('.github') |
| 44 | + for file in files: |
| 45 | + if file == 'pom.xml': |
| 46 | + pom_files.append(os.path.join(root, file)) |
| 47 | + |
| 48 | + print(f"Found {len(pom_files)} pom.xml files. Parsing unique non-SNAPSHOT artifacts...") |
| 49 | + |
| 50 | + to_check = {} |
| 51 | + |
| 52 | + for pom_path in pom_files: |
| 53 | + try: |
| 54 | + tree = ET.parse(pom_path) |
| 55 | + root = tree.getroot() |
| 56 | + |
| 57 | + namespaces = {'mvn': 'http://maven.apache.org/POM/4.0.0'} |
| 58 | + if '}' in root.tag: |
| 59 | + ns = root.tag.split('}')[0].strip('{') |
| 60 | + namespaces = {'mvn': ns} |
| 61 | + |
| 62 | + if is_deploy_skipped(root, namespaces): |
| 63 | + # print(f"Skipping {pom_path} because deployment is skipped.") |
| 64 | + continue |
| 65 | + |
| 66 | + group_id = get_tag_text(root, 'mvn:groupId', namespaces) |
| 67 | + artifact_id = get_tag_text(root, 'mvn:artifactId', namespaces) |
| 68 | + version = get_tag_text(root, 'mvn:version', namespaces) |
| 69 | + |
| 70 | + parent = root.find('mvn:parent', namespaces) |
| 71 | + if parent is not None: |
| 72 | + if not group_id: |
| 73 | + group_id = get_tag_text(parent, 'mvn:groupId', namespaces) |
| 74 | + if not version: |
| 75 | + version = get_tag_text(parent, 'mvn:version', namespaces) |
| 76 | + |
| 77 | + if not group_id or not artifact_id or not version: |
| 78 | + if not group_id: group_id = get_tag_text(root, 'groupId', {}) |
| 79 | + if not artifact_id: artifact_id = get_tag_text(root, 'artifactId', {}) |
| 80 | + if not version: version = get_tag_text(root, 'version', {}) |
| 81 | + if parent is not None: |
| 82 | + if not group_id: group_id = get_tag_text(parent, 'groupId', {}) |
| 83 | + if not version: version = get_tag_text(parent, 'version', {}) |
| 84 | + |
| 85 | + if not group_id or not artifact_id or not version: |
| 86 | + continue |
| 87 | + |
| 88 | + if version.endswith('-SNAPSHOT'): |
| 89 | + continue |
| 90 | + |
| 91 | + key = (group_id, artifact_id, version) |
| 92 | + if key not in to_check: |
| 93 | + to_check[key] = pom_path |
| 94 | + |
| 95 | + except Exception: |
| 96 | + pass |
| 97 | + |
| 98 | + print(f"Checking {len(to_check)} unique artifacts on Maven Central...") |
| 99 | + |
| 100 | + found_published = [] |
| 101 | + with ThreadPoolExecutor(max_workers=25) as executor: |
| 102 | + futures = [executor.submit(check_maven_central, g, a, v) for (g, a, v) in to_check.keys()] |
| 103 | + |
| 104 | + count = 0 |
| 105 | + total = len(futures) |
| 106 | + for future in as_completed(futures): |
| 107 | + is_published, g, a, v, url = future.result() |
| 108 | + count += 1 |
| 109 | + if is_published: |
| 110 | + found_published.append(f"{g}:{a}:{v} (found in {to_check[(g,a,v)]})") |
| 111 | + print(f"[{count}/{total}] ALREADY PUBLISHED: {g}:{a}:{v}") |
| 112 | + elif count % 100 == 0: |
| 113 | + print(f"[{count}/{total}] Checking...") |
| 114 | + |
| 115 | + if found_published: |
| 116 | + print("\nSummary of published versions found in local pom.xml files:") |
| 117 | + for item in sorted(found_published): |
| 118 | + print(item) |
| 119 | + else: |
| 120 | + print("\nNo published non-SNAPSHOT versions found.") |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments