Debian Codegraph @

coherent/licensed/setuptools.py

raw symbols SCIP
1 import contextlib
2 import functools
3 import os
4 import pathlib
5 import warnings
6
7 from . import resolve
8 from ._functools import apply, bypass_when, result_invoke
9
10
11 @bypass_when(os.environ.get('COHERENT_LICENSED_UNUSED_ACTION', 'warn') == 'ignore')
12 def warn_if_false(enabled):
13 enabled or warnings.warn(
14 "Avoid installing this plugin for projects that don't depend on it."
15 )
16
17
18 def dist_root(dist):
19 return pathlib.Path(dist.src_root or '')
20
21
22 @result_invoke(warn_if_false)
23 @apply(bool)
24 def enabled(dist):
25 root = dist_root(dist)
26 with contextlib.suppress(FileNotFoundError):
27 project = root.joinpath('pyproject.toml').read_text(encoding='utf-8')
28 return 'coherent.licensed' in project
29
30
31 def _finalize_license_files(dist, orig):
32 """
33 Resolve the license expression into a license file.
34 """
35 if not enabled(dist):
36 return orig()
37 license = dist_root(dist) / 'LICENSE'
38 dist.metadata.license_files = [str(license)]
39 if license.exists():
40 return
41 license.write_text(resolve(dist.metadata.license_expression))
42
43
44 def inject(dist):
45 """
46 Patch the dist to resolve the license expression.
47
48 This hook is called before `dist.parse_config_files` has been called, so
49 the license expression has not been loaded yet, so patch _finalize_license_files
50 to write out the license after expressions are loaded.
51 """
52 dist._finalize_license_files = functools.partial(
53 _finalize_license_files, dist, orig=dist._finalize_license_files
54 )