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

[DRAFT] builds from Dockerfile if image not present #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ scratch/

# integration testing scripts using unpublishable dicoms
integration_testing.sh

petdeface/pyproject.toml
petdeface/Dockerfile
petdeface/README.md
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
40 changes: 33 additions & 7 deletions petdeface/petdeface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import pathlib
import re
import shutil
import tempfile

# import shutil
import subprocess
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ authors = ["Martin Nørgaard <martin.noergaard@nru.dk>", "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
Expand Down