Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependency: remove pycurl package #30771

Merged
merged 28 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
23 changes: 1 addition & 22 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ polyline = "*"
# these should be removed
markdown-it-py = "*"
timezonefinder = "*"
pycurl = "*"
setproctitle = "*"


Expand Down Expand Up @@ -153,7 +152,6 @@ sphinx-rtd-theme = "*"
sphinx-sitemap = "*"
tabulate = "*"
tenacity = "*"
types-pycurl = "*"
types-requests = "*"
types-tabulate = "*"
tqdm = "*"
Expand Down
29 changes: 12 additions & 17 deletions system/qcomgpsd/qcomgpsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import itertools
import math
import time
import pycurl
import requests
from requests.exceptions import RequestException
import shutil
import subprocess
import datetime
Expand Down Expand Up @@ -102,27 +103,21 @@ def gps_enabled() -> bool:

def download_assistance():
try:
c = pycurl.Curl()
c.setopt(pycurl.URL, ASSISTANCE_URL)
c.setopt(pycurl.NOBODY, 1)
c.setopt(pycurl.CONNECTTIMEOUT, 2)
c.perform()
bytes_n = c.getinfo(pycurl.CONTENT_LENGTH_DOWNLOAD)
c.close()
response = requests.head(ASSISTANCE_URL, timeout=2)
bytes_n = int(response.headers.get('content-length', 0))

if bytes_n > 1e5:
cloudlog.error("Qcom assistance data larger than expected")
return

response = requests.get(ASSISTANCE_URL, timeout=5, stream=True)
with open(ASSIST_DATA_FILE_DOWNLOAD, 'wb') as fp:
c = pycurl.Curl()
c.setopt(pycurl.URL, ASSISTANCE_URL)
c.setopt(pycurl.CONNECTTIMEOUT, 5)

c.setopt(pycurl.WRITEDATA, fp)
c.perform()
c.close()
os.rename(ASSIST_DATA_FILE_DOWNLOAD, ASSIST_DATA_FILE)
except pycurl.error:
for chunk in response.iter_content(chunk_size=8192):
royjr marked this conversation as resolved.
Show resolved Hide resolved
fp.write(chunk)

os.rename(ASSIST_DATA_FILE_DOWNLOAD, ASSIST_DATA_FILE)

except RequestException:
royjr marked this conversation as resolved.
Show resolved Hide resolved
cloudlog.exception("Failed to download assistance file")
return

Expand Down
75 changes: 29 additions & 46 deletions tools/lib/url_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import time
import threading
import pycurl
from hashlib import sha256
from io import BytesIO
from tenacity import retry, wait_random_exponential, stop_after_attempt
from openpilot.common.file_helpers import atomic_write_in_dir
from openpilot.system.hardware.hw import Paths
import urllib3
royjr marked this conversation as resolved.
Show resolved Hide resolved
from urllib3 import PoolManager, Retry
from urllib3.util import Timeout
# Cache chunk size
K = 1000
CHUNK_SIZE = 1000 * K
Expand Down Expand Up @@ -35,13 +36,14 @@ def __init__(self, url, debug=False, cache=None):
if cache is not None:
self._force_download = not cache

try:
self._curl = self._tlocal.curl
except AttributeError:
self._curl = self._tlocal.curl = pycurl.Curl()
if not self._force_download:
os.makedirs(Paths.download_cache_root(), exist_ok=True)

def _get_http_client(self):
royjr marked this conversation as resolved.
Show resolved Hide resolved
if not hasattr(self._tlocal, 'http_client'):
self._tlocal.http_client = PoolManager()
return self._tlocal.http_client

def __enter__(self):
return self

Expand All @@ -53,17 +55,14 @@ def __exit__(self, exc_type, exc_value, traceback):

@retry(wait=wait_random_exponential(multiplier=1, max=5), stop=stop_after_attempt(3), reraise=True)
def get_length_online(self):
c = self._curl
c.reset()
c.setopt(pycurl.NOSIGNAL, 1)
c.setopt(pycurl.TIMEOUT_MS, 500000)
c.setopt(pycurl.FOLLOWLOCATION, True)
c.setopt(pycurl.URL, self._url)
c.setopt(c.NOBODY, 1)
c.perform()
length = int(c.getinfo(c.CONTENT_LENGTH_DOWNLOAD))
c.reset()
return length
retries = Retry(connect=3, read=2, redirect=5)
royjr marked this conversation as resolved.
Show resolved Hide resolved
timeout = Timeout(connect=5.0, read=50.0)
royjr marked this conversation as resolved.
Show resolved Hide resolved
try:
response = self._get_http_client().request('HEAD', self._url, retries=retries, timeout=timeout, preload_content=False)
length = response.headers.get('content-length', 0)
return int(length)
except urllib3.exceptions.HTTPError as e:
raise e
royjr marked this conversation as resolved.
Show resolved Hide resolved

def get_length(self):
if self._length is not None:
Expand Down Expand Up @@ -117,58 +116,42 @@ def read(self, ll=None):
@retry(wait=wait_random_exponential(multiplier=1, max=5), stop=stop_after_attempt(3), reraise=True)
def read_aux(self, ll=None):
download_range = False
headers = ["Connection: keep-alive"]
headers = {'Connection': 'keep-alive'}
if self._pos != 0 or ll is not None:
if ll is None:
end = self.get_length() - 1
else:
end = min(self._pos + ll, self.get_length()) - 1
if self._pos >= end:
return b""
headers.append(f"Range: bytes={self._pos}-{end}")
headers['Range'] = f"bytes={self._pos}-{end}"
download_range = True

dats = BytesIO()
c = self._curl
c.setopt(pycurl.URL, self._url)
c.setopt(pycurl.WRITEDATA, dats)
c.setopt(pycurl.NOSIGNAL, 1)
adeebshihadeh marked this conversation as resolved.
Show resolved Hide resolved
c.setopt(pycurl.TIMEOUT_MS, 500000)
royjr marked this conversation as resolved.
Show resolved Hide resolved
c.setopt(pycurl.HTTPHEADER, headers)
c.setopt(pycurl.FOLLOWLOCATION, True)
response = self._get_http_client().request(
'GET',
self._url,
headers=headers,
preload_content=False
)

if self._debug:
print("downloading", self._url)

def header(x):
if b'MISS' in x:
print(x.strip())

c.setopt(pycurl.HEADERFUNCTION, header)

def test(debug_type, debug_msg):
print(" debug(%d): %s" % (debug_type, debug_msg.strip()))

c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.DEBUGFUNCTION, test)
t1 = time.time()

c.perform()
royjr marked this conversation as resolved.
Show resolved Hide resolved
ret = response.data

if self._debug:
t2 = time.time()
if t2 - t1 > 0.1:
print(f"get {self._url} {headers!r} {t2 - t1:.f} slow")

response_code = c.getinfo(pycurl.RESPONSE_CODE)
response_code = response.status
if response_code == 416: # Requested Range Not Satisfiable
raise URLFileException(f"Error, range out of bounds {response_code} {headers} ({self._url}): {repr(dats.getvalue())[:500]}")
raise URLFileException(f"Error, range out of bounds {response_code} {headers} ({self._url}): {repr(ret)[:500]}")
if download_range and response_code != 206: # Partial Content
raise URLFileException(f"Error, requested range but got unexpected response {response_code} {headers} ({self._url}): {repr(dats.getvalue())[:500]}")
raise URLFileException(f"Error, requested range but got unexpected response {response_code} {headers} ({self._url}): {repr(ret)[:500]}")
if (not download_range) and response_code != 200: # OK
raise URLFileException(f"Error {response_code} {headers} ({self._url}): {repr(dats.getvalue())[:500]}")
raise URLFileException(f"Error {response_code} {headers} ({self._url}): {repr(ret)[:500]}")

ret = dats.getvalue()
self._pos += len(ret)
return ret

Expand Down
Loading