comparison lib/python3.8/site-packages/pip/_vendor/pep517/check.py @ 1:64071f2a4cf0 draft default tip

Deleted selected files
author guerler
date Mon, 27 Jul 2020 03:55:49 -0400
parents 9e54283cc701
children
comparison
equal deleted inserted replaced
0:9e54283cc701 1:64071f2a4cf0
1 """Check a project and backend by attempting to build using PEP 517 hooks.
2 """
3 import argparse
4 import logging
5 import os
6 from os.path import isfile, join as pjoin
7 from toml import TomlDecodeError, load as toml_load
8 import shutil
9 from subprocess import CalledProcessError
10 import sys
11 import tarfile
12 from tempfile import mkdtemp
13 import zipfile
14
15 from .colorlog import enable_colourful_output
16 from .envbuild import BuildEnvironment
17 from .wrappers import Pep517HookCaller
18
19 log = logging.getLogger(__name__)
20
21
22 def check_build_sdist(hooks, build_sys_requires):
23 with BuildEnvironment() as env:
24 try:
25 env.pip_install(build_sys_requires)
26 log.info('Installed static build dependencies')
27 except CalledProcessError:
28 log.error('Failed to install static build dependencies')
29 return False
30
31 try:
32 reqs = hooks.get_requires_for_build_sdist({})
33 log.info('Got build requires: %s', reqs)
34 except Exception:
35 log.error('Failure in get_requires_for_build_sdist', exc_info=True)
36 return False
37
38 try:
39 env.pip_install(reqs)
40 log.info('Installed dynamic build dependencies')
41 except CalledProcessError:
42 log.error('Failed to install dynamic build dependencies')
43 return False
44
45 td = mkdtemp()
46 log.info('Trying to build sdist in %s', td)
47 try:
48 try:
49 filename = hooks.build_sdist(td, {})
50 log.info('build_sdist returned %r', filename)
51 except Exception:
52 log.info('Failure in build_sdist', exc_info=True)
53 return False
54
55 if not filename.endswith('.tar.gz'):
56 log.error(
57 "Filename %s doesn't have .tar.gz extension", filename)
58 return False
59
60 path = pjoin(td, filename)
61 if isfile(path):
62 log.info("Output file %s exists", path)
63 else:
64 log.error("Output file %s does not exist", path)
65 return False
66
67 if tarfile.is_tarfile(path):
68 log.info("Output file is a tar file")
69 else:
70 log.error("Output file is not a tar file")
71 return False
72
73 finally:
74 shutil.rmtree(td)
75
76 return True
77
78
79 def check_build_wheel(hooks, build_sys_requires):
80 with BuildEnvironment() as env:
81 try:
82 env.pip_install(build_sys_requires)
83 log.info('Installed static build dependencies')
84 except CalledProcessError:
85 log.error('Failed to install static build dependencies')
86 return False
87
88 try:
89 reqs = hooks.get_requires_for_build_wheel({})
90 log.info('Got build requires: %s', reqs)
91 except Exception:
92 log.error('Failure in get_requires_for_build_sdist', exc_info=True)
93 return False
94
95 try:
96 env.pip_install(reqs)
97 log.info('Installed dynamic build dependencies')
98 except CalledProcessError:
99 log.error('Failed to install dynamic build dependencies')
100 return False
101
102 td = mkdtemp()
103 log.info('Trying to build wheel in %s', td)
104 try:
105 try:
106 filename = hooks.build_wheel(td, {})
107 log.info('build_wheel returned %r', filename)
108 except Exception:
109 log.info('Failure in build_wheel', exc_info=True)
110 return False
111
112 if not filename.endswith('.whl'):
113 log.error("Filename %s doesn't have .whl extension", filename)
114 return False
115
116 path = pjoin(td, filename)
117 if isfile(path):
118 log.info("Output file %s exists", path)
119 else:
120 log.error("Output file %s does not exist", path)
121 return False
122
123 if zipfile.is_zipfile(path):
124 log.info("Output file is a zip file")
125 else:
126 log.error("Output file is not a zip file")
127 return False
128
129 finally:
130 shutil.rmtree(td)
131
132 return True
133
134
135 def check(source_dir):
136 pyproject = pjoin(source_dir, 'pyproject.toml')
137 if isfile(pyproject):
138 log.info('Found pyproject.toml')
139 else:
140 log.error('Missing pyproject.toml')
141 return False
142
143 try:
144 with open(pyproject) as f:
145 pyproject_data = toml_load(f)
146 # Ensure the mandatory data can be loaded
147 buildsys = pyproject_data['build-system']
148 requires = buildsys['requires']
149 backend = buildsys['build-backend']
150 backend_path = buildsys.get('backend-path')
151 log.info('Loaded pyproject.toml')
152 except (TomlDecodeError, KeyError):
153 log.error("Invalid pyproject.toml", exc_info=True)
154 return False
155
156 hooks = Pep517HookCaller(source_dir, backend, backend_path)
157
158 sdist_ok = check_build_sdist(hooks, requires)
159 wheel_ok = check_build_wheel(hooks, requires)
160
161 if not sdist_ok:
162 log.warning('Sdist checks failed; scroll up to see')
163 if not wheel_ok:
164 log.warning('Wheel checks failed')
165
166 return sdist_ok
167
168
169 def main(argv=None):
170 ap = argparse.ArgumentParser()
171 ap.add_argument(
172 'source_dir',
173 help="A directory containing pyproject.toml")
174 args = ap.parse_args(argv)
175
176 enable_colourful_output()
177
178 ok = check(args.source_dir)
179
180 if ok:
181 print(ansi('Checks passed', 'green'))
182 else:
183 print(ansi('Checks failed', 'red'))
184 sys.exit(1)
185
186
187 ansi_codes = {
188 'reset': '\x1b[0m',
189 'bold': '\x1b[1m',
190 'red': '\x1b[31m',
191 'green': '\x1b[32m',
192 }
193
194
195 def ansi(s, attr):
196 if os.name != 'nt' and sys.stdout.isatty():
197 return ansi_codes[attr] + str(s) + ansi_codes['reset']
198 else:
199 return str(s)
200
201
202 if __name__ == '__main__':
203 main()