Skip to content

Commit

Permalink
get cfep list from repo archive, not api
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimergp committed Feb 16, 2024
1 parent df26c0f commit bc6667a
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion .ci_scripts/generate_cfep_index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import re
import requests
import sys
import tarfile
from dataclasses import dataclass
from pathlib import Path
from tempfile import TemporaryDirectory

REPO_URL = "https://github.com/conda-forge/cfep"
REPO_ARCHIVE = "https://github.com/conda-forge/cfep/archive/main.tar.gz"
REPO_CONTENTS = "https://api.github.com/repos/conda-forge/cfep/contents/"
TITLE_PATTERN = "<td>\s*Title\s*</td><td>\s*(.*)\s*</td>"
STATUS_PATTERN = "<td>\s*Status\s*</td><td>\s*(.*)\s*</td>"
Expand Down Expand Up @@ -32,7 +36,7 @@ def md_link(self) -> str:
)


def get_cfeps():
def get_cfeps_from_gh_api():
"""Generator that returns all CFEPs from GitHub repo"""
response = requests.get(
REPO_CONTENTS, headers={"Accept": "application/vnd.github.v3+json"}
Expand All @@ -57,6 +61,37 @@ def get_cfeps():
yield Cfep(content["name"], title, status, content["html_url"])


def get_cfeps():
"""Return a generator of CFEPs, by traversing the contents of the repo archive"""
r = requests.get(REPO_ARCHIVE, stream=True)
r.raise_for_status()
with TemporaryDirectory() as tmp:
# Write the tarball to a temporary directory
tarball = Path(tmp) / "cfep.tar.gz"
with tarball.open("wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
# Extract the tarball
extracted_dir = Path(tmp) / "cfep"
extracted_dir.mkdir()
with tarfile.open(tarball) as tar:
tar.extractall(extracted_dir)
# Traverse the extracted directory and return all CFEPs
for cfep in sorted(extracted_dir.rglob("cfep-*.md")):
name = cfep.name
url = f"{REPO_URL}/blob/main/{name}"
if name == "cfep-00.md":
# Hardcode title and status for CFEP-00
yield Cfep(name, "CFEP Template", "Proposed", url)
continue
cfep_text = cfep.read_text()
m = re.search(TITLE_PATTERN, cfep_text)
title = m.group(1).strip() if m else ""
m = re.search(STATUS_PATTERN, cfep_text)
status = m.group(1).strip() if m else ""
yield Cfep(name, title, status, url)


def write_cfep_index():
contents = CFEP_INDEX_RST.read_text()
if ".. REPLACE-THIS-LINE-WITH-THE-INDEX-OF-CFEPs" not in contents:
Expand Down

0 comments on commit bc6667a

Please sign in to comment.