|
8 | 8 | # ------------------------------------------------------------------------ |
9 | 9 | # Supporting library for harvesting metadata about Maven components. |
10 | 10 | # Requires direct access to the backing storage of the repositories. |
| 11 | +# |
| 12 | +# When run from the command line, generates a data structure with information |
| 13 | +# about the components and repositories of the SciJava component collection. |
11 | 14 |
|
12 | | -import datetime, logging, os, pathlib, re |
| 15 | +import datetime, json, logging, os, pathlib, re, sys |
13 | 16 | import xml.etree.ElementTree as ET |
14 | 17 |
|
| 18 | +# -- Constants -- |
| 19 | + |
15 | 20 | storage = "/opt/sonatype-work/nexus/storage" |
16 | 21 | release_repos = ["releases", "thirdparty", "sonatype", "central", "ome-releases"] |
17 | 22 | snapshot_repos = ["snapshots", "sonatype-snapshots", "ome-snapshots"] |
18 | 23 |
|
19 | 24 | ts_allowance = 10 # maximum seconds difference in SNAPSHOT timestamp |
20 | 25 |
|
| 26 | +# -- Classes -- |
| 27 | + |
21 | 28 | class XML: |
22 | 29 |
|
23 | 30 | def __init__(self, source): |
@@ -191,3 +198,79 @@ def _pom(repos, g, a, v, ts=None): |
191 | 198 | if os.path.exists(path): |
192 | 199 | return MavenPOM(path) |
193 | 200 | return None |
| 201 | + |
| 202 | +# -- Functions -- |
| 203 | + |
| 204 | +def resource_path(source): |
| 205 | + return None if source is None else source[len(storage):] |
| 206 | + |
| 207 | +def status(c): |
| 208 | + """ |
| 209 | + Gathers information from Maven about the given groupId:artifactId. |
| 210 | + """ |
| 211 | + record = { |
| 212 | + "groupId": c.groupId, |
| 213 | + "artifactId": c.artifactId |
| 214 | + } |
| 215 | + record["release"] = None if c.release is None else { |
| 216 | + "source": resource_path(c.release.source), |
| 217 | + "groupId": c.release.groupId, |
| 218 | + "artifactId": c.release.artifactId, |
| 219 | + "lastUpdated": c.release.lastUpdated, |
| 220 | + "latest": c.release.latest, |
| 221 | + "lastVersion": c.release.lastVersion, |
| 222 | + "release": c.release.release, |
| 223 | + } |
| 224 | + record["snapshot"] = None if c.snapshot is None else { |
| 225 | + "source": resource_path(c.snapshot.source), |
| 226 | + "groupId": c.snapshot.groupId, |
| 227 | + "artifactId": c.snapshot.artifactId, |
| 228 | + "lastUpdated": c.snapshot.lastUpdated, |
| 229 | + "latest": c.snapshot.latest, |
| 230 | + "lastVersion": c.snapshot.lastVersion, |
| 231 | + "release": c.snapshot.release, |
| 232 | + } |
| 233 | + record["pom"] = None if c.pom is None else { |
| 234 | + "source": resource_path(c.pom.source), |
| 235 | + "groupId": c.pom.groupId, |
| 236 | + "artifactId": c.pom.artifactId, |
| 237 | + "version": c.pom.version, |
| 238 | + "scm": c.pom.scmURL, |
| 239 | + "issues": c.pom.issuesURL, |
| 240 | + "ci": c.pom.ciURL, |
| 241 | + "developers": c.pom.developers, |
| 242 | + } |
| 243 | + return record |
| 244 | + |
| 245 | +def matches(g, a, patterns): |
| 246 | + return not patterns or any(re.match(pat, f"{g}:{a}") for pat in patterns) |
| 247 | + |
| 248 | +def process(patterns=[]): |
| 249 | + g = "org.scijava" |
| 250 | + a = "pom-scijava" |
| 251 | + psj = MavenComponent(g, a) |
| 252 | + |
| 253 | + if not psj.release and not psj.snapshot and not psj.pom: |
| 254 | + return None |
| 255 | + |
| 256 | + records = [] |
| 257 | + |
| 258 | + if matches(g, a, patterns): |
| 259 | + records.append(status(psj)) |
| 260 | + |
| 261 | + for dep in psj.pom.elements("dependencyManagement/dependencies/dependency"): |
| 262 | + g = dep.find("groupId").text |
| 263 | + a = dep.find("artifactId").text |
| 264 | + |
| 265 | + if matches(g, a, patterns): |
| 266 | + c = MavenComponent(g, a) |
| 267 | + records.append(status(c)) |
| 268 | + |
| 269 | + return records |
| 270 | + |
| 271 | +if __name__ == '__main__': |
| 272 | + result = process(sys.argv[1:]) |
| 273 | + if result: |
| 274 | + print(json.dumps(result, sort_keys=True, indent=4)) |
| 275 | + else: |
| 276 | + print("This script must be run from the SciJava Maven server.") |
0 commit comments