Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update developer install to modern package toml #250

Merged
merged 15 commits into from
Oct 5, 2024
2 changes: 1 addition & 1 deletion .github/workflows/conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install dependencies
run: conda install -y python conda-build "grpcio-tools<1.62" conda-verify
- name: Compiling the prototypes
run: python python-libraries/nanover-core/setup.py compile_proto --proto-dir=protocol
run: python ./python-libraries/compile_proto.py --proto-dir=./protocol --python-dir=./python-libraries/nanover-core/src
- name: Building the Conda packages
run: |
$build_command \
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ jobs:
run: conda install -y mpi4py openmm
- name: Install tests dependancies
run: python -m pip install -r python-libraries/requirements.test
- name: Compile
run: ./compile.sh --no-dotnet
- name: Compile (mypy requires --no-edit)
run: ./compile.sh --no-dotnet --no-edit
- name: mypy
run: |
# Mypy accepts paths, modules or packages as inputs. However, only
Expand Down
4 changes: 2 additions & 2 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ if [[ $with_python == true ]]; then
python -m pip install -r ./python-libraries/requirements.test ${user_option}

announce "Compiling proto files to python"
python ./python-libraries/nanover-core/setup.py compile_proto
python ./python-libraries/compile_proto.py --proto-dir=./protocol --python-dir=./python-libraries/nanover-core/src

announce "Installing the python packages"
python -m pip install ${edit_option} ${nanover_user_option} ./python-libraries/nanover-core/

for package in python-libraries/nanover-*/; do
if [[ -f "${package}/setup.py" ]]; then
if [[ -f "${package}/pyproject.toml" ]]; then
python -m pip install ${edit_option} ${nanover_user_option} ${package}
fi
done
Expand Down
130 changes: 130 additions & 0 deletions python-libraries/compile_proto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import argparse
import os
import textwrap
from contextlib import contextmanager
from importlib import resources
from pathlib import Path


def handle_user_arguments(args=None) -> argparse.Namespace:
"""
Parse the arguments from the command line.

:return: The namespace of arguments read from the command line.
"""
description = textwrap.dedent(
"""\
Compile the gRPC protocol files to python.
"""
)
parser = argparse.ArgumentParser(description=description)

parser.add_argument(
"--proto-dir",
required=True,
dest="proto_dir",
metavar="PATH",
help="The path to the directory containing the proto files",
)

parser.add_argument(
"--python-dir",
required=True,
dest="python_dir",
metavar="PATH",
help="The path to the directory where to generate the python files",
)

arguments = parser.parse_args(args)
return arguments


def compile_protocol(proto_dir, python_dir):
"""
Compile the protocol files to python.

:param proto_dir: The path to the directory containing the proto files.
:param python_dir: The path to the directory where to generate the python files.
"""
from grpc_tools import protoc

try:
os.makedirs(python_dir)
except OSError:
pass

# Note on calling grpc_tools.protoc as a python function:
# grpc_tools.protoc.main is called by the command line with sys.argv and
# the include path for the default protobuf proto files. sys.argv is a list of
# the arguments passed to the command line, the first element of that list is
# the command itself; what is passed as a command does not matter, but the actual
# arguments must start at sys.argv[1] (hence "protoc" as first argument passed
# to the function).
proto_include = resources.files("grpc_tools") / "_proto"
with move_in_directory(proto_dir):
for protocol_file in Path(".").glob("**/*.proto"):
print(f"Compiling {protocol_file}")
protoc.main(
(
"protoc",
"--proto_path=.",
f"--python_out={python_dir}",
f"--grpc_python_out={python_dir}",
str(protocol_file),
f"--proto_path={proto_include}",
)
)
generated_protocol_directories = (
path for path in (python_dir / "nanover/protocol").glob("**/*") if path.is_dir()
)
for directory in generated_protocol_directories:
(directory / "__init__.py").touch()
contained_files = (file for file in directory.glob("*_pb2*.py"))
with open(directory / "__init__.py", "w+") as init_py:
for contained_file in contained_files:
file_name = os.path.splitext(os.path.split(contained_file)[1])[0]
init_py.write("from .%s import *\n" % file_name)


@contextmanager
def move_in_directory(destination):
"""
Context manager that moves in the given directory.

When the interpreter enters the context manager, the working directory becomes
the given destination. When the interpreter exists the context manager, the
working directory is restored to where the working directory was before entering.

Example:
========

>>> with move_in_directory("bar"):
>>> # working directory is "bar"
>>> pass
>>> # working directory is "foo" again

:param destination: The directory to use as working directory.
"""
destination = Path(destination)
directory_to_restore = Path.cwd()
try:
os.chdir(str(destination))
yield
finally:
os.chdir(str(directory_to_restore))


def main():
"""
Entry point for the command line.
"""
arguments = handle_user_arguments()

compile_protocol(
Path(arguments.proto_dir).resolve(),
Path(arguments.python_dir).resolve(),
)


if __name__ == "__main__":
main()
24 changes: 24 additions & 0 deletions python-libraries/nanover-ase/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "nanover-ase"
authors = [
{name = "Intangible Realities Lab"},
]
description = "ASE integration for NanoVer"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"nanover",
"ase >= 3.21",
]
dynamic = ["version"]

[project.scripts]
nanover-omm-ase = "nanover.ase.openmm.cli:main"

[tool.setuptools.packages.find]
where = ["src"] # list of folders that contain the packages (["."] by default)
include = ["nanover.*"] # package names should match these glob patterns (["*"] by default)
23 changes: 0 additions & 23 deletions python-libraries/nanover-ase/setup.py

This file was deleted.

2 changes: 0 additions & 2 deletions python-libraries/nanover-core/conda/bld.bat

This file was deleted.

29 changes: 29 additions & 0 deletions python-libraries/nanover-core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "nanover"
authors = [
{name = "Intangible Realities Lab"},
]
description = "NanoVer python framework"
requires-python = ">=3.11"
dependencies = [
"setuptools>=40.1.1",
"grpcio>=1.63.0",
"grpcio-tools>=1.63.0",
"pytest",
"aiogrpc",
"numpy",
"hypothesis",
"typing_extensions",
]
dynamic = ["version"]

[project.scripts]
nanover-multiplayer = "nanover.multiplayer.cli:main"

[tool.setuptools.packages.find]
where = ["src"] # list of folders that contain the packages (["."] by default)
include = ["nanover.*", "nanover.protocol"] # package names should match these glob patterns (["*"] by default)
Loading
Loading