annotate piecrust/chefutil.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents 0e9a94b7fdfa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import time
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
2 import logging
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
3 import contextlib
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from colorama import Fore
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
7 @contextlib.contextmanager
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
8 def format_timed_scope(logger, message, *, level=logging.INFO, colored=True,
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
9 timer_env=None, timer_category=None):
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
10 start_time = time.perf_counter()
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
11 yield
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
12 logger.log(level, format_timed(start_time, message, colored=colored))
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
13 if timer_env is not None and timer_category is not None:
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
14 timer_env.stepTimer(timer_category, time.perf_counter() - start_time)
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
15
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
16
120
133845647083 Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents: 99
diff changeset
17 def format_timed(start_time, message, indent_level=0, colored=True):
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 272
diff changeset
18 end_time = time.perf_counter()
120
133845647083 Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents: 99
diff changeset
19 indent = indent_level * ' '
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 time_str = '%8.1f ms' % ((end_time - start_time) * 1000.0)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 if colored:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 return '[%s%s%s] %s' % (Fore.GREEN, time_str, Fore.RESET, message)
120
133845647083 Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents: 99
diff changeset
23 return '%s[%s] %s' % (indent, time_str, message)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
39
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
25
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
26 def log_friendly_exception(logger, ex):
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
27 indent = ''
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
28 while ex:
272
a561fbad0b7f logging: If an error doesn't have a message, print its type.
Ludovic Chabant <ludovic@chabant.com>
parents: 120
diff changeset
29 ex_msg = str(ex)
a561fbad0b7f logging: If an error doesn't have a message, print its type.
Ludovic Chabant <ludovic@chabant.com>
parents: 120
diff changeset
30 if not ex_msg:
a561fbad0b7f logging: If an error doesn't have a message, print its type.
Ludovic Chabant <ludovic@chabant.com>
parents: 120
diff changeset
31 ex_msg = '%s exception was thrown' % type(ex).__name__
a561fbad0b7f logging: If an error doesn't have a message, print its type.
Ludovic Chabant <ludovic@chabant.com>
parents: 120
diff changeset
32 logger.error('%s%s' % (indent, ex_msg))
39
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
33 indent += ' '
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
34 ex = ex.__cause__
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
35
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
36
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
37 def print_help_item(s, title, description, margin=4, align=25):
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
38 s.write(margin * ' ')
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
39 s.write(title)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
40 spacer = (align - margin - len(title) - 1)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
41 if spacer <= 0:
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
42 s.write("\n")
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
43 s.write(' ' * align)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
44 else:
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
45 s.write(' ' * spacer)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
46 s.write(description)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
47 s.write("\n")
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
48