diff --git a/.gitignore b/.gitignore index b00d8301f9081d40b86b5659c2bb3891edaeb4fd..f066e9c43206aa7ab0367d80d7f2c2f9e02a6f94 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ __pycache__/ build/ .cache/ -.venv/ \ No newline at end of file +.venv/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e2124a3631f71c666c857928b801bf494dfc6319..1bffa63ecd832f5339de48fbd0c2d29ab911cdf9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,31 @@ repos: + - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: check-added-large-files + - id: check-case-conflict + - id: check-merge-conflict + - id: check-symlinks + - id: check-yaml + - id: debug-statements + - id: end-of-file-fixer + - id: mixed-line-ending + - id: requirements-txt-fixer + - id: trailing-whitespace + - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/kynan/nbstripout rev: 0.8.0 hooks: - id: nbstripout files: \.ipynb$ + + - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/pre-commit/mirrors-clang-format + rev: v19.1.3 + hooks: + - id: clang-format + files: \.(cpp|hpp|c|h)$ + + - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/psf/black + rev: 24.10.0 # Use the latest stable version + hooks: + - id: black diff --git a/.vscode/settings.json b/.vscode/settings.json index 8657f3f95ab30753a21df63de0b5731eb86be25f..97453c13456ff049eabc31b05b49c3ffea754004 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,10 @@ }, "editor.defaultFormatter": "ms-python.black-formatter" }, + "[cpp]": { + "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" + }, + "editor.formatOnSave": true, "isort.args": ["--profile", "black"], "notebook.lineNumbers": "on" } diff --git a/CMakeLists.txt b/CMakeLists.txt index c3ed9abdbcca266b03d9961b3550b6c230091dbd..56217e41b645623b1cc13c47f9652a17845db306 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,4 @@ project("${SKBUILD_PROJECT_NAME}" # include(GNUInstallDirs) add_subdirectory(cpp) -add_subdirectory(python/myproj/simulation/_csrc) \ No newline at end of file +add_subdirectory(python/myproj/simulation/_csrc) diff --git a/README.md b/README.md index e259da8f83f15dde379f87bcdc48a88e9d9d2aa9..ad6f472695a53aa14da49e75b17f98a69421f94c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ use scikit-build-core to setup a python-cmake project that can be installed with ## Conclusion - package name has nothing to do with the import path. pacakge named my_package is installed with `pip install .`, uninstalled with `pip uninstall my_package`, but can be used as `import kk.ss as ks`. -- installation of cpp files: +- installation of cpp files: ```cmake install(TARGET mylib LIBRARY DESTINATION .) ``` @@ -47,7 +47,7 @@ wheel.exclude = ["CMakeLists.txt","*.cc","*.h"] wheel. ## style and lint check -### notebook otuput stripping +### notebook output stripping - use pre-commit 1. install ```bash @@ -56,16 +56,93 @@ pip install pre-commit nbstripout 2. set ```yaml repos: - - repo: https://github.com/kynan/nbstripout - rev: master # Pin to a specific version if desired + - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/kynan/nbstripout + rev: 0.8.0 hooks: - id: nbstripout files: \.ipynb$ ``` -3. install +3. install ```bash pre-commit install ``` +4. run and verify +```bash +pre-commit run --all-files +``` +or +```bash +nox -s pre_commit +``` + +### c++ formating +- pre-commit + vscode extensions +- install extensions +``` +Name: clangd +Id: llvm-vs-code-extensions.vscode-clangd +Description: C/C++ completion, navigation, and insights +Version: 0.1.30 +Publisher: LLVM +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd +``` +and make sure this is in `settings.json`: +```json + "[cpp]": { + "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" + } +``` +- install pre-commit hooks +Name: Black Formatter +Id: ms-python.black-formatter +Description: Formatting support for Python files using the Black formatter. +Version: 2024.4.0 +Publisher: Microsoft +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter +```yaml +- repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/pre-commit/mirrors-clang-format + rev: v19.1.3 + hooks: + - id: clang-format + files: \.(cpp|hpp|c|h)$ +``` + +### python formatting +- pre-commit + vscode extensions +- install vscode extensions +- install extensions +``` +Name: Black Formatter +Id: ms-python.black-formatter +Description: Formatting support for Python files using the Black formatter. +Version: 2024.4.0 +Publisher: Microsoft +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter +``` +and update `settings.json` +```json + "[python]": { + "editor.codeActionsOnSave": { + "source.organizeImports": "always" + }, + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "isort.args": ["--profile", "black"], +``` +- install pre-commit hooks +``` +repos: + - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/psf/black + rev: 24.10.0 # Use the latest stable version + hooks: + - id: black +``` +### c++ lint +- nox + vscode extensions + +### python lint +- ruff: pre-commit + vscode extensions +- pylint: nox + vscode extensions ## vscode extensions to install - "isort", which sort python import diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index add648983d57b42013dc00a6f9db0e9b7790d4bf..2dedfcf75cddbecc50147ec50baa0da3c914de44 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -21,4 +21,4 @@ set_target_properties(complex PROPERTIES # install( # EXPORT helloTargets # NAMESPACE hello:: -# DESTINATION ${HELLO_CMAKE_PACKAGE_INSTALL_SUBDIR}) \ No newline at end of file +# DESTINATION ${HELLO_CMAKE_PACKAGE_INSTALL_SUBDIR}) diff --git a/cpp/include/complex.h b/cpp/include/complex.h index 72d4d9a91d377ca7c1fb6c7263863840b7d4e129..0ebcf62ea2790d3b720bf6e7fc668799607d0cc5 100644 --- a/cpp/include/complex.h +++ b/cpp/include/complex.h @@ -1,3 +1,3 @@ #pragma once -double complex(double a, double b); \ No newline at end of file +double complex(double a, double b); diff --git a/cpp/include/hello.h b/cpp/include/hello.h index 4c8c54152768fdfcfd281d824209f21f423474bd..9c9beb9ab4a7cdf5f0b7bc45edd7d08f427936a1 100644 --- a/cpp/include/hello.h +++ b/cpp/include/hello.h @@ -1,3 +1,3 @@ #pragma once -double add(double a, double b); \ No newline at end of file +double add(double a, double b); diff --git a/cpp/src/complex.cc b/cpp/src/complex.cc index 429979813c208bb057f9e834aa0bd26d2df7b7c3..3114e15ef1e89e7ecc158bf197e50e46d54b4692 100644 --- a/cpp/src/complex.cc +++ b/cpp/src/complex.cc @@ -1,3 +1,4 @@ #include "complex.h" #include "hello.h" -double complex(double a, double b) { return add(a, b) + 2; } \ No newline at end of file + +double complex(double a, double b) { return add(a, b) + 2; } diff --git a/cpp/src/hello.cc b/cpp/src/hello.cc index 80e43e2e3ea685d329aba8a1fa5365dd0c2f683d..6f4948a6805d48b462132921cef4570ad2f4be27 100644 --- a/cpp/src/hello.cc +++ b/cpp/src/hello.cc @@ -1,3 +1,3 @@ #include "hello.h" -double add(double a, double b) { return a + b; } \ No newline at end of file +double add(double a, double b) { return a + b; } diff --git a/noxfile.py b/noxfile.py index 5fe405733c60774a9bd3c2e286e17fb331a1b2e6..a611171b1034f8f8260e00c73e3cf37c24661052 100644 --- a/noxfile.py +++ b/noxfile.py @@ -10,6 +10,15 @@ import nox DIR = Path(__file__).parent.resolve() +@nox.session(venv_backend="none") +def pre_commit(session: nox.Session) -> None: + """ + Run pre-commit + """ + if "pre_commit" in sys.argv: + session.run("pre-commit", "run", "--all-files") + + @nox.session(venv_backend="none") def dev(session: nox.Session) -> None: """ diff --git a/pyproject.toml b/pyproject.toml index 59c184db26452d99007703ab7e4dd8723cbb87c3..8ed0c30f5e8e97227a5f6ea609b1b511b73fbcd4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,4 +63,4 @@ disable = [ "C0114", # Missing module docstring "C0115", # Missing class docstring "C0116" # Missing function or method docstring -] \ No newline at end of file +] diff --git a/python/myproj/simulation/_csrc/CMakeLists.txt b/python/myproj/simulation/_csrc/CMakeLists.txt index d5baf7b55a5fb5d13f64ff256251fc6bd17c9681..3103c4af7c3ca884a74c9b7d7b77d420dede3042 100644 --- a/python/myproj/simulation/_csrc/CMakeLists.txt +++ b/python/myproj/simulation/_csrc/CMakeLists.txt @@ -6,4 +6,4 @@ target_link_libraries(_hello PRIVATE hello complex) install(TARGETS _hello DESTINATION _csrc) set_target_properties(_hello PROPERTIES INSTALL_RPATH "$ORIGIN/.." -) \ No newline at end of file +) diff --git a/python/myproj/simulation/_csrc/_hello_python.cc b/python/myproj/simulation/_csrc/_hello_python.cc index feaeae70c066eb587db75657d8f0757971975a5a..21078d9b3628fbf7fbb03fdc184a098b925648d2 100644 --- a/python/myproj/simulation/_csrc/_hello_python.cc +++ b/python/myproj/simulation/_csrc/_hello_python.cc @@ -9,4 +9,4 @@ PYBIND11_MODULE(_hello, m) { m.def("add", &add, "A function which adds two numbers"); m.def("complex", &complex, "complex(a,b) = add(a,b) + 2"); -} \ No newline at end of file +}