Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion vinca/distro.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import urllib.parse
import urllib.request

from rosdistro import get_cached_distribution, get_index, get_index_url
Expand Down Expand Up @@ -209,11 +210,23 @@ def get_package_xml_for_additional_package(self, pkg_info):
return self._download_raw_pkg_xml_or_cached(url=raw_url)
raise RuntimeError(f"Cannot handle unknown repository hoster: {raw_url_base}")

def _get_auth_headers(self, url):
host = urllib.parse.urlparse(url).netloc
for env_var, domains in (
("GITHUB_TOKEN", ("github.com", "githubusercontent.com")),
("GITLAB_TOKEN", ("gitlab.com",)),
):
token = os.environ.get(env_var)
if token and any(d in host for d in domains):
return {"Authorization": f"token {token}"}
return {}

def _download_raw_pkg_xml_or_cached(self, url):
if url in self._additional_xml_cache:
return self._additional_xml_cache[url]
req = urllib.request.Request(url, headers=self._get_auth_headers(url))
try:
with urllib.request.urlopen(url) as resp:
with urllib.request.urlopen(req) as resp:
xml_content = resp.read().decode("utf-8")
self._additional_xml_cache[url] = xml_content
return xml_content
Expand Down