-
Notifications
You must be signed in to change notification settings - Fork 44
/
install-to-project-repo.py
executable file
·229 lines (184 loc) · 6.15 KB
/
install-to-project-repo.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
# Install to Project Repo
# A script for installing jars to an in-project Maven repository.
#
# v0.1.2
#
# MIT License
# (c) 2012, Nikita Volkov. All rights reserved.
# https://github.com/nikita-volkov/install-to-project-repo
#
import os
import re
import shutil
import argparse
def jars(dir):
return [dir + "/" + f for f in os.listdir(dir) if f.lower().endswith(".jar")]
def parse_by_eclipse_standard(path):
file = os.path.splitext(os.path.basename(path))[0]
match = re.match(r"([\w\.-]+)_(\d+\.\d+\.\d+.*)", file)
if match != None:
(name, version) = match.group(1, 2)
name = name.split(".")
source = name[-1] == "source"
(group, name) = (".".join(name[:-2]), name[-2]) if source else (".".join(name[:-1]), name[-1])
snapshot = version.upper().endswith(".SNAPSHOT")
if snapshot:
version = version[:-len(".snapshot")]
return {
"group": group,
"name": name,
"version": version,
"snapshot": snapshot,
"source": source
}
def maven_dependencies(parsing_results):
def artifact(parsing):
return {
"groupId": parsing["group"],
"artifactId": parsing["name"],
"version": parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "")
}
def maven_dependency(artifact):
return """
<dependency>
<groupId>%(groupId)s</groupId>
<artifactId>%(artifactId)s</artifactId>
<version>%(version)s</version>
</dependency>
""" % artifact
def unique_artifacts():
artifacts = []
for (_, parsing) in parsing_results:
a = artifact(parsing)
if a not in artifacts:
artifacts.append(a)
return artifacts
return "\n".join([maven_dependency(a).strip() for a in unique_artifacts()])
def install(path, parsing):
os.system(
"mvn install:install-file" + \
" -Dfile=" + path + \
" -DgroupId=" + parsing["group"] + \
" -DartifactId=" + parsing["name"] + \
" -Dversion=" + parsing["version"] + ("-SNAPSHOT" if parsing["snapshot"] else "") + \
" -Dpackaging=jar" + \
" -DlocalRepositoryPath=repo" + \
" -DcreateChecksum=true" + \
(" -Dclassifier=sources" if parsing["source"] else "")
)
def splits(str, splitter):
parts = str.split(splitter)
def split(i):
(l, r) = splitAt(parts, i)
return (splitter.join(l), splitter.join(r))
return map(split, range(1, len(parts)))
def splitAt(list, i):
if i <= 0:
return ([], list)
elif i >= len(list):
return (list, [])
else:
return (list[:i], list[i:])
def name_to_version_alternatives(filename):
return [
(n, v)
for (n, v) in list(splits(filename, "-")) + list(splits(filename, "_"))
if v.lower() not in ["sources", "src", "snapshot"]
]
def group_to_name_alternatives(group):
return [
(n, v)
for (n, v) in splits(group, ".")
if (v.lower()) not in ["source", "snapshot"]
]
def version_parsing(version):
def f(version, splitter, ending):
parts = version.split(splitter)
if parts[-1] == ending:
return (splitter.join(parts[:-1]), True)
else:
return (version, False)
(version, source) = f(version, "-", "sources")
(version, snapshot) = f(version, "-", "SNAPSHOT")
if not snapshot:
(version, snapshot) = f(version, ".", "SNAPSHOT")
return version, snapshot, source
def name_parsing(name):
if name.endswith(".source"):
return name[:-len(".source")], True
else:
return name, False
def unzip(l):
return tuple(zip(*l))
def input_choice(labels, values):
for (i, v) in enumerate(labels):
print("%d) %s" % (i+1, v))
while True:
try:
i = input()
i = int(i)
return values[i-1]
except ValueError:
print("Incorrect input: `%s` is not a number. Try again" % i)
except IndexError:
print("Incorrect input: `%s` is out of range. Try again" % i)
def parse_interactively(path):
filename = os.path.splitext(os.path.basename(path))[0]
print("-----")
print("Processing `%s`" % path)
alternatives = name_to_version_alternatives(filename)
alternatives.sort(key=lambda n: len(n[1]))
if not alternatives:
print("Incorrect name format: `%s`. Skipping" % filename)
return
if len(alternatives) > 1:
print("Choose a correct version for `%s`:" % filename)
labels = [version_parsing(v)[0] for v in unzip(alternatives)[1]]
(name, version) = input_choice(labels, alternatives)
else:
(name, version) = alternatives[0]
alternatives = list(reversed(group_to_name_alternatives(name)))
if not alternatives:
print("Incorrect name format: `%s`. Skipping" % filename)
return
if len(alternatives) > 1:
print("Choose a correct artifactId for `%s`:" % name)
labels = [name_parsing(a)[0] for a in unzip(alternatives)[1]]
(group, name) = input_choice(labels, alternatives)
else:
(group, name) = alternatives[0]
version, snapshot, source = version_parsing(version)
name, source1 = name_parsing(name)
return {
"group": group,
"name": name,
"version": version,
"snapshot": snapshot,
"source": source or source1
}
parser = argparse.ArgumentParser(description='Installer for jars to an in-project Maven repository')
parser.add_argument('-i', '--interactive',
dest='interactive', action='store_true', default=False,
help='Interactively resolve ambiguous names. Use this option to install libraries of different naming standards')
parser.add_argument('-d', '--delete',
dest='delete', action='store_true', default=False,
help='Delete successfully installed libs in source location')
args = parser.parse_args()
parsings = (
[(path, parse_interactively(path)) for path in jars("lib")]
if args.interactive else
[(path, parse_by_eclipse_standard(path)) for path in jars("lib")]
)
unparsable_files = [r[0] for r in parsings if r[1] == None]
if unparsable_files:
print("The following files could not be parsed:")
for f in unparsable_files:
print("| - " + f)
print("Make sure the files are in the following format: groupId.artifactId[.source]_version[.SNAPSHOT].jar")
parsings = [p for p in parsings if p[1] != None]
for (path, parsing) in parsings:
install(path, parsing)
if args.delete:
os.remove(path)
print(maven_dependencies(parsings))