forked from GLMeece/rpaframework-msgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
240 lines (184 loc) · 6.36 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import platform
import re
import shutil
import subprocess
import toml
from glob import glob
from pathlib import Path
from invoke import task, call, ParseError
from colorama import Fore, Style
from tasks_common import poetry
def _git_root():
output = subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
output = output.decode().strip()
return Path(output)
def _remove_blank_lines(text):
return os.linesep.join([s for s in text.splitlines() if s])
GIT_ROOT = _git_root()
CONFIG = GIT_ROOT / "config"
TOOLS = GIT_ROOT / "tools"
if platform.system() != "Windows":
ACTIVATE_PATH = GIT_ROOT / ".venv" / "bin" / "activate"
ACTIVATE = f"source {ACTIVATE_PATH}"
else:
ACTIVATE_PATH = GIT_ROOT / ".venv" / "Scripts" / "activate"
ACTIVATE = f"{ACTIVATE_PATH}.bat"
CLEAN_PATTERNS = [
".cache",
".pytest_cache",
".venv",
".mypy_cache",
"**/__pycache__",
"**/*.pyc",
"**/*.egg-info",
"tests/output",
"*.libspec",
"*.pkl",
]
EXPECTED_POETRY_CONFIG = {
"virtualenvs": {"in-project": True, "create": True, "path": "null"},
"experimental": {"new-installer": True},
"installer": {"parallel": True},
}
def _is_poetry_configured():
try:
poetry_toml = toml.load(GIT_ROOT / "poetry.toml")
return all(
[
poetry_toml.get(key, None) == value
for key, value in EXPECTED_POETRY_CONFIG.items()
]
)
except FileNotFoundError:
return False
def _run(ctx, app, command, **kwargs):
kwargs.setdefault("echo", True)
if platform.system() != "Windows":
kwargs.setdefault("pty", True)
return ctx.run(f"{app} {command}", **kwargs)
def pip(ctx, command, **kwargs):
return _run(ctx, "pip", command, **kwargs)
def sphinx(ctx, command, **kwargs):
return poetry(ctx, f"run sphinx-build {command}", **kwargs)
def docgen(ctx, command, *flags, **kwargs):
return poetry(ctx, f"run docgen {' '.join(flags)} {command}", **kwargs)
def python_tool(ctx, tool, *args, **kwargs):
if tool[-3:] != ".py":
tool_path = TOOLS / f"{tool}.py"
else:
tool_path = TOOLS / tool
return poetry(ctx, f"run python {tool_path} {' '.join(args)}", **kwargs)
def package_invoke(ctx, directory, command, **kwargs):
with ctx.cd(directory):
return _run(ctx, "invoke", command, **kwargs)
def git(ctx, command, **kwargs):
return _run(ctx, "git", command, **kwargs)
@task()
def clean(ctx, venv=True):
"""Cleans the virtual development environment by
completely removing build artifacts and the .venv.
You can set ``--no-venv`` to avoid this default.
If ``--docs`` is supplied, the build artifacts for
local documentation will also be cleaned.
You can set flag ``all`` to clean all packages as
well.
"""
union_clean_patterns = []
if venv:
union_clean_patterns.extend(CLEAN_PATTERNS)
for pattern in union_clean_patterns:
for path in glob(pattern, recursive=True):
print(f"Removing: {path}")
shutil.rmtree(path, ignore_errors=True)
try:
os.remove(path)
except OSError:
pass
@task
def setup_poetry(ctx, username=None, password=None, token=None, devpi_url=None):
"""Configure local poetry installation for development.
If you provide ``username`` and ``password``, you can
also configure your pypi access. Our version of poetry
uses ``keyring`` so the password is not stored in the
clear.
Alternatively, you can set ``token`` to use a pypi token, be sure
to include the ``pypi-`` prefix in the token.
NOTE: Robocorp developers can use ``https://devpi.robocorp.cloud/ci/test``
as the devpi_url and obtain credentials from the Robocorp internal
documentation.
"""
poetry(ctx, "config -n --local virtualenvs.in-project true")
poetry(ctx, "config -n --local virtualenvs.create true")
poetry(ctx, "config -n --local virtualenvs.path null")
poetry(ctx, "config -n --local experimental.new-installer true")
poetry(ctx, "config -n --local installer.parallel true")
if devpi_url:
poetry(
ctx,
f"config -n --local repositories.devpi.url '{devpi_url}'",
)
if username and password and token:
raise ParseError(
"You cannot specify username-password combination and token simultaneously"
)
if username and password:
poetry(ctx, f"config -n http-basic.pypi {username} {password}")
elif username or password:
raise ParseError("You must specify both username and password")
if token:
poetry(ctx, f"config -n pypi-token.pypi {token}")
@task
def install(ctx):
"""Install development environment. If ``reset`` is set,
poetry will remove untracked packages, reverting the
.venv to the lock file.
If ``reset`` is attempted before an initial install, it
is ignored.
"""
if not _is_poetry_configured():
call(setup_poetry)
poetry(ctx, "install")
@task(install)
def test_python(ctx):
"""Run Python unit-tests."""
poetry(ctx, "run pytest")
@task(install)
def test_robot(ctx):
"""Run Robot Framework tests."""
exclude = "--exclude manual --exclude skip"
poetry(
ctx,
f"run robot -d tests/output {exclude} -L TRACE tests/robot",
)
@task(test_python, test_robot)
def test(_):
"""Run all tests."""
@task(install)
def lint(ctx):
"""Run format checks and static analysis."""
poetry(ctx, "run black --check --diff src tests")
poetry(ctx, f'run flake8 --config {CONFIG / "flake8"} src')
poetry(ctx, f'run pylint -j1 --rcfile {CONFIG / "pylint"} src')
@task(install)
def pretty(ctx):
"""Run code formatter on source files."""
poetry(ctx, "run black src")
@task(install)
def typecheck(ctx):
"""Run static type checks."""
# TODO: Add --strict mode
poetry(ctx, "run mypy src")
@task(lint, test)
def build(ctx):
"""Build distributable python package. (after linting, tests and libspec)"""
poetry(ctx, "build -vv -f sdist")
poetry(ctx, "build -vv -f wheel")
@task(clean, build, help={"ci": "Publish package to devpi instead of PyPI"})
def publish(ctx, ci=False):
"""Publish python package."""
if ci:
poetry(ctx, "publish -v --no-interaction --repository devpi")
else:
poetry(ctx, "publish -v")
ctx.run(f'{TOOLS / "tag.py"}')