Retrieve the latest snapshot from Nexus
#!/usr/bin/env python3
"""nexus_latest_snap.py: Locate the most recent snapshot in Nexus"""
import sys
import xml.etree.ElementTree as tree
import requests
ARTIFACT = sys.argv[1]
HINT = sys.argv[2]
BASE_URL = 'http://nexus:8080'
def main():
"""Blah"""
# Pull Lucene search results
root = tree.fromstring(requests.get(
BASE_URL + '/service/local/lucene/search?p=jar&a=' + ARTIFACT +
'&g=com.*' + HINT + '*').text)
# Translate the group ID into a path
group_id_path = root.find(
"./data/artifact/[artifactId='"+ARTIFACT +
"']/groupId").text.replace('.', '/')
# Pull more info about the repo
latest_snap = root.find(
"./data/artifact/[artifactId='"+ARTIFACT +
"']/latestSnapshot").text
snap_repo = root.find(
"./data/artifact/[artifactId='" + ARTIFACT +
"']/latestSnapshotRepositoryId").text
# Set the path to the maven-metadata.xml
repo_meta = BASE_URL + '/service/local/repositories/' + \
snap_repo + '/content/' + group_id_path + '/' + \
ARTIFACT + '/' + latest_snap + '/maven-metadata.xml'
# Pull the repo metadata and parse the XML
repo_root = tree.fromstring(requests.get(repo_meta).text)
# Pull out the lastUpdated value
most_recent = repo_root.find('./versioning/lastUpdated').text
# Pull the jar name that matches the most recent change
jar_ext = repo_root.find(
".//snapshotVersion/[extension='jar'][classifier][updated='" +
most_recent + "']/value").text
# Generate and print the final jar URL
url = BASE_URL + '/service/local/repositories/' + snap_repo + \
'/content' + '/'+group_id_path + '/'+ARTIFACT + '/' + \
latest_snap + '/' + ARTIFACT + '-' + jar_ext + '.jar'
print(url)
if __name__ == '__main__':
main()