Skip to content

Commit 5eb93f4

Browse files
committed
add support for mirrors for fedora
1 parent cfed450 commit 5eb93f4

1 file changed

Lines changed: 56 additions & 37 deletions

File tree

bin/package_build.py

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/python3
2+
from urllib.parse import unquote
3+
from bs4 import BeautifulSoup
24
import requests
35
import re
46
import sys
@@ -112,46 +114,63 @@ def clefos():
112114
print(f"Saved!\nfilename: {q}")
113115

114116
def fedora():
115-
global DATA,DATA_FILE_LOCATION
117+
global DATA, DATA_FILE_LOCATION
116118
sources = [38, 39, 40]
117-
pkg_reg = r'<a href="(.*)\.rpm"'
119+
pkg_reg = r'<a href="(.*?)\.rpm"'
118120
dirs = '0123456789abcdefghijklmnopqrstuvwxyz'
119-
for i in range(len(sources)):
120-
results = []
121-
q = f'Fedora_{sources[i]}_List.json'
122-
file_name = f'{DATA_FILE_LOCATION}/{q}'
123-
current_link = f'https://dl.fedoraproject.org/pub/fedora-secondary/releases/{sources[i]}/Everything/s390x/os/Packages/'
124-
archived_link = f'https://archives.fedoraproject.org/pub/archive/fedora-secondary/releases/{sources[i]}/Everything/s390x/os/Packages/'
125-
try:
126-
req = requests.get(current_link)
127-
if req.status_code == 404:
128-
current_link = archived_link
129-
req = requests.get(current_link)
130-
if req.status_code == 404:
131-
raise Exception(f"For Fedora {sources[i]}: Current link and Archive link both are down")
132-
else:
133-
print(f'Fedora {sources[i]} has been moved to archive')
134-
except Exception as e:
135-
print("Couldn't pull. Error: ",str(e))
121+
122+
for release in sources:
123+
mirrors = [
124+
f'https://dl.fedoraproject.org/pub/fedora-secondary/releases/{release}/Everything/s390x/os/',
125+
f'https://archives.fedoraproject.org/pub/archive/fedora-secondary/releases/{release}/Everything/s390x/os/'
126+
]
127+
data = requests.get(f'https://mirrors.fedoraproject.org/mirrorlist?repo=fedora-{release}&arch=s390x&country=global')
128+
if data.ok:
129+
soup = BeautifulSoup(data.text, 'html.parser')
130+
mirrors += [line.strip() for line in soup.text.splitlines() if line.startswith(('http', 'ftp'))]
136131
else:
137-
for each in range(len(dirs)):
138-
link = f"{current_link}{dirs[each]}/"
139-
req = requests.get(link)
140-
data = req.text
141-
if req.status_code == 404:
142-
print(f"404 Directory {dirs[each]} not found")
143-
continue
144-
ref_data = re.findall(pkg_reg, data)
145-
results.extend(ref_data)
146-
DATA = open(file_name, 'w')
147-
DATA.write('[\n')
148-
for each in results:
149-
each = each.replace('.s390x', '').replace('.noarch', '')
150-
each = re.sub(r'\.fc\d\d', '', each)
151-
pkg = re.search(r'([\w+\-]+)-([\w\-\.]+)', each)
152-
DATA.write('{"packageName": "'+pkg.group(1)+'","version": "'+pkg.group(2)+'"},\n')
153-
DATA.write('{}\n]')
154-
DATA.close()
132+
print('Failed to fetch mirrorlist. Using default mirrors.')
133+
134+
results = []
135+
q = f'Fedora_{release}_List.json'
136+
137+
for mirror in mirrors:
138+
all_dirs_processed = False
139+
index = 0
140+
while index < len(dirs):
141+
link = f"{mirror}Packages/{dirs[index]}/"
142+
try:
143+
req = requests.get(link, timeout=10)
144+
if req.status_code == 404:
145+
print(f"404 Directory {dirs[index]} not found at {mirror}")
146+
index+=1
147+
continue
148+
req.raise_for_status()
149+
data = unquote(req.text)
150+
except (requests.exceptions.RequestException, Exception) as e:
151+
print(f"Error fetching {link}: {e}")
152+
break # Move to the next mirror
153+
else:
154+
ref_data = re.findall(pkg_reg, data)
155+
results.extend(ref_data)
156+
index+=1
157+
else:
158+
# Completed without error, all directories processed for this mirror
159+
all_dirs_processed = True
160+
break # Break the outer mirror loop
161+
162+
if not all_dirs_processed:
163+
raise Exception(f"Failed to process all directories for Fedora {release}. All mirrors attempted.")
164+
165+
with open(f'{DATA_FILE_LOCATION}/{q}', 'w') as DATA:
166+
DATA.write('[\n')
167+
for each in results:
168+
each = each.replace('.s390x', '').replace('.noarch', '')
169+
each = re.sub(r'\.fc\d\d', '', each)
170+
pkg = re.search(r'([\w+\-]+)-([\w\-\.]+)', each)
171+
DATA.write('{"packageName": "'+pkg.group(1)+'","version": "'+pkg.group(2)+'"},\n')
172+
DATA.write('{}\n]')
173+
DATA.close()
155174
print(f"Saved!\nfilename: {q}")
156175

157176
def almaLinux():

0 commit comments

Comments
 (0)