diff --git a/codes/python/build.py b/codes/python/build.py index 98523f2c0b..cb5a83b29d 100644 --- a/codes/python/build.py +++ b/codes/python/build.py @@ -1,20 +1,28 @@ import glob -import py_compile as pyc +import subprocess if __name__ == "__main__": # find source code files - src_paths = sorted(glob.glob("codes/python/**/*.py")) - num_src = len(src_paths) - num_src_error = 0 + src_paths = sorted(glob.glob("codes/python/chapter_*/*.py")) + errors = [] + error_count = 0 - # compile python code + # run python code for src_path in src_paths: - try: - pyc.compile(src_path, doraise=True) - except pyc.PyCompileError as e: - num_src_error += 1 - print(e) + process = subprocess.Popen( + ["python", src_path], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + # Wait for the process to complete, and get the output and error messages + stdout, stderr = process.communicate() + # Check the exit status + exit_status = process.returncode + if exit_status != 0: + errors.append(stderr) + error_count += 1 - print(f"===== Build Complete =====") - print(f"Total: {num_src}") - print(f"Error: {num_src_error}") + print(f"===== Found exception in {error_count} files =====") + for error in errors: + print(error + "\n")