|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Release Post Generator for AboutCode.org |
| 4 | +
|
| 5 | +This script generates a news post file for a new project release. |
| 6 | +It is designed to be used in GitHub Actions workflows. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python3 generate_release_post.py --project "Project Name" --version "1.0.0" --url "https://..." --date "2023-10-27" --output-dir "news" |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import os |
| 14 | +import sys |
| 15 | +from datetime import datetime |
| 16 | + |
| 17 | +TEMPLATE = """ |
| 18 | +{title} |
| 19 | +{title_underline} |
| 20 | +
|
| 21 | +AboutCode is happy to announce the release of **{project} v{version}**! |
| 22 | +
|
| 23 | +Check out the full release notes and download it here: |
| 24 | +{url} |
| 25 | +
|
| 26 | +Visit the project homepage: |
| 27 | +{homepage} |
| 28 | +
|
| 29 | +-- The AboutCode Team |
| 30 | +""" |
| 31 | + |
| 32 | +PROJECT_HOMEPAGES = { |
| 33 | + "ScanCode Toolkit": "https://github.com/aboutcode-org/scancode-toolkit", |
| 34 | + "ScanCode.io": "https://github.com/aboutcode-org/scancode.io", |
| 35 | + "VulnerableCode": "https://github.com/aboutcode-org/vulnerablecode", |
| 36 | + "DejaCode": "https://github.com/aboutcode-org/dejacode", |
| 37 | + "PURLDB": "https://github.com/aboutcode-org/purldb", |
| 38 | +} |
| 39 | + |
| 40 | +def generate_post(project, version, url, date_str, output_dir): |
| 41 | + """Generates the release post file.""" |
| 42 | + |
| 43 | + # Format the title |
| 44 | + title = f"{project} v{version} released" |
| 45 | + title_underline = "=" * len(title) |
| 46 | + |
| 47 | + # Get homepage or default to github organization |
| 48 | + homepage = PROJECT_HOMEPAGES.get(project, "https://github.com/aboutcode-org") |
| 49 | + |
| 50 | + content = TEMPLATE.format( |
| 51 | + title=title, |
| 52 | + title_underline=title_underline, |
| 53 | + project=project, |
| 54 | + version=version, |
| 55 | + url=url, |
| 56 | + homepage=homepage |
| 57 | + ).strip() |
| 58 | + |
| 59 | + # Create filename: YYYY-MM-DD-project-vVERSION-released.rst (Sphinx uses RST usually) |
| 60 | + # Using RST as AboutCode docs are RST-heavy |
| 61 | + safe_project_name = project.lower().replace(" ", "-").replace(".", "") |
| 62 | + filename = f"{date_str}-{safe_project_name}-v{version}-released.rst" |
| 63 | + |
| 64 | + if output_dir: |
| 65 | + os.makedirs(output_dir, exist_ok=True) |
| 66 | + filepath = os.path.join(output_dir, filename) |
| 67 | + else: |
| 68 | + filepath = filename |
| 69 | + |
| 70 | + with open(filepath, "w", encoding="utf-8") as f: |
| 71 | + f.write(content) |
| 72 | + f.write("\n") |
| 73 | + |
| 74 | + print(f"Successfully generated release post: {filepath}") |
| 75 | + return filepath |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + parser = argparse.ArgumentParser(description="Generate AboutCode release post") |
| 79 | + parser.add_argument("--project", required=True, help="Project name (e.g., ScanCode.io)") |
| 80 | + parser.add_argument("--version", required=True, help="Release version (e.g., 32.0.1)") |
| 81 | + parser.add_argument("--url", required=True, help="Release URL") |
| 82 | + parser.add_argument("--date", help="Date in YYYY-MM-DD format (default: today)") |
| 83 | + parser.add_argument("--output-dir", default=".", help="Directory to save the file") |
| 84 | + |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + date_str = args.date if args.date else datetime.now().strftime("%Y-%m-%d") |
| 88 | + |
| 89 | + try: |
| 90 | + generate_post(args.project, args.version, args.url, date_str, args.output_dir) |
| 91 | + except Exception as e: |
| 92 | + print(f"Error: {e}", file=sys.stderr) |
| 93 | + sys.exit(1) |
0 commit comments