-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
config.py
585 lines (453 loc) · 23.6 KB
/
config.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
from __future__ import annotations
import re
from copy import deepcopy
from itertools import product
from os import environ
from typing import TYPE_CHECKING
from hatch.env.utils import ensure_valid_environment
from hatch.project.env import apply_overrides
from hatch.project.utils import format_script_commands, parse_script_command
if TYPE_CHECKING:
from packaging.requirements import Requirement
class ProjectConfig:
def __init__(self, root, config, plugin_manager=None):
self.root = root
self.config = config
self.plugin_manager = plugin_manager
self._matrices = None
self._env = None
self._env_requires_complex = None
self._env_requires = None
self._env_collectors = None
self._envs = None
self._internal_envs = None
self._internal_matrices = None
self._matrix_variables = None
self._publish = None
self._scripts = None
self._cached_env_overrides = {}
@property
def env(self):
if self._env is None:
config = self.config.get('env', {})
if not isinstance(config, dict):
message = 'Field `tool.hatch.env` must be a table'
raise TypeError(message)
self._env = config
return self._env
@property
def env_requires_complex(self) -> list[Requirement]:
if self._env_requires_complex is None:
from packaging.requirements import InvalidRequirement, Requirement
requires = self.env.get('requires', [])
if not isinstance(requires, list):
message = 'Field `tool.hatch.env.requires` must be an array'
raise TypeError(message)
requires_complex = []
for i, entry in enumerate(requires, 1):
if not isinstance(entry, str):
message = f'Requirement #{i} in `tool.hatch.env.requires` must be a string'
raise TypeError(message)
try:
requires_complex.append(Requirement(entry))
except InvalidRequirement as e:
message = f'Requirement #{i} in `tool.hatch.env.requires` is invalid: {e}'
raise ValueError(message) from None
self._env_requires_complex = requires_complex
return self._env_requires_complex
@property
def env_requires(self):
if self._env_requires is None:
self._env_requires = [str(r) for r in self.env_requires_complex]
return self._env_requires
@property
def env_collectors(self):
if self._env_collectors is None:
collectors = self.env.get('collectors', {})
if not isinstance(collectors, dict):
message = 'Field `tool.hatch.env.collectors` must be a table'
raise TypeError(message)
final_config = {'default': {}}
for collector, config in collectors.items():
if not isinstance(config, dict):
message = f'Field `tool.hatch.env.collectors.{collector}` must be a table'
raise TypeError(message)
final_config[collector] = config
self._env_collectors = final_config
return self._env_collectors
@property
def matrices(self):
if self._matrices is None:
_ = self.envs
return self._matrices
@property
def matrix_variables(self):
if self._matrix_variables is None:
_ = self.envs
return self._matrix_variables
@property
def internal_envs(self):
if self._internal_envs is None:
_ = self.envs
return self._internal_envs
@property
def internal_matrices(self):
if self._internal_matrices is None:
_ = self.envs
return self._internal_matrices
@property
def envs(self):
from hatch.env.internal import get_internal_env_config
from hatch.utils.platform import get_platform_name
if self._envs is None:
env_config = self.config.get('envs', {})
if not isinstance(env_config, dict):
message = 'Field `tool.hatch.envs` must be a table'
raise TypeError(message)
config = {}
environment_collectors = []
for collector, collector_config in self.env_collectors.items():
collector_class = self.plugin_manager.environment_collector.get(collector)
if collector_class is None:
from hatchling.plugin.exceptions import UnknownPluginError
message = f'Unknown environment collector: {collector}'
raise UnknownPluginError(message)
environment_collector = collector_class(self.root, collector_config)
environment_collectors.append(environment_collector)
for env_name, data in environment_collector.get_initial_config().items():
config.setdefault(env_name, data)
for env_name, data in env_config.items():
if not isinstance(data, dict):
message = f'Field `tool.hatch.envs.{env_name}` must be a table'
raise TypeError(message)
config.setdefault(env_name, {}).update(data)
for environment_collector in environment_collectors:
environment_collector.finalize_config(config)
# Prevent plugins from removing the default environment
ensure_valid_environment(config.setdefault('default', {}))
seen = set()
active = []
for env_name, data in config.items():
_populate_default_env_values(env_name, data, config, seen, active)
current_platform = get_platform_name()
all_matrices = {}
generated_envs = {}
final_config = {}
cached_overrides = {}
for env_name, raw_initial_config in config.items():
current_cached_overrides = cached_overrides[env_name] = {
'platform': [],
'env': [],
'matrix': [],
'name': [],
}
# Only shallow copying is necessary since we just want to modify keys
initial_config = raw_initial_config.copy()
matrix_name_format = initial_config.pop('matrix-name-format', '{value}')
if not isinstance(matrix_name_format, str):
message = f'Field `tool.hatch.envs.{env_name}.matrix-name-format` must be a string'
raise TypeError(message)
if '{value}' not in matrix_name_format:
message = (
f'Field `tool.hatch.envs.{env_name}.matrix-name-format` must '
f'contain at least the `{{value}}` placeholder'
)
raise ValueError(message)
overrides = initial_config.pop('overrides', {})
if not isinstance(overrides, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides` must be a table'
raise TypeError(message)
# Apply any configuration based on the current platform
platform_overrides = overrides.get('platform', {})
if not isinstance(platform_overrides, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.platform` must be a table'
raise TypeError(message)
for platform, options in platform_overrides.items():
if not isinstance(options, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.platform.{platform}` must be a table'
raise TypeError(message)
if platform != current_platform:
continue
apply_overrides(env_name, 'platform', platform, current_platform, options, initial_config)
current_cached_overrides['platform'].append((platform, current_platform, options))
# Apply any configuration based on environment variables
env_var_overrides = overrides.get('env', {})
if not isinstance(env_var_overrides, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.env` must be a table'
raise TypeError(message)
for env_var, options in env_var_overrides.items():
if not isinstance(options, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.env.{env_var}` must be a table'
raise TypeError(message)
if env_var not in environ:
continue
apply_overrides(env_name, 'env', env_var, environ[env_var], options, initial_config)
current_cached_overrides['env'].append((env_var, environ[env_var], options))
if 'matrix' not in initial_config:
final_config[env_name] = initial_config
continue
matrices = initial_config.pop('matrix')
if not isinstance(matrices, list):
message = f'Field `tool.hatch.envs.{env_name}.matrix` must be an array'
raise TypeError(message)
matrix_overrides = overrides.get('matrix', {})
if not isinstance(matrix_overrides, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.matrix` must be a table'
raise TypeError(message)
name_overrides = overrides.get('name', {})
if not isinstance(name_overrides, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.name` must be a table'
raise TypeError(message)
matrix_data = all_matrices[env_name] = {'config': deepcopy(initial_config)}
all_envs = matrix_data['envs'] = {}
for i, raw_matrix in enumerate(matrices, 1):
matrix = raw_matrix
if not isinstance(matrix, dict):
message = f'Entry #{i} in field `tool.hatch.envs.{env_name}.matrix` must be a table'
raise TypeError(message)
if not matrix:
message = f'Matrix #{i} in field `tool.hatch.envs.{env_name}.matrix` cannot be empty'
raise ValueError(message)
for j, (variable, values) in enumerate(matrix.items(), 1):
if not variable:
message = (
f'Variable #{j} in matrix #{i} in field `tool.hatch.envs.{env_name}.matrix` '
f'cannot be an empty string'
)
raise ValueError(message)
if not isinstance(values, list):
message = (
f'Variable `{variable}` in matrix #{i} in field `tool.hatch.envs.{env_name}.matrix` '
f'must be an array'
)
raise TypeError(message)
if not values:
message = (
f'Variable `{variable}` in matrix #{i} in field `tool.hatch.envs.{env_name}.matrix` '
f'cannot be empty'
)
raise ValueError(message)
existing_values = set()
for k, value in enumerate(values, 1):
if not isinstance(value, str):
message = (
f'Value #{k} of variable `{variable}` in matrix #{i} in field '
f'`tool.hatch.envs.{env_name}.matrix` must be a string'
)
raise TypeError(message)
if not value:
message = (
f'Value #{k} of variable `{variable}` in matrix #{i} in field '
f'`tool.hatch.envs.{env_name}.matrix` cannot be an empty string'
)
raise ValueError(message)
if value in existing_values:
message = (
f'Value #{k} of variable `{variable}` in matrix #{i} in field '
f'`tool.hatch.envs.{env_name}.matrix` is a duplicate'
)
raise ValueError(message)
existing_values.add(value)
variables = {}
# Ensure that any Python variable comes first
python_selected = False
for variable in ('py', 'python'):
if variable in matrix:
if python_selected:
message = (
f'Matrix #{i} in field `tool.hatch.envs.{env_name}.matrix` '
f'cannot contain both `py` and `python` variables'
)
raise ValueError(message)
python_selected = True
# Only shallow copying is necessary since we just want to remove a key
matrix = matrix.copy()
variables[variable] = matrix.pop(variable)
variables.update(matrix)
for result in product(*variables.values()):
# Make a value mapping for easy referencing
variable_values = dict(zip(variables, result))
# Create the environment's initial configuration
new_config = deepcopy(initial_config)
cached_matrix_overrides = []
# Apply any configuration based on matrix variables
for variable, options in matrix_overrides.items():
if not isinstance(options, dict):
message = (
f'Field `tool.hatch.envs.{env_name}.overrides.matrix.{variable}` must be a table'
)
raise TypeError(message)
if variable not in variables:
continue
apply_overrides(
env_name, 'matrix', variable, variable_values[variable], options, new_config
)
cached_matrix_overrides.append((variable, variable_values[variable], options))
# Construct the environment name
final_matrix_name_format = new_config.pop('matrix-name-format', matrix_name_format)
env_name_parts = []
for j, (variable, value) in enumerate(variable_values.items()):
if j == 0 and python_selected:
new_config['python'] = value
env_name_parts.append(value if value.startswith('py') else f'py{value}')
else:
env_name_parts.append(final_matrix_name_format.format(variable=variable, value=value))
new_env_name = '-'.join(env_name_parts)
cached_name_overrides = []
# Apply any configuration based on the final name, minus the prefix for non-default environments
for pattern, options in name_overrides.items():
if not isinstance(options, dict):
message = f'Field `tool.hatch.envs.{env_name}.overrides.name.{pattern}` must be a table'
raise TypeError(message)
if not re.search(pattern, new_env_name):
continue
apply_overrides(env_name, 'name', pattern, new_env_name, options, new_config)
cached_name_overrides.append((pattern, new_env_name, options))
if env_name != 'default':
new_env_name = f'{env_name}.{new_env_name}'
# Save the generated environment
final_config[new_env_name] = new_config
cached_overrides[new_env_name] = {
'platform': current_cached_overrides['platform'],
'env': current_cached_overrides['env'],
'matrix': cached_matrix_overrides,
'name': cached_name_overrides,
}
all_envs[new_env_name] = variable_values
if 'py' in variable_values:
all_envs[new_env_name] = {'python': variable_values.pop('py'), **variable_values}
# Remove the root matrix generator
del cached_overrides[env_name]
# Save the variables used to generate the environments
generated_envs.update(all_envs)
for environment_collector in environment_collectors:
environment_collector.finalize_environments(final_config)
self._matrices = all_matrices
self._internal_matrices = {}
self._envs = final_config
self._matrix_variables = generated_envs
self._cached_env_overrides.update(cached_overrides)
# Extract the internal environments
self._internal_envs = {}
for internal_name in get_internal_env_config():
try:
self._internal_envs[internal_name] = self._envs.pop(internal_name)
# Matrix
except KeyError:
self._internal_matrices[internal_name] = self._matrices.pop(internal_name)
for env_name in [env_name for env_name in self._envs if env_name.startswith(f'{internal_name}.')]:
self._internal_envs[env_name] = self._envs.pop(env_name)
return self._envs
@property
def publish(self):
if self._publish is None:
config = self.config.get('publish', {})
if not isinstance(config, dict):
message = 'Field `tool.hatch.publish` must be a table'
raise TypeError(message)
for publisher, data in config.items():
if not isinstance(data, dict):
message = f'Field `tool.hatch.publish.{publisher}` must be a table'
raise TypeError(message)
self._publish = config
return self._publish
@property
def scripts(self):
if self._scripts is None:
script_config = self.config.get('scripts', {})
if not isinstance(script_config, dict):
message = 'Field `tool.hatch.scripts` must be a table'
raise TypeError(message)
config = {}
for name, data in script_config.items():
if ' ' in name:
message = f'Script name `{name}` in field `tool.hatch.scripts` must not contain spaces'
raise ValueError(message)
commands = []
if isinstance(data, str):
commands.append(data)
elif isinstance(data, list):
for i, command in enumerate(data, 1):
if not isinstance(command, str):
message = f'Command #{i} in field `tool.hatch.scripts.{name}` must be a string'
raise TypeError(message)
commands.append(command)
else:
message = f'Field `tool.hatch.scripts.{name}` must be a string or an array of strings'
raise TypeError(message)
config[name] = commands
seen = {}
active = []
for script_name, commands in config.items():
commands[:] = expand_script_commands(script_name, commands, config, seen, active)
self._scripts = config
return self._scripts
def finalize_env_overrides(self, option_types):
# We lazily apply overrides because we need type information potentially defined by
# environment plugins for their options
if not self._cached_env_overrides:
return
for environments in (self.envs, self.internal_envs):
for env_name, config in environments.items():
for override_name, data in self._cached_env_overrides.get(env_name, {}).items():
for condition, condition_value, options in data:
apply_overrides(
env_name, override_name, condition, condition_value, options, config, option_types
)
self._cached_env_overrides.clear()
def expand_script_commands(script_name, commands, config, seen, active):
if script_name in seen:
return seen[script_name]
if script_name in active:
active.append(script_name)
message = f'Circular expansion detected for field `tool.hatch.scripts`: {" -> ".join(active)}'
raise ValueError(message)
active.append(script_name)
expanded_commands = []
for command in commands:
possible_script, args, ignore_exit_code = parse_script_command(command)
if possible_script in config:
expanded_commands.extend(
format_script_commands(
commands=expand_script_commands(possible_script, config[possible_script], config, seen, active),
args=args,
ignore_exit_code=ignore_exit_code,
)
)
else:
expanded_commands.append(command)
seen[script_name] = expanded_commands
active.pop()
return expanded_commands
def _populate_default_env_values(env_name, data, config, seen, active):
if env_name in seen:
return
if data.pop('detached', False):
data['template'] = env_name
data['skip-install'] = True
template_name = data.pop('template', 'default')
if template_name not in config:
message = f'Field `tool.hatch.envs.{env_name}.template` refers to an unknown environment `{template_name}`'
raise ValueError(message)
if env_name in active:
active.append(env_name)
message = f'Circular inheritance detected for field `tool.hatch.envs.*.template`: {" -> ".join(active)}'
raise ValueError(message)
if template_name == env_name:
ensure_valid_environment(data)
seen.add(env_name)
return
active.append(env_name)
template_config = config[template_name]
_populate_default_env_values(template_name, template_config, config, seen, active)
for key, value in template_config.items():
if key == 'matrix':
continue
if key == 'scripts':
scripts = data['scripts'] if 'scripts' in data else data.setdefault('scripts', {})
for script, commands in value.items():
scripts.setdefault(script, commands)
else:
data.setdefault(key, value)
seen.add(env_name)
active.pop()