agentos-project/agentos

Bug with use of `str.lstrip()` when parsing github repo URL

Closed this issue · 2 comments

andyk commented

To reproduce:

>>> import agentos
>>> ilya_repo = agentos.Repo.from_github("ikostrikov", "pytorch-a2c-ppo-acktr-gail")
>>> ilya_repo.get_local_repo_dir("master")

...throws the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/andyk/Development/agentos/agentos/repo.py", line 299, in get_local_repo_dir
    local_repo_path = self._clone_repo(version)
  File "/Users/andyk/Development/agentos/agentos/repo.py", line 315, in _clone_repo
    porcelain.clone(
  File "/usr/local/Caskroom/miniforge/base/envs/agentos_dev/lib/python3.8/site-packages/dulwich/porcelain.py", line 460, in clone
    fetch_result = fetch(
  File "/usr/local/Caskroom/miniforge/base/envs/agentos_dev/lib/python3.8/site-packages/dulwich/porcelain.py", line 1583, in fetch
    fetch_result = client.fetch(path, r, progress=errstream.write, depth=depth)
  File "/usr/local/Caskroom/miniforge/base/envs/agentos_dev/lib/python3.8/site-packages/dulwich/client.py", line 532, in fetch
    result = self.fetch_pack(
  File "/usr/local/Caskroom/miniforge/base/envs/agentos_dev/lib/python3.8/site-packages/dulwich/client.py", line 1978, in fetch_pack
    refs, server_capabilities, url = self._discover_references(
  File "/usr/local/Caskroom/miniforge/base/envs/agentos_dev/lib/python3.8/site-packages/dulwich/client.py", line 1839, in _discover_references
    resp, read = self._http_request(url, headers, allow_compression=True)
  File "/usr/local/Caskroom/miniforge/base/envs/agentos_dev/lib/python3.8/site-packages/dulwich/client.py", line 2118, in _http_request
    raise HTTPUnauthorized(resp.getheader("WWW-Authenticate"), url)
dulwich.client.HTTPUnauthorized: No valid credentials provided

@nickjalbert is it obvious to you what I'm doing wrong here?

andyk commented

I found the bug causing this.

I believe our use of lstrip in parse_github_web_ui_url() inside agentos/utils.py is incorrect:

stripped_url = github_url.lstrip(URL_STARTS_WITH)

Per https://docs.python.org/3/library/stdtypes.html#str.lstrip "the chars argument [to str.lstrip(chars)] is not a prefix; rather, all combinations of its values are stripped."

I tested this with:

>>> "abc".lstrip("ba")
'c'
>>> "abcabc".lstrip("abc")
''

So the fix will be to instead use str.replace(old, new, count) with count=1.

Good find! Thanks for the fix.