|
| 1 | +""" |
| 2 | +Release script. |
| 3 | +""" |
| 4 | +import argparse |
| 5 | +import sys |
| 6 | +from subprocess import check_call |
| 7 | + |
| 8 | +from colorama import init, Fore |
| 9 | +from git import Repo, Remote |
| 10 | + |
| 11 | + |
| 12 | +def create_branch(version): |
| 13 | + """Create a fresh branch from upstream/master""" |
| 14 | + repo = Repo.init('.') |
| 15 | + if repo.is_dirty(untracked_files=True): |
| 16 | + raise RuntimeError(f'Repository is dirty, please commit/stash your changes.') |
| 17 | + |
| 18 | + branch_name = f"release-{version}" |
| 19 | + print(f"{Fore.CYAN}Create {branch_name} branch from upstream master") |
| 20 | + upstream = get_upstream(repo) |
| 21 | + upstream.fetch() |
| 22 | + release_branch = repo.create_head(branch_name, upstream.refs.master, force=True) |
| 23 | + release_branch.checkout() |
| 24 | + return repo |
| 25 | + |
| 26 | + |
| 27 | +def get_upstream(repo: Repo) -> Remote: |
| 28 | + """Find upstream repository for pluggy on the remotes""" |
| 29 | + for remote in repo.remotes: |
| 30 | + for url in remote.urls: |
| 31 | + if url.endswith("pytest-dev/pluggy.git"): |
| 32 | + return remote |
| 33 | + raise RuntimeError("could not find tox-dev/tox.git remote") |
| 34 | + |
| 35 | + |
| 36 | +def pre_release(version): |
| 37 | + """Generates new docs, release announcements and creates a local tag.""" |
| 38 | + repo = create_branch(version) |
| 39 | + changelog(version, write_out=True) |
| 40 | + repo.index.add(['CHANGELOG.rst', 'changelog']) |
| 41 | + repo.index.commit(f"Preparing release {version}") |
| 42 | + |
| 43 | + print() |
| 44 | + print(f"{Fore.GREEN}Please push your branch to your fork and open a PR.") |
| 45 | + |
| 46 | + |
| 47 | +def changelog(version, write_out=False): |
| 48 | + if write_out: |
| 49 | + addopts = [] |
| 50 | + else: |
| 51 | + addopts = ["--draft"] |
| 52 | + print(f"{Fore.CYAN}Generating CHANGELOG") |
| 53 | + check_call(["towncrier", "--yes", "--version", version] + addopts) |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + init(autoreset=True) |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + parser.add_argument("version", help="Release version") |
| 60 | + options = parser.parse_args() |
| 61 | + try: |
| 62 | + pre_release(options.version) |
| 63 | + except RuntimeError as e: |
| 64 | + print(f'{Fore.RED}ERROR: {e}') |
| 65 | + return 1 |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + sys.exit(main()) |
0 commit comments