Skip to content

Commit

Permalink
Pre-commit for python
Browse files Browse the repository at this point in the history
  • Loading branch information
kratman committed Oct 5, 2024
1 parent 24ca486 commit 33c5197
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 57 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Static analysis

on:
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: 'true'
- uses: actions/setup-python@v4
- uses: pre-commit/[email protected]
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.3"
hooks:
- id: ruff
args: [--fix, --show-fixes]
types_or: [python, pyi, jupyter]
- id: ruff-format
types_or: [python, pyi, jupyter]
25 changes: 16 additions & 9 deletions install_KLU_Sundials.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def build_solvers():
SUITESPARSE_CHECKSUM = (
"7111b505c1207f6f4bd0be9740d0b2897e1146b845d73787df07901b4f5c1fb7"
)
SUNDIALS_CHECKSUM = "4e0b998dff292a2617e179609b539b511eb80836f5faacf800e688a886288502"
SUNDIALS_CHECKSUM = (
"4e0b998dff292a2617e179609b539b511eb80836f5faacf800e688a886288502"
)
DEFAULT_INSTALL_DIR = str(pathlib.Path(__file__).parent.resolve() / ".idaklu")

def safe_remove_dir(path):
Expand Down Expand Up @@ -58,19 +60,19 @@ def install_suitesparse(download_dir):
# if in CI, set RPATH to the install directory for SuiteSparse_config
# dylibs to find libomp.dylib when repairing the wheel
if os.environ.get("CIBUILDWHEEL") == "1":
env["CMAKE_OPTIONS"] = (
f"-DCMAKE_INSTALL_PREFIX={install_dir} -DCMAKE_INSTALL_RPATH={install_dir}/lib"
)
env[
"CMAKE_OPTIONS"
] = f"-DCMAKE_INSTALL_PREFIX={install_dir} -DCMAKE_INSTALL_RPATH={install_dir}/lib"
else:
env["CMAKE_OPTIONS"] = f"-DCMAKE_INSTALL_PREFIX={install_dir}"
else:
# For AMD, COLAMD, BTF and KLU; do not set a BUILD RPATH but use an
# INSTALL RPATH in order to ensure that the dynamic libraries are found
# at runtime just once. Otherwise, delocate complains about multiple
# references to the SuiteSparse_config dynamic library (auditwheel does not).
env["CMAKE_OPTIONS"] = (
f"-DCMAKE_INSTALL_PREFIX={install_dir} -DCMAKE_INSTALL_RPATH={install_dir}/lib -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=FALSE -DCMAKE_BUILD_WITH_INSTALL_RPATH=FALSE"
)
env[
"CMAKE_OPTIONS"
] = f"-DCMAKE_INSTALL_PREFIX={install_dir} -DCMAKE_INSTALL_RPATH={install_dir}/lib -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=FALSE -DCMAKE_BUILD_WITH_INSTALL_RPATH=FALSE"
subprocess.run(make_cmd, cwd=build_dir, env=env, shell=True, check=True)
subprocess.run(install_cmd, cwd=build_dir, check=True)

Expand Down Expand Up @@ -106,7 +108,9 @@ def install_sundials(download_dir, install_dir):
OpenMP_C_LIB_NAMES = "omp"
OpenMP_omp_LIBRARY = "/opt/homebrew/opt/libomp/lib/libomp.dylib"
elif platform.processor() == "i386":
OpenMP_C_FLAGS = "-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include"
OpenMP_C_FLAGS = (
"-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include"
)
OpenMP_C_LIB_NAMES = "omp"
OpenMP_omp_LIBRARY = "/usr/local/opt/libomp/lib/libomp.dylib"
else:
Expand Down Expand Up @@ -305,7 +309,9 @@ def parallel_download(urls, download_dir):
"The '--force' option is activated: installation will be forced, ignoring any existing libraries."
)
safe_remove_dir(os.path.join(download_dir, "build_sundials"))
safe_remove_dir(os.path.join(download_dir, f"SuiteSparse-{SUITESPARSE_VERSION}"))
safe_remove_dir(
os.path.join(download_dir, f"SuiteSparse-{SUITESPARSE_VERSION}")
)
safe_remove_dir(os.path.join(download_dir, f"sundials-{SUNDIALS_VERSION}"))
sundials_found, suitesparse_found = False, False
else:
Expand Down Expand Up @@ -334,5 +340,6 @@ def parallel_download(urls, download_dir):
parallel_download([(SUITESPARSE_URL, SUITESPARSE_CHECKSUM)], download_dir)
install_suitesparse(download_dir)


if __name__ == "__main__":
build_solvers()
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def run_pybamm_requires(session):
else:
session.error("nox -s idaklu-requires is only available on Linux & macOS.")


@nox.session(name="unit")
def run_unit(session):
"""Run the unit tests."""
Expand All @@ -131,4 +132,3 @@ def run_unit(session):
silent=False,
)
session.run("pytest", "tests")

59 changes: 59 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Exclude a variety of commonly ignored directories.
exclude = [
"__init__.py",
"docs",
".eggs",
".git",
".mypy_cache",
".pytype",
".ruff_cache",
".venv",
"__pypackages__",
"_build",
"build",
"dist",
"node_modules",
"venv",
]

line-length = 88
indent-width = 4

target-version = "py310"

[lint]
select = [
"W",
"E",
"F",
"B",
"A",
"U",
"SLF",
]
ignore = [
"E501", # Line length check conflicted with the formatter
"B018", # Useless expression checks were a problem for notebooks
"B905", # Requires zip to have explict strict flag. Enable in the future
]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

94 changes: 48 additions & 46 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def move_output(self, ext):

# ---------- end of CMake steps --------------------------------------------------------


class CustomInstall(install):
"""A custom install command to add 2 build options"""

Expand Down Expand Up @@ -231,52 +232,53 @@ def run(self):
orig.bdist_wheel.run(self)


idaklu_ext = Extension(
name="pybammsolvers.idaklu",
# The sources list should mirror the list in CMakeLists.txt
sources=[
"src/pybammsolvers/idaklu_source/Expressions/Expressions.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/Expression.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionSet.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionTypes.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionSparsity.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Casadi/CasadiFunctions.cpp",
"src/pybammsolvers/idaklu_source/Expressions/Casadi/CasadiFunctions.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEBaseFunction.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunction.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunctions.cpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunctions.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/iree_jit.cpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/iree_jit.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/ModuleParser.cpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/ModuleParser.hpp",
"src/pybammsolvers/idaklu_source/idaklu_solver.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolver.cpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolver.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.cpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.inl",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP_solvers.cpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP_solvers.hpp",
"src/pybammsolvers/idaklu_source/sundials_functions.inl",
"src/pybammsolvers/idaklu_source/sundials_functions.hpp",
"src/pybammsolvers/idaklu_source/IdakluJax.cpp",
"src/pybammsolvers/idaklu_source/IdakluJax.hpp",
"src/pybammsolvers/idaklu_source/common.hpp",
"src/pybammsolvers/idaklu_source/common.cpp",
"src/pybammsolvers/idaklu_source/Solution.cpp",
"src/pybammsolvers/idaklu_source/Solution.hpp",
"src/pybammsolvers/idaklu_source/SolutionData.cpp",
"src/pybammsolvers/idaklu_source/SolutionData.hpp",
"src/pybammsolvers/idaklu_source/observe.cpp",
"src/pybammsolvers/idaklu_source/observe.hpp",
"src/pybammsolvers/idaklu_source/Options.hpp",
"src/pybammsolvers/idaklu_source/Options.cpp",
"src/pybammsolvers/idaklu.cpp",
],
)
ext_modules = [idaklu_ext]
ext_modules = [
Extension(
name="pybammsolvers.idaklu",
# The sources list should mirror the list in CMakeLists.txt
sources=[
"src/pybammsolvers/idaklu_source/Expressions/Expressions.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/Expression.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionSet.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionTypes.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionSparsity.hpp",
"src/pybammsolvers/idaklu_source/Expressions/Casadi/CasadiFunctions.cpp",
"src/pybammsolvers/idaklu_source/Expressions/Casadi/CasadiFunctions.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEBaseFunction.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunction.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunctions.cpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunctions.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/iree_jit.cpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/iree_jit.hpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/ModuleParser.cpp",
"src/pybammsolvers/idaklu_source/Expressions/IREE/ModuleParser.hpp",
"src/pybammsolvers/idaklu_source/idaklu_solver.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolver.cpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolver.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.cpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.inl",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.hpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP_solvers.cpp",
"src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP_solvers.hpp",
"src/pybammsolvers/idaklu_source/sundials_functions.inl",
"src/pybammsolvers/idaklu_source/sundials_functions.hpp",
"src/pybammsolvers/idaklu_source/IdakluJax.cpp",
"src/pybammsolvers/idaklu_source/IdakluJax.hpp",
"src/pybammsolvers/idaklu_source/common.hpp",
"src/pybammsolvers/idaklu_source/common.cpp",
"src/pybammsolvers/idaklu_source/Solution.cpp",
"src/pybammsolvers/idaklu_source/Solution.hpp",
"src/pybammsolvers/idaklu_source/SolutionData.cpp",
"src/pybammsolvers/idaklu_source/SolutionData.hpp",
"src/pybammsolvers/idaklu_source/observe.cpp",
"src/pybammsolvers/idaklu_source/observe.hpp",
"src/pybammsolvers/idaklu_source/Options.hpp",
"src/pybammsolvers/idaklu_source/Options.cpp",
"src/pybammsolvers/idaklu.cpp",
],
)
]

# Project metadata was moved to pyproject.toml (which is read by pip). However, custom
# build commands and setuptools extension modules are still defined here.
Expand Down
2 changes: 1 addition & 1 deletion src/pybammsolvers/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__="0.0.1"
__version__ = "0.0.1"

0 comments on commit 33c5197

Please sign in to comment.