-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
186 lines (158 loc) · 4.92 KB
/
noxfile.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
import logging
import os
import tempfile
from contextlib import (
contextmanager,
)
from functools import (
reduce,
)
from pathlib import (
Path,
)
from typing import (
Any,
Generator,
List,
)
import nox
from nox.sessions import (
Session,
)
from setuptools import (
find_namespace_packages,
find_packages,
)
nox.options.sessions = "lint", "mypy", "safety", "test"
SOURCE_LOCATIONS = ["src", "tests", "noxfile.py"]
logger = logging.getLogger(__name__) # type: ignore
@nox.session
def test(session: Session) -> None:
args = session.posargs or [
"-m",
"not e2e",
]
session.run(
"poetry",
"install",
"--with",
"testing",
"--without",
"dev,linting",
external=True,
)
session.run("pytest", *args)
@nox.session
def lint(session: Session) -> None:
args = session.posargs or SOURCE_LOCATIONS
session.run(
"poetry",
"install",
"--with",
"linting",
"--without",
"dev,testing",
external=True,
)
session.run("flake8", *args)
@nox.session
def safety(session: Session) -> None:
with temporary_file() as requirements:
export_poetry_requirements(session, requirements)
session.install("safety")
session.run("safety", "check", f"--file={requirements}", "--full-report")
@nox.session
def mypy(session: Session) -> None:
args = session.posargs or SOURCE_LOCATIONS
# Install the whole project in virutal env for running mypy, to ensure we have
# library stubs of dependencies.
# It allows to limit the use of 'ignore_missing_imports' in mypy.ini
session.run("poetry", "install", external=True)
# Unfortunately, mypy may not work as expected with namespace packages
# We enforce the presence of __init__.py file in all subpackages to avoid any issue
with temporary_init_files_in_namespace_packages(args):
session.run("mypy", *args)
@contextmanager
def temporary_file() -> Generator[str, None, None]:
# On windows we cannot use tempfile.NamedTemporaryFile() directly because
# the file cannot be written while still open
tmpf = tempfile.NamedTemporaryFile(delete=False)
tmpf.close()
try:
yield tmpf.name
finally:
os.unlink(tmpf.name)
def export_poetry_requirements(
session: Session,
file_name: str,
groups: str = "dev,linting,testing",
) -> None:
session.run(
"poetry",
"export",
"--with",
groups,
"--format=requirements.txt",
f"--output={file_name}",
external=True,
)
def install_with_constraints(
session: Session,
*args: str,
**kwargs: Any, # noqa: ANN401 typing.Any disallowed
) -> None:
with temporary_file() as requirements:
export_poetry_requirements(session, requirements)
session.install(f"--constraint={requirements}", *args, **kwargs)
@contextmanager
def temporary_init_files_in_namespace_packages(
source_locations: List[str],
) -> Generator[List[Path], None, None]:
"""
Convert all namespace packages to packages in specified source locations.
Returns the list of __init__.py files created that way.
"""
init_files = []
for source_location in source_locations:
if not Path(source_location).is_dir():
continue
root_dir = source_location
found_packages = find_packages(root_dir)
found_ns_packages = find_namespace_packages(root_dir)
for package in set(found_ns_packages) - set(found_packages):
subdir = reduce(
lambda parent_dir, subdir: parent_dir / subdir,
package.split("."),
Path(root_dir),
)
init_file = subdir / "__init__.py"
if not Path(init_file).exists():
init_files.append(init_file)
for init_file in init_files:
with open(init_file, "w") as init_file_io:
logger.info(f"Creating temporary empty init file {init_file}")
init_file_io.write("# empty init file for mypy\n")
# Count total number of python files so we can check if mypy process the same amount
logger.info(
f"Total number of python files: {_count_python_files(source_locations)}"
)
try:
yield init_files
finally:
for init_file in init_files:
logger.info(f"Removing temporary empty init file {init_file}")
init_file.unlink()
def _count_python_files(
source_locations: List[str],
) -> int:
python_file_count = 0
for source_location in source_locations:
path = Path(source_location)
if path.is_file() and path.suffix == ".py":
python_file_count += 1
elif path.is_dir():
for _root, _dirs, files in os.walk(source_location):
for f in files:
if Path(f).suffix == ".py":
python_file_count += 1
return python_file_count