Skip to content

Commit d18e424

Browse files
committed
support gitlab.com and git.bam.de with auth support
1 parent ae4dc58 commit d18e424

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

pydeps2env/environment.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
else:
1616
import tomllib
1717

18+
from .helpers import extract_url_user_auth, guess_suffix_from_url
1819

1920
def clean_list(item: list, sort: bool = True) -> list:
2021
"""Remove duplicate entries from a list."""
@@ -118,22 +119,35 @@ def _read_source(self):
118119
self.filename, extras = split_extras(self.filename)
119120
self.extras |= set(extras)
120121

121-
# store suffix for later parsing
122-
self._suffix = Path(self.filename).suffix
123-
124122
# read file contents into bytes
125123
_filename = self.filename
126124
if isinstance(_filename, str) and _filename.startswith("http"):
127125
import urllib.request
128126

127+
_token = None
128+
129+
# site specific url parsing
129130
if "/github.com/" in _filename:
130131
_filename = _filename.replace(
131132
"/github.com/", "/raw.githubusercontent.com/"
132133
)
133134
_filename = _filename.replace("/blob/", "/")
134-
with urllib.request.urlopen(_filename) as f:
135+
elif "git.bam.de" in _filename or "gitlab.com" in _filename:
136+
_filename, _, _token = extract_url_user_auth(_filename)
137+
138+
req = urllib.request.Request(_filename)
139+
if _token:
140+
req.add_header("PRIVATE-TOKEN", _token)
141+
142+
# store suffix for later parsing
143+
self._suffix = guess_suffix_from_url(_filename)
144+
145+
with urllib.request.urlopen(req) as f:
135146
_contents: bytes = f.read() # read raw content into bytes
136147
else:
148+
# store suffix for later parsing
149+
self._suffix = Path(self.filename).suffix
150+
137151
with open(_filename, "rb") as f:
138152
_contents: bytes = f.read()
139153

pydeps2env/helpers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Helper utilities for url parsing etc. ."""
2+
3+
def extract_url_user_auth(url) -> tuple[str, str, str]:
4+
"""Extract basic url, user and authentication from url scheme.
5+
6+
Returns
7+
-------
8+
tuple
9+
Tuple consisting of the url with authentication stripped
10+
and username and password if supplied.
11+
"""
12+
import urllib.parse
13+
14+
split_results = urllib.parse.urlsplit(url=url)
15+
components = [*split_results]
16+
components[1] = components[1].split("@")[-1] # remove user:auth info
17+
return urllib.parse.urlunsplit(components), split_results.username, split_results.password
18+
19+
def guess_suffix_from_url(url) -> str:
20+
"""Try to extract filename suffix from url."""
21+
22+
return "." + url.split(".")[-1].split("/")[0]

0 commit comments

Comments
 (0)