#!/usr/bin/python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import sys, re, os, mllib, optparse
def die(msg):
print >> sys.stderr, msg
sys.exit(1)
parser = optparse.OptionParser(usage="usage: %prog [options] JARs ...",
description="Generates a pom.")
parser.add_option("-n", "--name")
parser.add_option("-g", "--group")
parser.add_option("-a", "--artifact")
parser.add_option("-v", "--version")
parser.add_option("-d", "--description", default="")
parser.add_option("-u", "--url", default="")
parser.add_option("-i", "--ignore", action="store_true", help="ignore missing poms")
parser.add_option("-s", "--search-path", action="append",
help="the path to search for poms")
parser.add_option("-S", "--scope", metavar="ARTIFACT=SCOPE", action="append",
default=[],
help="specify scope for an artifact")
parser.add_option("-o", "--output")
opts, jars = parser.parse_args()
if opts.search_path is None:
path=["%s/.m2" % os.environ["HOME"]]
else:
path = []
for p in opts.search_path:
path.extend(p.split(":"))
expanded_path = []
for p in path:
os.path.walk(p, lambda a, d, fs: expanded_path.append(d), None)
if opts.group is None:
die("the group option is required")
if opts.version is None:
die("the version option is required")
if opts.name is None and opts.artifact is None:
die("one of name or artifact must be supplied")
if opts.name is None:
opts.name = opts.artifact
if opts.artifact is None:
opts.artifact = opts.name
def lookup(pom, attr):
nd = pom["project"][attr]
if nd is None:
nd = pom["project/parent"][attr]
if nd is None:
return None
return nd.text()
def search(path, file):
for d in path:
f = os.path.join(d, file)
if os.path.exists(f):
return mllib.xml_parse(f)
scopes = {}
for s in opts.scope:
m = re.match(r"(.*)=(.*)", s)
if not m:
die("bad scope specifier: %s" % s)
scopes[m.group(1)] = m.group(2)
deps = []
for jar in jars:
base, ext = os.path.splitext(os.path.basename(jar))
pom = search(expanded_path, "%s.pom" % base)
if pom is None:
if opts.ignore:
continue
else:
die("unable to locate pom for %s" % jar)
group = lookup(pom, "groupId")
artifactId = lookup(pom, "artifactId")
version = lookup(pom, "version")
deps.append("""
%s
%s
%s
%s
""" % (group, artifactId, version,
scopes.get(artifactId, "compile")))
TEMPLATE = """
4.0.0
%(group)s
%(artifact)s
%(version)s
%(name)s
%(url)s
%(description)s
The Apache Software Foundation
http://www.apache.org
The Apache Software License, Version 2.0
/LICENSE.txt
%(dependencies)s
"""
vars = {}
vars.update(opts.__dict__)
vars["dependencies"] = "".join(deps)
if opts.output is None:
out = sys.stdout
else:
out = open(opts.output, "w")
out.write(TEMPLATE % vars)
out.close()