Skip to content

Commit 3f0c60f

Browse files
committed
refactor: replace regex with str.startswith and add raw-file URL mapping
Per review suggestion from hugovk: - Replace re.match with str.startswith for clarity (re.match anchors start but not end, which can be confusing) - Also handle hg.python.org/cpython/raw-file/ URLs, mapping them to raw.githubusercontent.com (e.g. Python 2.7.3 release notes use this form)
1 parent f0514fd commit 3f0c60f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

apps/downloads/models.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,29 @@ def corrected_release_notes_url(self):
123123
the "Release notes" links on the downloads page still work for legacy
124124
releases (3.3.6 and earlier).
125125
126-
Example::
126+
Examples::
127127
128128
http://hg.python.org/cpython/file/v3.3.6/Misc/NEWS
129129
→ https://github.com/python/cpython/blob/v3.3.6/Misc/NEWS
130+
131+
http://hg.python.org/cpython/raw-file/v2.7.3/Misc/NEWS
132+
→ https://raw.githubusercontent.com/python/cpython/v2.7.3/Misc/NEWS
130133
"""
131134
url = self.release_notes_url
132135
if not url:
133136
return url
134-
match = re.match(r"https?://hg\.python\.org/cpython/file/([^/]+)/(.+)", url)
135-
if match:
136-
tag, path = match.group(1), match.group(2)
137-
return f"https://github.com/python/cpython/blob/{tag}/{path}"
137+
for prefix in (
138+
"http://hg.python.org/cpython/file/",
139+
"https://hg.python.org/cpython/file/",
140+
):
141+
if url.startswith(prefix):
142+
return "https://github.com/python/cpython/blob/" + url[len(prefix):]
143+
for prefix in (
144+
"http://hg.python.org/cpython/raw-file/",
145+
"https://hg.python.org/cpython/raw-file/",
146+
):
147+
if url.startswith(prefix):
148+
return "https://raw.githubusercontent.com/python/cpython/" + url[len(prefix):]
138149
return url
139150

140151
def download_file_for_os(self, os_slug):

0 commit comments

Comments
 (0)