1#!/usr/bin/env python3
2
3"""
4A script that can be used to query maven.google.com to get information about androidx artifacts to
5see what stable/latest versions are available and when they were released.
6
7Usage:
8./librarystats.py
9
10"""
11
12from datetime import datetime
13import requests
14import re
15import os
16from xml.dom.minidom import parseString
17
18current_directory = os.path.dirname(__file__)
19
20libraries = []
21docs_public_build = os.path.join(current_directory, r'../docs-public/build.gradle')
22
23with open(docs_public_build, "r") as f:
24    build_file_contents = f.read()
25    p = re.compile('\("(androidx\..*)\:[0-9]+\.[0-9]+\.[0-9]+.*"\)')
26    libraries = [val for val in p.findall(build_file_contents) if not val.endswith("-samples")]
27
28cache_directory = os.path.join(current_directory, r'cache')
29if not os.path.exists(cache_directory):
30    os.makedirs(cache_directory)
31
32def getOrDownloadMetadata(library_to_fetch):
33    cache_file_name = "cache/" + library_to_fetch + ".xml"
34    if os.path.isfile(cache_file_name):
35        with open(cache_file_name, "r") as f:
36            return f.read()
37    url = "https://dl.google.com/android/maven2/" + library_to_fetch.replace(".", "/").replace(":", "/") + "/maven-metadata.xml"
38    r = requests.get(url, allow_redirects=True)
39    if not r.ok:
40        return None
41    with open(cache_file_name, "w") as f:
42        f.write(r.text)
43    return r.text
44
45def getOrDownloadUpdatedDate(library_to_fetch, version_to_fetch):
46    cache_file_name = "cache/" + library_to_fetch + "-" + version_to_fetch + ".txt"
47    if os.path.isfile(cache_file_name):
48        with open(cache_file_name, "r") as f:
49            return f.read()
50    artifact_id = library_to_fetch.split(":")[-1]
51    url = "https://dl.google.com/android/maven2/" + library_to_fetch.replace(".", "/").replace(":", "/") + "/" + version_to_fetch + "/" + artifact_id + "-" + version_to_fetch + ".pom"
52    r = requests.get(url, allow_redirects=True)
53    last_updated_pattern = "%a, %d %b %Y %H:%M:%S %Z"
54    timestamp = datetime.strptime(r.headers["Last-Modified"], last_updated_pattern).strftime("%Y %m %d")
55    with open(cache_file_name, "w") as f:
56        f.write(timestamp)
57    return timestamp
58
59print("Library,Latest Stable,Latest Stable Release Date,Latest Version,Latest Version Release Date")
60
61for library in libraries:
62    metadata = getOrDownloadMetadata(library)
63    if metadata is None:
64        print(library + ",-,-,-,-")
65        continue
66    document = parseString(metadata)
67    versions = [a.childNodes[0].nodeValue for a in document.getElementsByTagName("version")]
68    line = library + ","
69    latest_stable_version = None
70    for version in reversed(versions):
71        if "-" not in version:
72            latest_stable_version = version
73            break
74    if latest_stable_version:
75        line += latest_stable_version + "," + getOrDownloadUpdatedDate(library, latest_stable_version) + ","
76    else:
77        line += "-,-,"
78    latest_version = versions[-1]
79    line += latest_version + "," + getOrDownloadUpdatedDate(library, latest_version)
80    print(line)
81