Skip to content

Instantly share code, notes, and snippets.

@FuegoFro
Created November 20, 2017 18:15
Show Gist options
  • Save FuegoFro/fde6f9c0ecd0cef8a5caf4e8bbfc8bfe to your computer and use it in GitHub Desktop.
Save FuegoFro/fde6f9c0ecd0cef8a5caf4e8bbfc8bfe to your computer and use it in GitHub Desktop.
A script to download the `-sources.jar`s for an Android Gradle Plugin version.
#!/usr/bin/env python3
import argparse
import shutil
import zipfile
from pathlib import Path
from tempfile import NamedTemporaryFile
from xml.etree import ElementTree
import requests
URL_TEMPLATE_TEMPLATE = "https://dl.google.com/dl/android/maven2/" \
"{group}/{artifact}/{version}/{artifact}-{version}{{ending}}"
BASE_GROUP_AND_ARTIFACT = "com.android.tools.build", "gradle"
POM_NAMESPACE = {"pom": "http://maven.apache.org/POM/4.0.0"}
def crawl_sources(plugin_version, destination_dir):
if destination_dir.exists():
shutil.rmtree(str(destination_dir))
destination_dir.mkdir()
download_queue = [BASE_GROUP_AND_ARTIFACT + (plugin_version, )]
downloaded = set()
while download_queue:
to_download = download_queue.pop()
if to_download in downloaded:
continue
downloaded.add(to_download)
group, artifact, version = to_download
print("Handling {}:{}:{}".format(group, artifact, version))
url_template = URL_TEMPLATE_TEMPLATE.format(
group=group.replace(".", "/"),
artifact=artifact,
version=version,
)
# First grab the dependencies for this.
pom_url = url_template.format(ending=".pom")
response = requests.get(pom_url)
if response.status_code == 404:
print("!!! Cannot find {}:{}:{}".format(group, artifact, version))
continue
pom_content = response.content.decode('utf-8')
pom_root = ElementTree.fromstring(pom_content)
dependencies = pom_root.find('pom:dependencies', POM_NAMESPACE)
if dependencies is not None:
for dependency in dependencies.findall('pom:dependency', POM_NAMESPACE):
dep_group = _get_single_tag_content(dependency, "groupId")
dep_artifact = _get_single_tag_content(dependency, "artifactId")
dep_version = _get_single_tag_content(dependency, "version")
download_queue.append((dep_group, dep_artifact, dep_version))
# Now get the content of this.
sources_url = url_template.format(ending="-sources.jar")
with NamedTemporaryFile() as temp_file:
with open(temp_file.name, 'wb') as open_temp_file:
open_temp_file.write(requests.get(sources_url).content)
with zipfile.ZipFile(temp_file.name, 'r') as zip_ref:
zip_ref.extractall(str(destination_dir))
def _get_single_tag_content(current, tag_name):
tags = current.findall("pom:{}".format(tag_name), POM_NAMESPACE)
assert len(tags) == 1
return tags[0].text
def main():
parser = argparse.ArgumentParser()
parser.add_argument("version")
parser.add_argument("--destination", default=str(Path.home()/"tmp_gradle_sources"), required=False)
args = parser.parse_args()
crawl_sources(args.version, Path(args.destination))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment