| 1 |
|
| 2 |
"""A script to publish a release of cli_helpers to PyPI.""" |
| 3 |
|
| 4 |
import io |
| 5 |
from optparse import OptionParser |
| 6 |
import re |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
|
| 10 |
import click |
| 11 |
|
| 12 |
DEBUG = False |
| 13 |
CONFIRM_STEPS = False |
| 14 |
DRY_RUN = False |
| 15 |
|
| 16 |
|
| 17 |
def skip_step(): |
| 18 |
""" |
| 19 |
Asks for user's response whether to run a step. Default is yes. |
| 20 |
:return: boolean |
| 21 |
""" |
| 22 |
global CONFIRM_STEPS |
| 23 |
|
| 24 |
if CONFIRM_STEPS: |
| 25 |
return not click.confirm("--- Run this step?", default=True) |
| 26 |
return False |
| 27 |
|
| 28 |
|
| 29 |
def run_step(*args): |
| 30 |
""" |
| 31 |
Prints out the command and asks if it should be run. |
| 32 |
If yes (default), runs it. |
| 33 |
:param args: list of strings (command and args) |
| 34 |
""" |
| 35 |
global DRY_RUN |
| 36 |
|
| 37 |
cmd = args |
| 38 |
print(" ".join(cmd)) |
| 39 |
if skip_step(): |
| 40 |
print("--- Skipping...") |
| 41 |
elif DRY_RUN: |
| 42 |
print("--- Pretending to run...") |
| 43 |
else: |
| 44 |
subprocess.check_output(cmd) |
| 45 |
|
| 46 |
|
| 47 |
def version(version_file): |
| 48 |
_version_re = re.compile( |
| 49 |
r'__version__\s+=\s+(?P<quote>[\'"])(?P<version>.*)(?P=quote)' |
| 50 |
) |
| 51 |
|
| 52 |
with io.open(version_file, encoding="utf-8") as f: |
| 53 |
ver = _version_re.search(f.read()).group("version") |
| 54 |
|
| 55 |
return ver |
| 56 |
|
| 57 |
|
| 58 |
def commit_for_release(version_file, ver): |
| 59 |
run_step("git", "reset") |
| 60 |
run_step("git", "add", version_file) |
| 61 |
run_step("git", "commit", "--message", "Releasing version {}".format(ver)) |
| 62 |
|
| 63 |
|
| 64 |
def create_git_tag(tag_name): |
| 65 |
run_step("git", "tag", tag_name) |
| 66 |
|
| 67 |
|
| 68 |
def create_distribution_files(): |
| 69 |
run_step("python", "setup.py", "clean", "--all", "sdist", "bdist_wheel") |
| 70 |
|
| 71 |
|
| 72 |
def upload_distribution_files(): |
| 73 |
run_step("twine", "upload", "dist/*") |
| 74 |
|
| 75 |
|
| 76 |
def push_to_github(): |
| 77 |
run_step("git", "push", "origin", "main") |
| 78 |
|
| 79 |
|
| 80 |
def push_tags_to_github(): |
| 81 |
run_step("git", "push", "--tags", "origin") |
| 82 |
|
| 83 |
|
| 84 |
def checklist(questions): |
| 85 |
for question in questions: |
| 86 |
if not click.confirm("--- {}".format(question), default=False): |
| 87 |
sys.exit(1) |
| 88 |
|
| 89 |
|
| 90 |
if __name__ == "__main__": |
| 91 |
if DEBUG: |
| 92 |
subprocess.check_output = lambda x: x |
| 93 |
|
| 94 |
checks = [ |
| 95 |
"Have you updated the AUTHORS file?", |
| 96 |
"Have you updated the `Usage` section of the README?", |
| 97 |
] |
| 98 |
checklist(checks) |
| 99 |
|
| 100 |
ver = version("cli_helpers/__init__.py") |
| 101 |
print("Releasing Version:", ver) |
| 102 |
|
| 103 |
parser = OptionParser() |
| 104 |
parser.add_option( |
| 105 |
"-c", |
| 106 |
"--confirm-steps", |
| 107 |
action="store_true", |
| 108 |
dest="confirm_steps", |
| 109 |
default=False, |
| 110 |
help=( |
| 111 |
"Confirm every step. If the step is not " "confirmed, it will be skipped." |
| 112 |
), |
| 113 |
) |
| 114 |
parser.add_option( |
| 115 |
"-d", |
| 116 |
"--dry-run", |
| 117 |
action="store_true", |
| 118 |
dest="dry_run", |
| 119 |
default=False, |
| 120 |
help="Print out, but not actually run any steps.", |
| 121 |
) |
| 122 |
|
| 123 |
popts, pargs = parser.parse_args() |
| 124 |
CONFIRM_STEPS = popts.confirm_steps |
| 125 |
DRY_RUN = popts.dry_run |
| 126 |
|
| 127 |
if not click.confirm("Are you sure?", default=False): |
| 128 |
sys.exit(1) |
| 129 |
|
| 130 |
commit_for_release("cli_helpers/__init__.py", ver) |
| 131 |
create_git_tag("v{}".format(ver)) |
| 132 |
create_distribution_files() |
| 133 |
push_to_github() |
| 134 |
push_tags_to_github() |
| 135 |
upload_distribution_files() |