|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import shutil |
| 5 | + |
| 6 | + |
| 7 | +ROOT_DIR = pathlib.Path(os.path.abspath(__file__)).parents[1] |
| 8 | +LECTURE_DIR = os.path.join(ROOT_DIR, 'lectures') |
| 9 | + |
| 10 | + |
| 11 | +def check_unused_imports(py_files_dir): |
| 12 | + # Use flake8 to check for unused imports (F401) |
| 13 | + ret = subprocess.run(['flake8', '--select', 'F401', py_files_dir], |
| 14 | + stdout=subprocess.PIPE) |
| 15 | + if ret.returncode != 0: |
| 16 | + print(ret.stdout.decode('utf-8')) |
| 17 | + raise Exception("Please remove the unused imports") |
| 18 | + print("Passed: Code Quality tests") |
| 19 | + |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + test_dir = os.path.join(ROOT_DIR, '__test_code_quality_tmp') |
| 23 | + if not os.path.exists(test_dir): |
| 24 | + os.makedirs(test_dir) |
| 25 | + |
| 26 | + for file_name in os.listdir(LECTURE_DIR): |
| 27 | + if file_name.endswith(".md"): |
| 28 | + base_file_name = pathlib.Path(file_name).stem |
| 29 | + cmd = ["jupytext", "--to", "py", |
| 30 | + f"lectures/{file_name}", "-o", |
| 31 | + f"{test_dir}/{base_file_name}.py"] |
| 32 | + ret = subprocess.run(cmd, stdout=subprocess.PIPE) |
| 33 | + if ret.returncode != 0: |
| 34 | + shutil.rmtree(test_dir) |
| 35 | + print(ret.stdout.decode('utf-8')) |
| 36 | + raise Exception(f"jupytext failed for file {file_name}") |
| 37 | + |
| 38 | + try: |
| 39 | + check_unused_imports(test_dir) |
| 40 | + finally: |
| 41 | + shutil.rmtree(test_dir) |
0 commit comments