src/black_primer/lib.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import asyncio
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import errno
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import json
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import logging
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import os
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import stat
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import sys
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from functools import partial
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from pathlib import Path
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from platform import system
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from shutil import rmtree, which
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from subprocess import CalledProcessError
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from sys import version_info
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from tempfile import TemporaryDirectory
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Any,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Callable,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Dict,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | List,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | NamedTuple,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Optional,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Sequence,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Tuple,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Union,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from urllib.parse import urlparse
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import click
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TEN_MINUTES_SECONDS = 600
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | WINDOWS = system() == "Windows"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BLACK_BINARY = "black.exe" if WINDOWS else "black"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | GIT_BINARY = "git.exe" if WINDOWS else "git"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LOG = logging.getLogger(__name__)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # Windows needs a ProactorEventLoop if you want to exec subprocesses
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # Starting with 3.8 this is the default - can remove when Black >= 3.8
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # mypy only respects sys.platform if directly in the evaluation
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # https://mypy.readthedocs.io/en/latest/common_issues.html#python-version-and-system-platform-checks # noqa: B950
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | if sys.platform == "win32":
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | asyncio.set_event_loop(asyncio.ProactorEventLoop())
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class Results(NamedTuple):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | stats: Dict[str, int] = {}
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | failed_projects: Dict[str, CalledProcessError] = {}
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | async def _gen_check_output(
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | cmd: Sequence[str],
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | timeout: float = TEN_MINUTES_SECONDS,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | env: Optional[Dict[str, str]] = None,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | cwd: Optional[Path] = None,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | stdin: Optional[bytes] = None,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | ) -> Tuple[bytes, bytes]:
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | process = await asyncio.create_subprocess_exec(
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | *cmd,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | stdin=asyncio.subprocess.PIPE,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | stdout=asyncio.subprocess.PIPE,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | stderr=asyncio.subprocess.STDOUT,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | env=env,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | cwd=cwd,
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | )
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | try:
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | (stdout, stderr) = await asyncio.wait_for(process.communicate(stdin), timeout)
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | except asyncio.TimeoutError:
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | process.kill()
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | await process.wait()
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | raise
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 |
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | # A non-optional timeout was supplied to asyncio.wait_for, guaranteeing
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | # a timeout or completed process. A terminated Python process will have a
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | # non-empty returncode value.
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | assert process.returncode is not None
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 |
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | if process.returncode != 0:
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | cmd_str = " ".join(cmd)
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | raise CalledProcessError(
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | process.returncode, cmd_str, output=stdout, stderr=stderr
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | )
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 |
0034 0013 0028 0003 0000 0003 0003 -- 03 002 003 002 004 005 006 0013.93 0018.58 0001.33 | return (stdout, stderr)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | def analyze_results(project_count: int, results: Results) -> int:
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | failed_pct = round(((results.stats["failed"] / project_count) * 100), 2)
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | success_pct = round(((results.stats["success"] / project_count) * 100), 2)
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | if results.failed_projects:
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.secho("\nFailed projects:\n", bold=True)
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | for project_name, project_cpe in results.failed_projects.items():
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | print(f"## {project_name}:")
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | print(f" - Returned {project_cpe.returncode}")
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | if project_cpe.stderr:
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | print(f" - stderr:\n{project_cpe.stderr.decode('utf8')}")
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | if project_cpe.stdout:
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | print(f" - stdout:\n{project_cpe.stdout.decode('utf8')}")
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | print("")
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.secho("-- primer results 📊 --\n", bold=True)
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.secho(
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | f"{results.stats['success']} / {project_count} succeeded ({success_pct}%) ✅",
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | bold=True,
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | fg="green",
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | )
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.secho(
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | f"{results.stats['failed']} / {project_count} FAILED ({failed_pct}%) 💩",
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | bold=bool(results.stats["failed"]),
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | fg="red",
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | )
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | s = "" if results.stats["disabled"] == 1 else "s"
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.echo(f" - {results.stats['disabled']} project{s} disabled by config")
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | s = "" if results.stats["wrong_py_ver"] == 1 else "s"
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.echo(
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | f" - {results.stats['wrong_py_ver']} project{s} skipped due to Python version"
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | )
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.echo(
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | f" - {results.stats['skipped_long_checkout']} skipped due to long checkout"
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | )
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | if results.failed_projects:
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | failed = ", ".join(results.failed_projects.keys())
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | click.secho(f"\nFailed projects: {failed}\n", bold=True)
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 |
0042 0025 0037 0000 0000 0005 0000 -- 08 003 009 006 012 012 018 0064.53 0129.06 0002.00 | return results.stats["failed"]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def _flatten_cli_args(cli_args: List[Union[Sequence[str], str]]) -> List[str]:
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Allow a user to put long arguments into a list of strs
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | to make the JSON human readable"""
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | flat_args = []
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for arg in cli_args:
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if isinstance(arg, str):
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | flat_args.append(arg)
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | continue
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | args_as_str = "".join(arg)
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | flat_args.append(args_as_str)
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0013 0010 0009 0000 0002 0002 0000 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return flat_args
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | async def black_run(
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | project_name: str,
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | repo_path: Optional[Path],
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | project_config: Dict[str, Any],
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results: Results,
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | no_diff: bool = False,
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | ) -> None:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | """Run Black and record failures"""
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if not repo_path:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["failed"] += 1
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.failed_projects[project_name] = CalledProcessError(
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | 69, [], f"{project_name} has no repo_path: {repo_path}".encode(), b""
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | )
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | return
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | stdin_test = project_name.upper() == "STDIN"
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd = [str(which(BLACK_BINARY))]
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if "cli_arguments" in project_config and project_config["cli_arguments"]:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd.extend(_flatten_cli_args(project_config["cli_arguments"]))
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd.append("--check")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if not no_diff:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd.append("--diff")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | # Workout if we should read in a python file or search from cwd
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | stdin = None
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if stdin_test:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd.append("-")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | stdin = repo_path.read_bytes()
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | elif "base_path" in project_config:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd.append(project_config["base_path"])
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | else:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd.append(".")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | timeout = (
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | project_config["timeout_seconds"]
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if "timeout_seconds" in project_config
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | else TEN_MINUTES_SECONDS
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | )
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | with TemporaryDirectory() as tmp_path:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | # Prevent reading top-level user configs by manipulating environment variables
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | env = {
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | **os.environ,
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | "XDG_CONFIG_HOME": tmp_path, # Unix-like
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | "USERPROFILE": tmp_path, # Windows (changes `Path.home()` output)
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | }
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cwd_path = repo_path.parent if stdin_test else repo_path
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | try:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | LOG.debug(f"Running black for {project_name}: {' '.join(cmd)}")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | _stdout, _stderr = await _gen_check_output(
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | cmd, cwd=cwd_path, env=env, stdin=stdin, timeout=timeout
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | )
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | except asyncio.TimeoutError:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["failed"] += 1
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | LOG.error(f"Running black for {repo_path} timed out ({cmd})")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | except CalledProcessError as cpe:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | # TODO: Tune for smarter for higher signal
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | # If any other return value than 1 we raise - can disable project in config
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if cpe.returncode == 1:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if not project_config["expect_formatting_changes"]:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["failed"] += 1
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.failed_projects[repo_path.name] = cpe
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | else:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["success"] += 1
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | return
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | elif cpe.returncode > 1:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["failed"] += 1
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.failed_projects[repo_path.name] = cpe
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | return
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | LOG.error(f"Unknown error with {repo_path}")
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | raise
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | # If we get here and expect formatting changes something is up
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | if project_config["expect_formatting_changes"]:
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["failed"] += 1
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.failed_projects[repo_path.name] = CalledProcessError(
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | 0, cmd, b"Expected formatting changes but didn't get any!", b""
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | )
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | return
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 |
0082 0051 0069 0007 0000 0007 0006 -- 15 006 020 017 031 026 048 0225.62 1049.14 0004.65 | results.stats["success"] += 1
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | async def git_checkout_or_rebase(
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | work_path: Path,
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | project_config: Dict[str, Any],
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | rebase: bool = False,
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | *,
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | depth: int = 1,
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | ) -> Optional[Path]:
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | """git Clone project or rebase"""
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | git_bin = str(which(GIT_BINARY))
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | if not git_bin:
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | LOG.error("No git binary found")
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | return None
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 |
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | repo_url_parts = urlparse(project_config["git_clone_url"])
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | path_parts = repo_url_parts.path[1:].split("/", maxsplit=1)
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 |
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | repo_path: Path = work_path / path_parts[1].replace(".git", "")
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | cmd = [git_bin, "clone", "--depth", str(depth), project_config["git_clone_url"]]
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | cwd = work_path
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | if repo_path.exists() and rebase:
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | cmd = [git_bin, "pull", "--rebase"]
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | cwd = repo_path
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | elif repo_path.exists():
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | return repo_path
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 |
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | try:
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | _stdout, _stderr = await _gen_check_output(cmd, cwd=cwd)
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | except (asyncio.TimeoutError, CalledProcessError) as e:
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | LOG.error(f"Unable to git clone / pull {project_config['git_clone_url']}: {e}")
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | return None
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 |
0032 0024 0027 0000 0000 0004 0001 -- 06 003 005 003 005 008 008 0024.00 0036.00 0001.50 | return repo_path
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | def handle_PermissionError(
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | func: Callable[..., None], path: Path, exc: Tuple[Any, Any, Any]
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | ) -> None:
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | """
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | Handle PermissionError during shutil.rmtree.
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 |
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | This checks if the erroring function is either 'os.rmdir' or 'os.unlink', and that
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | the error was EACCES (i.e. Permission denied). If true, the path is set writable,
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | readable, and executable by everyone. Finally, it tries the error causing delete
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | operation again.
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 |
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | If the check is false, then the original error will be reraised as this function
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | can't handle it.
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | """
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | excvalue = exc[1]
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | LOG.debug(f"Handling {excvalue} from {func.__name__}... ")
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | if func in (os.rmdir, os.unlink) and excvalue.errno == errno.EACCES:
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | LOG.debug(f"Setting {path} writable, readable, and executable by everyone... ")
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # chmod 0777
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | func(path) # Try the error causing delete operation again
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | else:
0022 0010 0011 0002 0009 0002 0000 -- 03 004 010 005 010 014 015 0057.11 0114.22 0002.00 | raise
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | async def load_projects_queue(
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | config_path: Path,
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | projects_to_run: List[str],
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ) -> Tuple[Dict[str, Any], asyncio.Queue]:
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Load project config and fill queue with all the project names"""
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | with config_path.open("r") as cfp:
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | config = json.load(cfp)
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | # TODO: Offer more options here
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | # e.g. Run on X random packages etc.
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | queue: asyncio.Queue = asyncio.Queue(maxsize=len(projects_to_run))
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for project in projects_to_run:
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | await queue.put(project)
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
0015 0009 0010 0002 0000 0002 0003 -- 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return config, queue
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | async def project_runner(
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | idx: int,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | config: Dict[str, Any],
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | queue: asyncio.Queue,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | work_path: Path,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | results: Results,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | long_checkouts: bool = False,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | rebase: bool = False,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | keep: bool = False,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | no_diff: bool = False,
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | ) -> None:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | """Check out project and run Black on it + record result"""
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | loop = asyncio.get_event_loop()
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | py_version = f"{version_info[0]}.{version_info[1]}"
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | while True:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | try:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | project_name = queue.get_nowait()
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | except asyncio.QueueEmpty:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.debug(f"project_runner {idx} exiting")
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | return
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.debug(f"worker {idx} working on {project_name}")
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | project_config = config["projects"][project_name]
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | # Check if disabled by config
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | if "disabled" in project_config and project_config["disabled"]:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | results.stats["disabled"] += 1
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.info(f"Skipping {project_name} as it's disabled via config")
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | continue
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | # Check if we should run on this version of Python
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | if (
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | "all" not in project_config["py_versions"]
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | and py_version not in project_config["py_versions"]
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | ):
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | results.stats["wrong_py_ver"] += 1
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.debug(f"Skipping {project_name} as it's not enabled for {py_version}")
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | continue
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | # Check if we're doing big projects / long checkouts
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | if not long_checkouts and project_config["long_checkout"]:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | results.stats["skipped_long_checkout"] += 1
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.debug(f"Skipping {project_name} as it's configured as a long checkout")
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | continue
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | repo_path: Optional[Path] = Path(__file__)
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | stdin_project = project_name.upper() == "STDIN"
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | if not stdin_project:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | repo_path = await git_checkout_or_rebase(work_path, project_config, rebase)
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | if not repo_path:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | continue
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | await black_run(project_name, repo_path, project_config, results, no_diff)
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | if not keep and not stdin_project:
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.debug(f"Removing {repo_path}")
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | rmtree_partial = partial(
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | rmtree, path=repo_path, onerror=handle_PermissionError
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | )
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | await loop.run_in_executor(None, rmtree_partial)
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 |
0061 0037 0050 0003 0000 0007 0004 -- 13 006 024 016 027 030 043 0211.00 0712.11 0003.38 | LOG.info(f"Finished {project_name}")
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | async def process_queue(
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | config_file: str,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | work_path: Path,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | workers: int,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | projects_to_run: List[str],
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | keep: bool = False,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | long_checkouts: bool = False,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | rebase: bool = False,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | no_diff: bool = False,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | ) -> int:
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | """
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | Process the queue with X workers and evaluate results
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | - Success is guaged via the config "expect_formatting_changes"
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 |
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | Integer return equals the number of failed projects
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | """
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results = Results()
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results.stats["disabled"] = 0
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results.stats["failed"] = 0
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results.stats["skipped_long_checkout"] = 0
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results.stats["success"] = 0
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results.stats["wrong_py_ver"] = 0
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 |
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | config, queue = await load_projects_queue(Path(config_file), projects_to_run)
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | project_count = queue.qsize()
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | s = "" if project_count == 1 else "s"
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | LOG.info(f"{project_count} project{s} to run Black over")
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | if project_count < 1:
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | return -1
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 |
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | s = "" if workers == 1 else "s"
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | LOG.debug(f"Using {workers} parallel worker{s} to run Black")
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | # Wait until we finish running all the projects before analyzing
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | await asyncio.gather(
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | *[
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | project_runner(
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | i,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | config,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | queue,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | work_path,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | results,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | long_checkouts,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | rebase,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | keep,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | no_diff,
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | )
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | for i in range(workers)
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | ]
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | )
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 |
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | LOG.info("Analyzing results")
0052 0019 0042 0001 0005 0004 0001 -- 05 003 003 004 007 006 011 0028.43 0099.52 0003.50 | return analyze_results(project_count, results)
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | if __name__ == "__main__": # pragma: nocover
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | raise NotImplementedError("lib is a library, funnily enough.")