-
Notifications
You must be signed in to change notification settings - Fork 14
/
noxfile.py
346 lines (309 loc) · 13.6 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from __future__ import annotations
import difflib
import inspect
import json
import os
import tempfile
import typing as t
from contextlib import contextmanager
from pathlib import Path
import nox
if t.TYPE_CHECKING:
from tempfile import _TemporaryFileWrapper
nox.options.reuse_existing_virtualenvs = True
PYTHON_VERSIONS = ["3.9", "3.10", "3.11"]
REQUIREMENTS_CONTENT = {}
@contextmanager
def requirements_file(
session, s3_extra: bool = False
) -> t.Iterator[_TemporaryFileWrapper[bytes]]:
global REQUIREMENTS_CONTENT
extra_args = tuple()
if s3_extra is True:
extra_args = ("-E", "s3")
if REQUIREMENTS_CONTENT.get(s3_extra) is None:
with tempfile.NamedTemporaryFile("rb") as requirements:
session.run(
"poetry",
"export",
"--dev",
*extra_args,
"--without-hashes",
"--format=requirements.txt",
f"--output={requirements.name}",
external=True,
)
REQUIREMENTS_CONTENT[s3_extra] = requirements.read()
yield requirements
else:
with tempfile.NamedTemporaryFile("wb", buffering=0) as requirements:
requirements.write(REQUIREMENTS_CONTENT[s3_extra])
yield requirements
@nox.session(python=PYTHON_VERSIONS)
def tests(session):
with requirements_file(session) as requirements:
session.install("-r", requirements.name)
session.run(
"pytest",
*session.posargs,
env={
"HRFLOW_CONNECTORS_STORE_ENABLED": "1",
"HRFLOW_CONNECTORS_LOCALJSON_DIR": "/tmp/",
"PYTHONPATH": "./src/",
},
)
@nox.session(python=PYTHON_VERSIONS)
def tests_s3(session):
with requirements_file(session, s3_extra=True) as requirements:
session.install("-r", requirements.name)
session.run(
"pytest",
*session.posargs,
env={
"HRFLOW_CONNECTORS_STORE_ENABLED": "1",
"HRFLOW_CONNECTORS_LOCALJSON_DIR": "/tmp/",
"S3_STORE_TEST_BUCKET": os.getenv("S3_STORE_TEST_BUCKET"),
"S3_STORE_TEST_AWS_REGION": os.getenv("S3_STORE_TEST_AWS_REGION"),
"S3_STORE_TEST_AWS_ACCESS_KEY_ID": os.getenv(
"S3_STORE_TEST_AWS_ACCESS_KEY_ID"
),
"S3_STORE_TEST_AWS_SECRET_ACCESS_KEY": os.getenv(
"S3_STORE_TEST_AWS_SECRET_ACCESS_KEY"
),
"S3_STORE_TEST_READ_ONLY_AWS_ACCESS_KEY_ID": os.getenv(
"S3_STORE_TEST_READ_ONLY_AWS_ACCESS_KEY_ID"
),
"S3_STORE_TEST_READ_ONLY_AWS_SECRET_ACCESS_KEY": os.getenv(
"S3_STORE_TEST_READ_ONLY_AWS_SECRET_ACCESS_KEY"
),
"S3_STORE_TEST_WRITE_ONLY_AWS_ACCESS_KEY_ID": os.getenv(
"S3_STORE_TEST_WRITE_ONLY_AWS_ACCESS_KEY_ID"
),
"S3_STORE_TEST_WRITE_ONLY_AWS_SECRET_ACCESS_KEY": os.getenv(
"S3_STORE_TEST_WRITE_ONLY_AWS_SECRET_ACCESS_KEY"
),
"PYTHONPATH": "./src/",
},
)
PRODUCE_MANIFEST_IN_DIRECTORY = (
"from hrflow_connectors.v2 import __CONNECTORS__, hrflow_connectors_manifest as m;"
" m(connectors=__CONNECTORS__, directory_path='{directory}')"
)
@nox.session(python=PYTHON_VERSIONS)
def manifest(session):
with open("./manifest.json", "r") as f:
current_manifest = json.load(f)
with requirements_file(session) as requirements:
session.install("-r", requirements.name)
with tempfile.TemporaryDirectory() as directory:
session.run(
"python",
"-c",
PRODUCE_MANIFEST_IN_DIRECTORY.format(directory=directory),
env={
"PYTHONPATH": "./src/",
},
)
with open(f"{directory}/manifest.json", "rb") as f:
produced_manifest_binary = f.read()
produced_manifest = json.loads(produced_manifest_binary.decode())
if current_manifest != produced_manifest:
with tempfile.NamedTemporaryFile("wb", delete=False) as produced_manifest:
produced_manifest.write(produced_manifest_binary)
session.error(
f"Produced manifest for python{session.python} is "
"different than reference\n"
f"File can be inspected at '{produced_manifest.name}'"
)
GENERATE_DOCUMENTATION = """
import json
import typing as t
{GENERATE_DIGEST_SOURCE}
digest, content = {GENARATE_DIGEST_FUNC_NAME}()
with open("{save_digest_to}", "wt") as f:
json.dump(digest, f)
with open("{save_content_to}", "wt") as f:
json.dump(content, f)
"""
def generate_doc_digest() -> t.Tuple[dict, dict]:
import hashlib
from collections import defaultdict
from unittest import mock
from hrflow_connectors import __CONNECTORS__ as __CONNECTORS__V1
from hrflow_connectors import generate_docs as generate_docs_v1
from hrflow_connectors.v1.core import documentation as documentation_v1
from hrflow_connectors.v2 import __CONNECTORS__ as __CONNECTORS__V2
from hrflow_connectors.v2 import hrflow_connectors_docs as generate_docs_v2
from hrflow_connectors.v2.core import documentation as documentation_v2
doc_digest = dict(
v1=defaultdict[str, dict](lambda: defaultdict(dict)),
v2=defaultdict[str, dict](lambda: defaultdict(dict)),
)
doc_content = dict(
v1=defaultdict[str, dict](lambda: defaultdict(dict)),
v2=defaultdict[str, dict](lambda: defaultdict(dict)),
)
with mock.patch.object(
documentation_v1.Path, "write_bytes", autospec=True
) as mocked_writer_v1:
generate_docs_v1(connectors=__CONNECTORS__V1)
for call in mocked_writer_v1.call_args_list:
args, _ = call
path, data = args
if path.parts[-2:] == ("hrflow-connectors", "README.md"):
doc_digest["v1"]["root"]["readme"] = hashlib.md5(data).hexdigest()
doc_content["v1"]["root"]["readme"] = data.decode() + "\n"
elif path.name == "README.md":
connector = path.parts[-2]
doc_digest["v1"][connector]["readme"] = hashlib.md5(data).hexdigest()
doc_content["v1"][connector]["readme"] = data.decode() + "\n"
else:
connector = path.parts[-3]
action = path.parts[-1].strip(".md")
doc_digest["v1"][connector]["actions"][action] = hashlib.md5(
data
).hexdigest()
doc_content["v1"][connector]["actions"][action] = data.decode() + "\n"
with mock.patch.object(
documentation_v2.Path, "write_bytes", autospec=True
) as mocked_writer_v2:
generate_docs_v2(connectors=__CONNECTORS__V2)
for call in mocked_writer_v2.call_args_list:
args, _ = call
path, data = args
if path.name == "README.md":
connector = path.parts[-2]
doc_digest["v2"][connector]["readme"] = hashlib.md5(data).hexdigest()
doc_content["v2"][connector]["readme"] = data.decode() + "\n"
elif path.name == "connector.pyi":
connector = path.parts[-2]
doc_digest["v2"][connector]["stub"] = hashlib.md5(data).hexdigest()
doc_content["v2"][connector]["stub"] = data.decode() + "\n"
else:
connector = path.parts[-3]
action = path.parts[-1].strip(".md")
doc_digest["v2"][connector]["actions"][action] = hashlib.md5(
data
).hexdigest()
doc_content["v2"][connector]["actions"][action] = data.decode() + "\n"
return doc_digest, doc_content
@nox.session(python=PYTHON_VERSIONS)
def docs(session):
baseline_doc_digest, baseline_content = generate_doc_digest()
with requirements_file(session) as requirements:
session.install("-r", requirements.name)
with tempfile.NamedTemporaryFile("wb") as generated_digest_fp:
with tempfile.NamedTemporaryFile("wb") as generated_content_fp:
with tempfile.NamedTemporaryFile("wt") as script_fp:
script_content = GENERATE_DOCUMENTATION.format(
GENERATE_DIGEST_SOURCE=inspect.getsource(generate_doc_digest),
GENARATE_DIGEST_FUNC_NAME=generate_doc_digest.__name__,
save_digest_to=generated_digest_fp.name,
save_content_to=generated_content_fp.name,
)
script_fp.write(script_content)
script_fp.flush()
session.run(
"python",
script_fp.name,
env={
"PYTHONPATH": "./src/",
},
)
with open(generated_digest_fp.name, "rt") as generated_digest:
generated_digest = json.loads(generated_digest.read())
with open(generated_content_fp.name, "rt") as generated_content:
generated_content = json.loads(generated_content.read())
if generated_digest != baseline_doc_digest:
difference = []
# Root is only handled by v1 code for now
v1_connectors_directory = Path("./src/hrflow_connectors/v1/connectors")
root_readme_digest = baseline_doc_digest["v1"].pop("root")["readme"]
if root_readme_digest != generated_digest["v1"]["root"]["readme"]:
file = str(
v1_connectors_directory / ".." / ".." / ".." / ".." / "README.md"
)
baseline = baseline_content["v1"]["root"]["readme"]
generated = generated_content["v1"]["root"]["readme"]
difference.append(
difflib.unified_diff(
a=baseline.splitlines(keepends=True),
b=generated.splitlines(keepends=True),
fromfile=file,
tofile=session.python,
)
)
for version in ["v1", "v2"]:
connectors_directory = Path(f"./src/hrflow_connectors/{version}/connectors")
for connector, digests in baseline_doc_digest[version].items():
if digests["readme"] != generated_digest[version].get(
connector, {}
).get("readme"):
file = str(connectors_directory / connector / "README.md")
baseline = baseline_content[version][connector]["readme"]
generated = (
generated_content[version].get(connector, {}).get("readme", "")
)
difference.append(
difflib.unified_diff(
a=baseline.splitlines(keepends=True),
b=generated.splitlines(keepends=True),
fromfile=file,
tofile=session.python,
)
)
# Only v2 generates stub files
if version == "v2":
if digests["stub"] != generated_digest[version].get(
connector, {}
).get("stub"):
file = str(connectors_directory / connector / "connector.pyi")
baseline = baseline_content[version][connector]["stub"]
generated = (
generated_content[version]
.get(connector, {})
.get("stub", "")
)
difference.append(
difflib.unified_diff(
a=baseline.splitlines(keepends=True),
b=generated.splitlines(keepends=True),
fromfile=file,
tofile=session.python,
)
)
for action, digest in digests["actions"].items():
if digest != generated_digest[version].get(connector, {}).get(
"actions", {}
).get(action):
file = str(
connectors_directory / connector / "docs" / f"{action}.md"
)
baseline = baseline_content[version][connector]["actions"][
action
]
generated = (
generated_content[version]
.get(connector, {})
.get("actions", {})
.get(action, "")
)
difference.append(
difflib.unified_diff(
a=baseline.splitlines(keepends=True),
b=generated.splitlines(keepends=True),
fromfile=file,
tofile=session.python,
)
)
with tempfile.NamedTemporaryFile(
"wt", suffix=".diff", delete=False
) as difffile:
for diff in difference:
difffile.write("\n")
difffile.writelines(diff)
session.error(
f"Documentation python{session.python} is "
"different than reference\n"
f"Check difference here {difffile.name}"
)