-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
146 lines (114 loc) · 3.94 KB
/
tasks.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""
tasks module.
"""
import configparser
import os
from invoke import task
@task
def format_python(context, paths=".", git_add=True):
"""
Formats the python source files using the uncompromising black.
Args:
context (invoke.context.Context):
paths (str, optional):
git_add (bool, optional):
"""
context.run("black -l 79 . {!s}".format(paths))
if git_add:
context.run("git add --all")
context.run("git status")
@task
def lint_markdown(context):
"""
Lint markdown files.
Args:
context (invoke.context.Context):
"""
context.run("markdownlint $(find . -iname '*.md')")
@task
def lint_python(context):
"""
Lint python files using flake8 and pylint.
Args:
context (invoke.context.Context):
"""
context.run("flake8")
context.run("pylint $(find . -iname '*.py')")
@task
def release(context, version_part, changelog=False):
"""
release task.
Args:
context (invoke.context.Context):
version_part (str):
changelog (bool, optional):
"""
repo_dirpath = os.path.dirname(os.path.realpath(__file__))
if changelog:
if not os.environ.get("GITHUB_USERNAME", None):
raise OSError("Environment variable 'GITHUB_USERNAME' not set")
changelog_config = os.path.join(
repo_dirpath, ".github_changelog_generator"
)
if not os.path.exists(changelog_config):
changelog_error = "Github Changelog Generator config not found: {}"
raise OSError(changelog_error.format(changelog_config))
# this throws 'command not found' error if needed
context.run("github_changelog_generator --help")
version_parts = ["major", "minor", "patch"]
if version_part.lower() not in version_parts:
version_part_error = "Version Part must be one of {} not {}"
raise ValueError(
version_part_error.format(version_parts, version_part)
)
bumpversion_config = os.path.join(repo_dirpath, ".bumpversion.cfg")
if not os.path.exists(bumpversion_config):
bumpversion_error = "Bumpversion config not found: {}"
raise OSError(bumpversion_error.format(bumpversion_config))
context.run("bump2version {}".format(version_part))
context.run("git push")
context.run("git push --tags")
if changelog:
config = configparser.ConfigParser()
config.read(bumpversion_config)
current_version = config["bumpversion"]["current_version"]
git_commit_changelog = 'git commit --message="Generate changelog: v{}"'
context.run("github_changelog_generator --user $GITHUB_USERNAME")
context.run("git add --all")
context.run(git_commit_changelog.format(current_version))
context.run("git push")
@task
def revert(context, changelog=False):
"""
revert task.
Args:
context (invoke.context.Context):
changelog (bool, optional):
"""
# FIXME: make sure last 2 commits are 'Generate changelog:' and 'Bump version:'
latest_tag = context.run(
"git describe --abbrev=0 --tags", hide=True
).stdout.strip()
number_of_commits = 2 if changelog else 1
prompt = ">>> Are you sure?? You're about to run these commands:"
git_reset_hard = "git reset --hard HEAD~{}".format(number_of_commits)
git_tag_delete = "git tag --delete {}".format(latest_tag)
git_push_delete = "git push --delete origin {}".format(latest_tag)
git_push_force = "git push --force"
user_input = input(
"{}\n{}\n{}\n{}\n{}\n>>> [y/N]".format(
prompt,
git_reset_hard,
git_tag_delete,
git_push_delete,
git_push_force,
)
)
if not user_input.lower().startswith("y"):
raise SystemExit
context.run(git_reset_hard)
context.run(git_tag_delete)
context.run(git_push_delete)
context.run(git_push_force)