forked from Tecnativa/doodba-copier-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
134 lines (115 loc) · 4.56 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
"""Template maintenance tasks.
These tasks are to be executed with https://www.pyinvoke.org/ in Python 3.6+
and are related to the maintenance of this template project, not the child
projects generated with it.
"""
import re
import tempfile
from pathlib import Path
from unittest import mock
from invoke import task
from invoke.util import yaml
TEMPLATE_ROOT = Path(__file__).parent.resolve()
ESSENTIALS = ("git", "python3", "poetry")
def _load_copier_conf():
"""Load copier.yml."""
with open("copier.yml") as copier_fd:
# HACK https://stackoverflow.com/a/44875714/1468388
# TODO Remove hack when https://github.com/pyinvoke/invoke/issues/708 is fixed
with mock.patch.object(
yaml.reader.Reader,
"NON_PRINTABLE",
re.compile(
"[^\x09\x0A\x0D\x20-\x7E\x85\xA0-"
"\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]"
),
):
return yaml.safe_load(copier_fd)
@task
def check_dependencies(c):
"""Check essential development dependencies are present."""
failures = []
for dependency in ESSENTIALS:
try:
c.run(f"{dependency} --version", hide=True)
except Exception:
failures.append(dependency)
if failures:
print(f"Missing essential dependencies: {failures}")
@task(check_dependencies)
def develop(c):
"""Set up a development environment."""
with c.cd(str(TEMPLATE_ROOT)):
c.run("git submodule update --init --checkout --recursive")
# Use poetry to set up development environment in a local venv
c.run("poetry install")
c.run("poetry run pre-commit install")
@task(develop)
def lint(c, verbose=False):
"""Lint & format source code."""
flags = ["--show-diff-on-failure", "--all-files", "--color=always"]
if verbose:
flags.append("--verbose")
flags = " ".join(flags)
with c.cd(str(TEMPLATE_ROOT)):
c.run(f"poetry run pre-commit run {flags}")
@task(develop)
def test(c, verbose=False, sequential=False, docker=True):
"""Test project.
Add --sequential to run only sequential tests, with parallelization disabled.
"""
flags = ["--color=yes"]
if verbose:
flags.append("-vv")
if not docker:
flags.append("--skip-docker-tests")
if sequential:
flags.extend(["-m", "sequential"])
else:
flags.extend(["-n", "auto", "-m", '"not sequential"'])
flags = " ".join(flags)
cmd = f"poetry run pytest {flags} tests"
with c.cd(str(TEMPLATE_ROOT)):
c.run(cmd)
@task(develop)
def update_test_samples(c):
"""Update test samples renderings.
Since this project is just a template, some render results are stored as
tests to be able to check the templates produce the right results.
However, when something changes, the samples must be properly updated and
reviewed to make sure they are still OK.
This job updates all those samples.
"""
with c.cd(str(TEMPLATE_ROOT)):
# Make sure git repo is clean
try:
c.run("git diff --quiet --exit-code")
except Exception:
print("git repo is dirty; clean it and repeat")
raise
copier_conf = _load_copier_conf()
odoo_versions = copier_conf["odoo_version"]["choices"]
samples = Path("tests", "samples")
for odoo_version in odoo_versions:
with tempfile.TemporaryDirectory(
prefix="dct-samples"
) as dct_copy_path, tempfile.TemporaryDirectory(
prefix="oca-samples"
) as oca_copy_path:
c.run(
"poetry run copier -fr HEAD -x '**' -x '!.pylintrc*' -x '!tasks.py' -x '!common.yaml' "
f"-d odoo_version={odoo_version} copy . {dct_copy_path}"
)
oca_template_version = odoo_version if odoo_version >= 13 else "13.0"
c.run(
"poetry run copier -fr HEAD -x '**' -x '!.pylintrc*' "
f"-d odoo_version={oca_template_version} copy vendor/oca-addons-repo-template {oca_copy_path}"
)
for file_name in (".pylintrc", ".pylintrc-mandatory"):
with open(
samples / "mqt-diffs" / f"v{odoo_version}-{file_name}.diff", "w"
) as fd:
copied = Path(dct_copy_path, file_name)
mqt = Path(oca_copy_path, file_name)
fd.write(c.run(f"diff {copied} {mqt}", warn=True).stdout)
c.run("poetry run pre-commit run -a", warn=True)