comparison env/lib/python3.9/site-packages/pip/_vendor/pep517/build.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Build a project using PEP 517 hooks.
2 """
3 import argparse
4 import logging
5 import os
6 from pip._vendor import toml
7 import shutil
8
9 from .envbuild import BuildEnvironment
10 from .wrappers import Pep517HookCaller
11 from .dirtools import tempdir, mkdir_p
12 from .compat import FileNotFoundError
13
14 log = logging.getLogger(__name__)
15
16
17 def validate_system(system):
18 """
19 Ensure build system has the requisite fields.
20 """
21 required = {'requires', 'build-backend'}
22 if not (required <= set(system)):
23 message = "Missing required fields: {missing}".format(
24 missing=required-set(system),
25 )
26 raise ValueError(message)
27
28
29 def load_system(source_dir):
30 """
31 Load the build system from a source dir (pyproject.toml).
32 """
33 pyproject = os.path.join(source_dir, 'pyproject.toml')
34 with open(pyproject) as f:
35 pyproject_data = toml.load(f)
36 return pyproject_data['build-system']
37
38
39 def compat_system(source_dir):
40 """
41 Given a source dir, attempt to get a build system backend
42 and requirements from pyproject.toml. Fallback to
43 setuptools but only if the file was not found or a build
44 system was not indicated.
45 """
46 try:
47 system = load_system(source_dir)
48 except (FileNotFoundError, KeyError):
49 system = {}
50 system.setdefault(
51 'build-backend',
52 'setuptools.build_meta:__legacy__',
53 )
54 system.setdefault('requires', ['setuptools', 'wheel'])
55 return system
56
57
58 def _do_build(hooks, env, dist, dest):
59 get_requires_name = 'get_requires_for_build_{dist}'.format(**locals())
60 get_requires = getattr(hooks, get_requires_name)
61 reqs = get_requires({})
62 log.info('Got build requires: %s', reqs)
63
64 env.pip_install(reqs)
65 log.info('Installed dynamic build dependencies')
66
67 with tempdir() as td:
68 log.info('Trying to build %s in %s', dist, td)
69 build_name = 'build_{dist}'.format(**locals())
70 build = getattr(hooks, build_name)
71 filename = build(td, {})
72 source = os.path.join(td, filename)
73 shutil.move(source, os.path.join(dest, os.path.basename(filename)))
74
75
76 def build(source_dir, dist, dest=None, system=None):
77 system = system or load_system(source_dir)
78 dest = os.path.join(source_dir, dest or 'dist')
79 mkdir_p(dest)
80
81 validate_system(system)
82 hooks = Pep517HookCaller(
83 source_dir, system['build-backend'], system.get('backend-path')
84 )
85
86 with BuildEnvironment() as env:
87 env.pip_install(system['requires'])
88 _do_build(hooks, env, dist, dest)
89
90
91 parser = argparse.ArgumentParser()
92 parser.add_argument(
93 'source_dir',
94 help="A directory containing pyproject.toml",
95 )
96 parser.add_argument(
97 '--binary', '-b',
98 action='store_true',
99 default=False,
100 )
101 parser.add_argument(
102 '--source', '-s',
103 action='store_true',
104 default=False,
105 )
106 parser.add_argument(
107 '--out-dir', '-o',
108 help="Destination in which to save the builds relative to source dir",
109 )
110
111
112 def main(args):
113 # determine which dists to build
114 dists = list(filter(None, (
115 'sdist' if args.source or not args.binary else None,
116 'wheel' if args.binary or not args.source else None,
117 )))
118
119 for dist in dists:
120 build(args.source_dir, dist, args.out_dir)
121
122
123 if __name__ == '__main__':
124 main(parser.parse_args())