Skip to content

Commit 28ceec4

Browse files
committed
Drafted an implementation of a 'build-meta' command for pep517 projects. Ref #26.
1 parent ecd511e commit 28ceec4

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

pep517/build-meta.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""Build metadata for a project using PEP 517 hooks.
2+
"""
3+
import argparse
4+
import logging
5+
import os
6+
import contextlib
7+
import pytoml
8+
import shutil
9+
import errno
10+
import tempfile
11+
12+
from .envbuild import BuildEnvironment
13+
from .wrappers import Pep517HookCaller
14+
15+
log = logging.getLogger(__name__)
16+
17+
18+
@contextlib.contextmanager
19+
def tempdir():
20+
td = tempfile.mkdtemp()
21+
try:
22+
yield td
23+
finally:
24+
shutil.rmtree(td)
25+
26+
27+
def _prep_meta(hooks, env, dest):
28+
reqs = hooks.get_requires_for_build_wheel({})
29+
log.info('Got build requires: %s', reqs)
30+
31+
env.pip_install(reqs)
32+
log.info('Installed dynamic build dependencies')
33+
34+
with tempdir() as td:
35+
log.info('Trying to build metadata in %s', td)
36+
filename = hooks.prepare_metadata_for_build_wheel(td, {})
37+
source = os.path.join(td, filename)
38+
shutil.move(source, os.path.join(dest, os.path.basename(filename)))
39+
40+
41+
def mkdir_p(*args, **kwargs):
42+
"""Like `mkdir`, but does not raise an exception if the
43+
directory already exists.
44+
"""
45+
try:
46+
return os.mkdir(*args, **kwargs)
47+
except OSError as exc:
48+
if exc.errno != errno.EEXIST:
49+
raise
50+
51+
52+
def build_meta(source_dir, dest=None):
53+
pyproject = os.path.join(source_dir, 'pyproject.toml')
54+
dest = os.path.join(source_dir, dest or 'dist')
55+
mkdir_p(dest)
56+
57+
with open(pyproject) as f:
58+
pyproject_data = pytoml.load(f)
59+
# Ensure the mandatory data can be loaded
60+
buildsys = pyproject_data['build-system']
61+
requires = buildsys['requires']
62+
backend = buildsys['build-backend']
63+
64+
hooks = Pep517HookCaller(source_dir, backend)
65+
66+
with BuildEnvironment() as env:
67+
env.pip_install(requires)
68+
_prep_meta(hooks, env, dest)
69+
70+
71+
parser = argparse.ArgumentParser()
72+
parser.add_argument(
73+
'source_dir',
74+
help="A directory containing pyproject.toml",
75+
)
76+
parser.add_argument(
77+
'--out-dir', '-o',
78+
help="Destination in which to save the builds relative to source dir",
79+
)
80+
81+
82+
def main(args):
83+
build_meta(args.source_dir, args.out_dir)
84+
85+
86+
if __name__ == '__main__':
87+
main(parser.parse_args())

0 commit comments

Comments
 (0)