|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | +from github import Github |
| 5 | +from typing import List, Dict |
| 6 | +import itertools |
| 7 | +import requests |
| 8 | + |
| 9 | +HTML_TEMPLATE = """<!DOCTYPE html> |
| 10 | + <html> |
| 11 | + <head> |
| 12 | + <title>{package_name}</title> |
| 13 | + </head> |
| 14 | + <body> |
| 15 | + <h1>{package_name}</h1> |
| 16 | + {package_links} |
| 17 | + </body> |
| 18 | + </html> |
| 19 | +""" |
| 20 | + |
| 21 | +class PackageIndexBuilder: |
| 22 | + def __init__(self, token: str, repo_name: str, output_dir: str): |
| 23 | + self.github = Github(token) |
| 24 | + self.repo_name = repo_name |
| 25 | + self.output_dir = Path(output_dir) |
| 26 | + self.packages: Dict[str, List[Dict]] = {} |
| 27 | + |
| 28 | + # Set up authenticated session |
| 29 | + self.session = requests.Session() |
| 30 | + self.session.headers.update({ |
| 31 | + "Authorization": f"token {token}", |
| 32 | + "Accept": "application/octet-stream", |
| 33 | + }) |
| 34 | + |
| 35 | + def collect_packages(self): |
| 36 | + |
| 37 | + print ("Query release assets") |
| 38 | + repo = self.github.get_repo(self.repo_name) |
| 39 | + |
| 40 | + for release in repo.get_releases(): |
| 41 | + for asset in release.get_assets(): |
| 42 | + if asset.name.endswith(('.whl', '.tar.gz')): |
| 43 | + package_name = asset.name.split('-')[0].replace('_', '-') |
| 44 | + if package_name not in self.packages: |
| 45 | + self.packages[package_name] = [] |
| 46 | + |
| 47 | + self.packages[package_name].append({ |
| 48 | + 'filename': asset.name, |
| 49 | + 'url': asset.url, |
| 50 | + 'size': asset.size, |
| 51 | + 'upload_time': asset.created_at.strftime('%Y-%m-%d %H:%M:%S'), |
| 52 | + }) |
| 53 | + |
| 54 | + def generate_index_html(self): |
| 55 | + # Generate main index |
| 56 | + package_list = self.packages.keys() |
| 57 | + main_index = HTML_TEMPLATE.format( |
| 58 | + package_name="Simple Package Index", |
| 59 | + package_links="\n".join([f'<a href="{x}/">{x}</a><br/>' for x in package_list]) |
| 60 | + ) |
| 61 | + |
| 62 | + with open(self.output_dir / "index.html", "w") as f: |
| 63 | + f.write(main_index) |
| 64 | + |
| 65 | + for package, assets in self.packages.items(): |
| 66 | + |
| 67 | + package_dir = self.output_dir / package |
| 68 | + package_dir.mkdir(exist_ok=True) |
| 69 | + |
| 70 | + # Generate package-specific index.html |
| 71 | + file_links = [] |
| 72 | + assets = sorted(assets, key=lambda x: x["filename"]) |
| 73 | + for filename, items in itertools.groupby(assets, key=lambda x: x["filename"]): |
| 74 | + file_links.append(f'<a href="./{filename}">{filename}</a><br/>') |
| 75 | + url = next(items)['url'] |
| 76 | + |
| 77 | + # Download the file |
| 78 | + with open(package_dir / filename, 'wb') as f: |
| 79 | + print (f"Downloading '{filename}' from '{url}'") |
| 80 | + response = self.session.get(url, stream=True) |
| 81 | + response.raise_for_status() |
| 82 | + for chunk in response.iter_content(chunk_size=8192): |
| 83 | + if chunk: |
| 84 | + f.write(chunk) |
| 85 | + |
| 86 | + package_index = HTML_TEMPLATE.format( |
| 87 | + package_name=package, |
| 88 | + package_links="\n".join(file_links) |
| 89 | + ) |
| 90 | + |
| 91 | + with open(package_dir / "index.html", "w") as f: |
| 92 | + f.write(package_index) |
| 93 | + |
| 94 | + def build(self): |
| 95 | + # Create output directory |
| 96 | + self.output_dir.mkdir(parents=True, exist_ok=True) |
| 97 | + |
| 98 | + # Collect and generate |
| 99 | + self.collect_packages() |
| 100 | + self.generate_index_html() |
| 101 | + |
| 102 | + |
| 103 | +def main(): |
| 104 | + # Get environment variables |
| 105 | + token = os.environ.get("GITHUB_TOKEN") |
| 106 | + repo = os.environ.get("GITHUB_REPOSITORY") |
| 107 | + print (repo) |
| 108 | + output_dir = os.environ.get("OUTPUT_DIR", "dist") |
| 109 | + |
| 110 | + if not all([token, repo]): |
| 111 | + print ("Missing required environment variables") |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + builder = PackageIndexBuilder(token, repo, output_dir) |
| 115 | + builder.build() |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments