1+ import importlib .resources
12import re
3+ import shutil
24import subprocess
35import time
46from pathlib import Path
810
911from knora .dsplib .models .helpers import BaseError
1012
11- # relative path to "knora/dsplib/docker", to make it accessible when dsp-tools is called from another working directory
12- docker_path = Path ( __file__ ). parent / Path ( "../docker" )
13+ docker_path_of_user = Path . home () / Path ( ". dsp-tools/docker" )
14+ docker_path_of_user . mkdir ( parents = True , exist_ok = True )
1315
1416
1517def start_stack (
@@ -33,6 +35,14 @@ def start_stack(
3335 if enforce_docker_system_prune and suppress_docker_system_prune :
3436 raise BaseError ('The arguments "--prune" and "--no-prune" are mutually exclusive' )
3537
38+ # copy contents of knora/dsplib/docker to ~/.dsp-tools/docker
39+ # rationale to use importlib.resources: https://setuptools.pypa.io/en/latest/userguide/datafiles.html#accessing-data-files-at-runtime
40+ docker_path_of_distribution = importlib .resources .files ("knora" ).joinpath ("dsplib" ).joinpath ("docker" )
41+ for file in docker_path_of_distribution .iterdir ():
42+ dst = docker_path_of_user / file .name
43+ if not dst .is_file ():
44+ shutil .copy (file , dst )
45+
3646 # get sipi.docker-config.lua
3747 commit_of_used_api_version = "3f44354df"
3848 url_prefix = f"https://github.com/dasch-swiss/dsp-api/raw/{ commit_of_used_api_version } /"
@@ -42,11 +52,11 @@ def start_stack(
4252 if not re .search (max_post_size_regex , docker_config_lua_text ):
4353 raise BaseError ("Unable to set max_file_size. Please try again without this flag." )
4454 docker_config_lua_text = re .sub (max_post_size_regex , f"max_post_size = '{ max_file_size } M'" , docker_config_lua_text )
45- with open (docker_path / "sipi.docker-config.lua" , "w" ) as f :
55+ with open (docker_path_of_user / "sipi.docker-config.lua" , "w" ) as f :
4656 f .write (docker_config_lua_text )
4757
4858 # start up the fuseki database
49- completed_process = subprocess .run ("docker compose up db -d" , shell = True , cwd = docker_path )
59+ completed_process = subprocess .run ("docker compose up db -d" , shell = True , cwd = docker_path_of_user )
5060 if not completed_process or completed_process .returncode != 0 :
5161 raise BaseError ("Cannot start the API: Error while executing 'docker compose up db -d'" )
5262
@@ -94,7 +104,7 @@ def start_stack(
94104 raise BaseError (f"Cannot start DSP-API: Error when creating graph '{ graph } '" )
95105
96106 # startup all other components
97- subprocess .run ("docker compose up -d" , shell = True , cwd = docker_path )
107+ subprocess .run ("docker compose up -d" , shell = True , cwd = docker_path_of_user )
98108 print ("DSP-API is now running on http://0.0.0.0:3333/ and DSP-APP on http://0.0.0.0:4200/" )
99109
100110 # docker system prune
@@ -108,11 +118,11 @@ def start_stack(
108118 prune_docker = input ("Allow dsp-tools to execute 'docker system prune'? This is necessary to keep your "
109119 "Docker clean. If you are unsure what that means, just type y and press Enter. [y/n]" )
110120 if prune_docker == "y" :
111- subprocess .run ("docker system prune -f" , shell = True , cwd = docker_path )
121+ subprocess .run ("docker system prune -f" , shell = True , cwd = docker_path_of_user )
112122
113123
114124def stop_stack () -> None :
115125 """
116126 Shut down the Docker containers of your local DSP stack and delete all data that is in it.
117127 """
118- subprocess .run ("docker compose down --volumes" , shell = True , cwd = docker_path )
128+ subprocess .run ("docker compose down --volumes" , shell = True , cwd = docker_path_of_user )
0 commit comments