diff --git a/.gitignore b/.gitignore index 4916211..99cd144 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,7 @@ scratch/ # integration testing scripts using unpublishable dicoms integration_testing.sh + +petdeface/pyproject.toml +petdeface/Dockerfile +petdeface/README.md \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..26f75e4 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +# this makefile allows for some prepacking steps and other quality of life fixes +# for this repo + +# make build activates poetry build after copying over dockerfile and pyproject.toml to petdeface dir +build: + cp Dockerfile petdeface/ + cp pyproject.toml petdeface/ + cp README.md petdeface/ + poetry build + +# make publish runs the above make build command, then pushs the most recent build to pypi +publish: build + poetry publish diff --git a/petdeface/petdeface.py b/petdeface/petdeface.py index 7c2b6d7..3952e43 100755 --- a/petdeface/petdeface.py +++ b/petdeface/petdeface.py @@ -3,6 +3,8 @@ import os import pathlib import re +import shutil +import tempfile # import shutil import subprocess @@ -132,12 +134,30 @@ def check_docker_image_exists(image_name, build=False): if build: try: # get dockerfile path - dockerfile_path = pathlib.Path(__file__).parent / pathlib.Path("Dockerfile") - subprocess.run( - ["docker", "build", "-t", image_name, str(dockerfile_path)], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=True, + this_files_folder = pathlib.Path(__file__).parent.resolve() + dockerfile_path = this_files_folder / pathlib.Path("Dockerfile") + + # create a temporary directory to put the package into resembling the structure of the this package + # installed in python site-packages + with tempfile.TemporaryDirectory() as temp_dir: + # copy the package into the temporary directory + shutil.copytree( + this_files_folder, + os.path.join(temp_dir, "petdeface"), + ignore=shutil.ignore_patterns("__pycache__")) + shutil.copy(dockerfile_path, + os.path.join(temp_dir, "Dockerfile")) + shutil.copy(os.path.join(this_files_folder, "pyproject.toml"), + os.path.join(temp_dir, "pyproject.toml")) + shutil.copy(os.path.join(this_files_folder, "README.md"), + os.path.join(temp_dir, "README.md")) + # change the working directory to the temporary directory + os.chdir(temp_dir) + # build the docker image + subprocess.run( + ["docker", "build", '.', "-t", image_name,], + check=True, + cwd=str(temp_dir) ) image_exists = True print("Docker image {} has been built.".format(image_name)) @@ -502,7 +522,13 @@ def main(): # noqa: max-complexity: 12 if args.docker: check_docker_installed() - check_docker_image_exists("petdeface", build=False) + image_exists = check_docker_image_exists(f"petdeface:{__version__}", build=False) + if not image_exists: + built_image = check_docker_image_exists(f"petdeface:{__version__}", build=True) + if not built_image: + #TODO add docker pull + pass + input_mount_point = str(args.input_dir) output_mount_point = str(args.output_dir) diff --git a/pyproject.toml b/pyproject.toml index a43153c..c8fd0af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,8 +6,7 @@ authors = ["Martin Nørgaard ", "Anthony Galassi <28850 license = "MIT" readme = "README.md" include = [ - "petdeface/*", - "pyproject.toml", + "petdeface/*" ] # please update the bids version to the latest compliant version when making modifications to this code here