Created
July 4, 2013 13:06
-
-
Save xshyamx/5927550 to your computer and use it in GitHub Desktop.
Simple python script to generate a maven pom.xml file from a list of jars in a directory (with proxy support but, remember urllib2 proxy does not work for https)
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
import os | |
import json | |
import urllib2 | |
import hashlib | |
import argparse | |
import urlparse | |
import traceback | |
# configurable parameters | |
def maven_central(sha1_hash): | |
""" | |
Use the hash search of maven central | |
""" | |
url_template = 'http://search.maven.org/solrsearch/select?q=1:%22{sha}%22&rows=20&wt=json' | |
content = get_url(url_template.format(sha=sha1_hash)) | |
if ( content ): | |
try: | |
js = json.loads(content) | |
if ( js['response']['numFound'] > 0 and len(js['response']['docs']) > 0 ): | |
dep = js['response']['docs'][0] | |
return dep_template.format(groupId=dep['g'], artifactId=dep['a'], version=dep['v']) | |
except Exception as e: | |
print('[ERROR] ' + str(e)) | |
return None | |
def jboss_repo(sha1_hash): | |
""" | |
User the hash search of jboss nexus repo | |
""" | |
url_template = 'https://repository.jboss.org/nexus/service/local/lucene/search?sha1={sha}' | |
content = get_url(url_template.format(sha=sha1_hash), None, {'Accept': 'application/json'}) | |
if ( content ): | |
try: | |
js = json.loads(content) | |
if ( js['totalCount'] > 0 and len(js['data']) > 0 ): | |
dep = js['data'][0] | |
return dep_template.format(groupId=dep['groupId'], artifactId=dep['artifactId'], version=dep['version']) | |
except Exception as e: | |
print('[ERROR] ' + str(e)) | |
return None | |
def debug(msg): | |
if ( DEBUG ): | |
print(msg) | |
def get_dependency(sha1_hash): | |
providers = { | |
'central': maven_central, | |
'jboss' : jboss_repo | |
} | |
dep = None | |
for name, provider in providers.items(): | |
debug("Trying %s" % name) | |
dep = provider(sha1_hash) | |
if ( dep != None ): | |
break | |
return dep | |
def list_jars(dr): | |
jars = [] | |
for fn in os.listdir(dr): | |
jar = os.path.join(os.path.realpath(dr), fn) | |
if ( os.path.isfile(jar) and jar.endswith('.jar') ): | |
jars.append(jar) | |
return jars | |
def list_jar_files(dirs=['.']): | |
files = [] | |
for dr in dirs: | |
files.extend(list_jars(dr)) | |
return files | |
def calculate_hashes(jar_files): | |
default_sha1 = hashlib.sha1().hexdigest() | |
sha_map = {} | |
for jar in jar_files: | |
sha1_hash = sha1(jar) | |
if ( default_sha1 != sha1_hash ): | |
sha_map[os.path.basename(jar)] = sha1_hash | |
return sha_map | |
def set_proxy(proxy_cfg={}): | |
debug('setting proxy : {0}'.format(proxy_cfg)) | |
opener = urllib2.build_opener(urllib2.ProxyHandler(proxy_cfg)) | |
urllib2.install_opener(opener) | |
def make_request(url, data=None, headers={}): | |
req = urllib2.Request(url, data) if data else urllib2.Request(url) | |
for k,v in default_headers.items(): | |
req.add_header(k, v) | |
for k,v in headers.items(): | |
req.add_header(k, v) | |
return req | |
def get_url(url, data=None, headers={}): | |
try: | |
debug('GET {0}'.format(url)) | |
req = make_request(url, data, headers) | |
f = urllib2.urlopen(req) | |
b = f.read() | |
f.close() | |
return b | |
except Exception as e: | |
traceback.print_exc() | |
print('[ERROR] ' + str(e)) | |
print('[ERROR] fetching url : %s' % url) | |
def sha1(fp): | |
blocksize = 64*1024 | |
sha = hashlib.sha1() | |
with open(fp, 'rb') as fp: | |
while True: | |
data = fp.read(blocksize) | |
if not data: | |
break | |
sha.update(data) | |
return sha.hexdigest() | |
# constants | |
pom_template = '''<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>group-id</groupId> | |
<artifactId>artifact-id</artifactId> | |
<version>1.0</version> | |
<packaging>jar</packaging> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
{dependencies} | |
</dependencies> | |
</project>''' | |
dep_template = ''' | |
<dependency> | |
<groupId>{groupId}</groupId> | |
<artifactId>{artifactId}</artifactId> | |
<version>{version}</version> | |
</dependency>''' | |
default_headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0' | |
} | |
# configure argparse | |
parser = argparse.ArgumentParser(description='Generate sample pom.xml from list of jars') | |
parser.add_argument('jars_dir', metavar='dir', nargs='*', help='directory containing the jars') | |
parser.add_argument('-o', '--output', metavar='file', required=False, help='output file name') | |
parser.add_argument('-p', '--proxy', metavar='proxy', required=False, help='use specified proxy') | |
parser.add_argument('-d', '--debug', required=False, action='store_true', help='print debug info') | |
args = parser.parse_args() | |
DEBUG = args.debug | |
debug(args) | |
dirs = args.jars_dir | |
output_file = args.output if args.output else 'pom.xml' | |
if ( args.proxy ): | |
proxy_cfg = {} | |
up = urlparse.urlparse(args.proxy) | |
proxy_cfg[up.scheme] = args.proxy | |
set_proxy(proxy_cfg) | |
else: | |
set_proxy() | |
sha1_hashmap = calculate_hashes(list_jar_files(dirs)) | |
dependencies = [] | |
for jar,sha1 in sha1_hashmap.items(): | |
debug('checking for {0}...'.format(jar)) | |
dep = get_dependency(sha1) | |
if ( dep ): | |
dependencies.append(dep) | |
debug('success') | |
else: | |
print('failed to find dependency for %s' % jar) | |
f = open(output_file, 'w') | |
f.write(pom_template.format(dependencies='\n'.join(dependencies))) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment