Created
December 6, 2022 14:21
-
-
Save daaniam/72fa565147dd2e5b104022b4a4afe5cf to your computer and use it in GitHub Desktop.
Run script in container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_script_in_container(script_name: str, console_output: bool = False) -> str | None: | |
"""Run script within the devcontainer to gather application metadata.""" | |
# Add Python file suffix if not present | |
if script_name[-3:] != ".py": | |
script_name = f'{script_name}.py' | |
# Command | |
cmd = ['python', f'/workspace/scripts/{script_name}'] | |
try: | |
# Run command in container | |
result = subprocess.run( | |
[ | |
'docker-compose', | |
'-f', | |
f'{PROJECT_ROOT}/.devcontainer/docker-compose.yml', | |
'run', | |
'--rm', | |
'devcontainer', | |
*cmd, | |
], | |
stdout=sys.stdout if console_output is True else subprocess.PIPE, | |
stderr=sys.stderr if console_output is True else subprocess.PIPE, | |
text=True, | |
) | |
result.check_returncode() | |
except subprocess.CalledProcessError as e: | |
print(e.output) | |
else: | |
return str(result.stdout) | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment