Coverage for pyquickhelper/pycode/insetup_helper.py: 71%
38 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1"""
2@file
3@brief Function to use inside setup.py
4"""
5import os
6import sys
8from .. loghelper import run_cmd
11def must_build(argv=None):
12 """
13 Determines if the module must be built before running the command
14 in *argv*.
16 @param argv if None, default to sys.argv
17 @return boolean
19 *built* means calling ``setup.py build_ext --inplace``.
20 """
21 if argv is None:
22 argv = sys.argv # pragma: no cover
23 for k in {'unittests', 'unittests_LONG', 'unittests_SKIP',
24 'unittests_GUI', 'build_sphinx'}:
25 if k in argv:
26 return True
27 return False
30def run_build_ext(setup_file):
31 """
32 Runs ``setup.py build_ext --inplace``.
34 @param setup_file setup_file
35 @return output
36 """
37 exe = sys.executable
38 setup = os.path.normpath(os.path.join(os.path.abspath(
39 os.path.dirname(setup_file)), "setup.py"))
40 cmd = f"{exe} -u {setup} build_ext --inplace"
41 chd = os.path.abspath(os.path.dirname(setup_file))
42 out, err = run_cmd(cmd, wait=True, change_path=chd)
43 err0 = _filter_out_warning(err)
44 if len(err0) > 0:
45 mes0 = "\n".join("### " + _ for _ in err.split("\n"))
46 mes = "Unable to run '{2}'\nin '{3}'\nCMD: '{0}'\n[pyqerror]\n{1}".format(
47 cmd, mes0, setup_file, chd)
48 raise RuntimeError(mes)
49 return out
52def _filter_out_warning(out):
53 """
54 Filters out (import) warnings from error.
56 @param out string
57 @return filtered string
58 """
59 lines = out.split("\n")
60 new_lines = []
61 skip = False
62 for line in lines:
63 if len(line) == 0:
64 skip = True
65 elif line[0] != " ":
66 skip = "ImportWarning" in line or "warning D9002: option '-std=c++11'" in line
67 skip = skip or "RuntimeWarning: Config variable 'Py_DEBUG'" in line
68 skip = skip or "RuntimeWarning: Config variable 'WITH_PYMALLOC'" in line
69 skip = skip or "UserWarning: Unbuilt egg for Unknown" in line
70 skip = skip or "pkg_resources.working_set.add" in line
71 for mod in ['pyquickhelper', 'nbconvert', 'six']:
72 skip = skip or "UserWarning: Module {} was already imported".format(
73 mod) in line
74 if not skip:
75 new_lines.append(line) # pragma: no cover
76 return "\n".join(new_lines)