Created
October 13, 2015 00:11
-
-
Save khill/b6134487d1415da035a4 to your computer and use it in GitHub Desktop.
Python script to download jar file info from Maven using SHA1 checksums and print Maven dependency records
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| ''' | |
| Script to generate the pom.xml dependency section for a bunch of jar files. | |
| ''' | |
| import hashlib | |
| import sys | |
| import os | |
| import json | |
| import httplib | |
| def get_sha1_sum(fname): | |
| ''' generates a SHA1 sum for the given file ''' | |
| with open(fname, 'rb') as f: | |
| return hashlib.sha1(f.read()).hexdigest() | |
| def get_dependency(group_id, artifact_id, version): | |
| ''' creates XML dependency element ''' | |
| return '<dependency>\n<groupId>%s</groupId>\n<artifactId>%s</artifactId>\n<version>%s</version>\n</dependency>\n' % (group_id, artifact_id, version) | |
| def get_dep_xml(sha1sum): | |
| ''' looks up the jar file SHA1 checksum at search.maven.org and grabs the XML dependency info ''' | |
| search_uri = '/solrsearch/select?q=1%%3A%%22%s%%22&rows=20&wt=json' % sha1sum | |
| conn = httplib.HTTPConnection('search.maven.org') | |
| conn.request('GET', search_uri) | |
| response = conn.getresponse() | |
| if response.status == 200: | |
| search_result = json.loads(response.read()) | |
| if search_result['response']['numFound'] == 1: | |
| project = search_result['response']['docs'][0] | |
| group_id = project.get('g') | |
| artifact_id = project.get('a') | |
| version_id = project.get('v') | |
| return get_dependency(group_id, artifact_id, version_id) | |
| return None | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 1: | |
| jar_dir = sys.argv[1] | |
| else: | |
| jar_dir = raw_input('Please specify a path to the jar file directory: ') | |
| jar_files = [x for x in os.listdir(jar_dir) if x.endswith('.jar')] | |
| for jar_file in jar_files: | |
| sha1sum = get_sha1_sum(os.path.join(jar_dir, jar_file)) | |
| dep_xml = get_dep_xml(sha1sum) | |
| if dep_xml: | |
| print dep_xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment