1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#!/usr/bin/env python3
"""
This script updates the changelog for a single module.
"""
import argparse
import datetime
import os
import re
import subprocess
from pathlib import Path
from stub_uploader.const import CHANGELOG_PATH
THIRD_PARTY_NAMESPACE = "stubs"
def main() -> None:
args = parse_args()
update_changelog(
args.typeshed_dir,
args.previous_commit,
args.distribution,
args.version,
dry_run=args.dry_run,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("typeshed_dir", help="Path to typeshed checkout directory")
parser.add_argument(
"previous_commit", help="Previous typeshed commit for which we performed upload"
)
parser.add_argument("distribution", help="Distribution to update the changelog for")
parser.add_argument("version", help="New version to add to the changelog")
parser.add_argument(
"--dry-run",
action="store_true",
help="Should we perform a dry run (don't actually update)",
)
return parser.parse_args()
def update_changelog(
typeshed_dir: str,
previous_commit: str,
distribution: str,
version: str,
*,
dry_run: bool = False,
) -> None:
dist_path = Path(THIRD_PARTY_NAMESPACE, distribution)
result = subprocess.run(
["git", "log", f"{previous_commit}..HEAD", "--", dist_path],
cwd=typeshed_dir,
stdout=subprocess.PIPE,
check=True,
)
log = result.stdout.decode("utf-8")
if not log:
print(f"{distribution}: Changelog unchanged")
return
new_entry = process_git_log(log, version)
changelog = os.path.join(CHANGELOG_PATH, f"{distribution}.md")
try:
with open(changelog) as f:
old_entries = f.read()
except FileNotFoundError:
old_entries = ""
if dry_run:
if old_entries:
print(
f"Would add {len(new_entry.splitlines())} lines to existing {changelog}"
)
else:
print(f"Would add {len(new_entry.splitlines())} lines to new {changelog}")
return
with open(changelog, "w") as f:
f.write(new_entry + old_entries)
def process_git_log(log: str, version: str) -> str:
today = datetime.date.today()
entry = f"## {version} ({today:%Y-%m-%d})\n"
for line in log.splitlines():
if line.strip() == "":
entry += "\n"
elif line.startswith(" "):
# Proper git log lines start with four spaces.
entry += f"{line.rstrip()[4:]}\n"
else:
pass # Ignore header entries.
entry += "\n"
# Strip multiple empty lines.
return re.sub("\n\n+", "\n\n", entry)
if __name__ == "__main__":
main()
|