summaryrefslogtreecommitdiff
path: root/java/genpom
blob: 39eccd4c3e851625eb132f770f58e722e75696cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/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("-m", "--modules", help="modules dependencies")
parser.add_option("-p", "--prefix", help="prefix of the project's artifacts names")
parser.add_option("-i", "--ignore", action="store_true", help="ignore missing deps")
parser.add_option("-s", "--search-path", action="append",
                  help="the path to search for deps")
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(os.pathsep))

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.prefix is not None:
  opts.artifact = opts.prefix + "-" + opts.artifact

if opts.name is None:
  opts.name = opts.artifact

if opts.artifact is None:
  opts.artifact = opts.name

def lookup(dep, attr):
  nd = dep["dep"][attr]
  if nd is None:
    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 = []
module_depends = []
if opts.modules is not None:
  module_depends = opts.modules.replace("/", "-").split();
  
for module in module_depends:
  if opts.prefix is None:
     artifactId = module
  else:
     artifactId = opts.prefix + "-" + module
  
  deps.append("""
    <dependency>
      <groupId>%s</groupId>
      <artifactId>%s</artifactId>
      <version>%s</version>
    </dependency>
""" % (opts.group, artifactId, opts.version))

for jar in jars:
  base, ext = os.path.splitext(os.path.basename(jar))
  dep = search(expanded_path, "%s.xml" % base)
  if dep is None:
    if opts.ignore:
      continue
    else:
      die("unable to locate xml for %s" % jar)
  group = lookup(dep, "groupId")
  artifactId = lookup(dep, "artifactId")
  version = lookup(dep, "version")
  deps.append("""
    <dependency>
      <groupId>%s</groupId>
      <artifactId>%s</artifactId>
      <version>%s</version>
      <scope>%s</scope>
    </dependency>
""" % (group, artifactId, version,
       scopes.get(artifactId, "compile")))

TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd ">
  <modelVersion>4.0.0</modelVersion>
  <groupId>%(group)s</groupId>
  <artifactId>%(artifact)s</artifactId>
  <version>%(version)s</version>

  <name>%(name)s</name>
  <url>%(url)s</url>
  <description>%(description)s</description>

  <organization>
    <name>The Apache Software Foundation</name>
    <url>http://www.apache.org</url>
  </organization>

  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
    </license>
  </licenses>

  <dependencies>
    %(dependencies)s
  </dependencies>

</project>
"""

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()