in tools/dependency_summary.py [0:0]
def _find_license_url(self, id, chosenLicense, licenseFile, pkgInfo):
"""Find an appropriate URL at which humans can view a project's license."""
licenseUrl = pkgInfo.get("license_url")
if licenseUrl is not None:
return licenseUrl
# Try to infer a suitable URL from the local license file
# and github repo metadata.
if urlparse(licenseFile).scheme:
licenseUrl = licenseFile
else:
repo = pkgInfo["repository"]
if repo:
if repo.startswith("http://github.com/"):
repo = repo.replace("http://", "https://")
if repo.startswith("https://github.com/"):
# Some projects include extra context in their repo URL; strip it off.
for strip_suffix in [
".git",
"/tree/main/{}".format(pkgInfo["name"]),
"/tree/master/{}".format(pkgInfo["name"]),
]:
if repo.endswith(strip_suffix):
repo = repo[: -len(strip_suffix)]
# Try a couple of common locations for the license file.
for path in [
"/main/",
"/master/",
"/main/{}/".format(pkgInfo["name"]),
"/master/{}/".format(pkgInfo["name"]),
]:
licenseUrl = repo.replace(
"github.com", "raw.githubusercontent.com"
)
licenseUrl += path + licenseFile
r = requests.get(licenseUrl)
if r.status_code == 200:
# Found it!
# TODO: We could check whether the content matches what was on disk.
break
else:
# No potential URLs were actually live.
licenseUrl = None
if licenseUrl is None:
err = "Could not infer license URL for '{}'.\n".format(pkgInfo["name"])
err += "Please locate the correct license URL and add it to `PACKAGE_METADATA_FIXUPS`.\n"
err += f"You may need to poke around in the source repository at {repo}"
err += f" for a {chosenLicense} license file named {licenseFile}."
raise RuntimeError(err)
# As a special case, convert raw github URLs back into human-friendly page URLs.
if licenseUrl.startswith("https://raw.githubusercontent.com/"):
licenseUrl = re.sub(
r"raw.githubusercontent.com/([^/]+)/([^/]+)/",
r"github.com/\1/\2/blob/",
licenseUrl,
)
return licenseUrl