Rename TemplateProject -> TestProject & tidy some comments

This commit is contained in:
Joe Rickerby
2020-05-17 16:57:11 +01:00
parent 420995f567
commit 81f960b17e
17 changed files with 50 additions and 45 deletions
+2
View File
@@ -0,0 +1,2 @@
from .base import TestProject # noqa
from .c import new_c_project # noqa
+37
View File
@@ -0,0 +1,37 @@
from argparse import ArgumentParser
import importlib
import tempfile
import sys
import subprocess
def main():
parser = ArgumentParser(
prog="python -m test.test_projects",
description='Generate a test project to check it out'
)
parser.add_argument('PROJECT', help='''
Python path to a project object. E.g. test.test_0_basic.basic_project
''')
options = parser.parse_args()
module, _, name = options.PROJECT.rpartition('.')
project = getattr(importlib.import_module(module), name)
project_dir = tempfile.mkdtemp()
project.generate(project_dir)
print('Project generated at', project_dir)
print()
if sys.platform == 'darwin':
subprocess.check_call(['open', '--', project_dir])
elif sys.platform == 'linux2':
subprocess.check_call(['xdg-open', '--', project_dir])
elif sys.platform == 'win32':
subprocess.check_call(['explorer', project_dir])
if __name__ == '__main__':
main()
+39
View File
@@ -0,0 +1,39 @@
import os
import jinja2
from typing import Union, Dict, Any
FilesDict = Dict[str, Union[str, jinja2.Template]]
TemplateContext = Dict[str, Any]
class TestProject:
'''
An object that represents a project that can be built by cibuildwheel.
Can be manipulated in tests by changing `files` and `template_context`.
Write out to the filesystem using `generate`.
'''
files: FilesDict
template_context: TemplateContext
def __init__(self):
self.files = {}
self.template_context = {}
def generate(self, path: str):
for filename, content in self.files.items():
file_path = os.path.join(path, filename)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf8') as f:
if isinstance(content, jinja2.Template):
content = content.render(self.template_context)
f.write(content)
def copy(self):
other = TestProject()
other.files = self.files.copy()
other.template_context = self.template_context.copy()
return other
+100
View File
@@ -0,0 +1,100 @@
import jinja2
from .base import TestProject
SPAM_C_TEMPLATE = r'''
#include <Python.h>
{{ spam_c_top_level_add }}
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
{{ spam_c_function_add | indent(4) }}
return PyLong_FromLong(sts);
}
/* Module initialization */
#if PY_MAJOR_VERSION >= 3
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define MOD_DEF(m, name, doc, methods, module_state_size) \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, name, doc, module_state_size, methods, }; \
m = PyModule_Create(&moduledef);
#define MOD_RETURN(m) return m;
#else
#define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
#define MOD_DEF(m, name, doc, methods, module_state_size) \
m = Py_InitModule3(name, methods, doc);
#define MOD_RETURN(m) return;
#endif
static PyMethodDef module_methods[] = {
{"system", (PyCFunction)spam_system, METH_VARARGS,
"Execute a shell command."},
{NULL} /* Sentinel */
};
MOD_INIT(spam)
{
PyObject* m;
MOD_DEF(m,
"spam",
"Example module",
module_methods,
-1)
MOD_RETURN(m)
}
'''
SETUP_PY_TEMPLATE = r'''
from setuptools import setup, Extension
{{ setup_py_add }}
setup(
ext_modules=[Extension('spam', sources=['spam.c'])],
{{ setup_py_setup_args_add | indent(4) }}
)
'''
SETUP_CFG_TEMPLATE = r'''
[metadata]
name = spam
version = 0.1.0
{{ setup_cfg_add }}
'''
def new_c_project(*, spam_c_top_level_add='', spam_c_function_add='', setup_py_add='',
setup_py_setup_args_add='', setup_cfg_add=''):
project = TestProject()
project.files.update({
'spam.c': jinja2.Template(SPAM_C_TEMPLATE),
'setup.py': jinja2.Template(SETUP_PY_TEMPLATE),
'setup.cfg': jinja2.Template(SETUP_CFG_TEMPLATE),
})
project.template_context.update({
'spam_c_top_level_add': spam_c_top_level_add,
'spam_c_function_add': spam_c_function_add,
'setup_py_add': setup_py_add,
'setup_py_setup_args_add': setup_py_setup_args_add,
'setup_cfg_add': setup_cfg_add,
})
return project