Skip to content
Snippets Groups Projects
Commit 64864bbd authored by Klaus Reuter's avatar Klaus Reuter
Browse files

add --directory flag to enable operation from arbitrary working directories

parent f5245384
No related branches found
No related tags found
No related merge requests found
...@@ -201,7 +201,7 @@ def compress_environment(cfg): ...@@ -201,7 +201,7 @@ def compress_environment(cfg):
assert(proc.returncode == 0) assert(proc.returncode == 0)
def run_cmd(args): def run_cmd(args, cwd):
"""Run command in a sub-process, where PATH is prepended with the 'bin' directory of the 'condainer' environment in the container. """Run command in a sub-process, where PATH is prepended with the 'bin' directory of the 'condainer' environment in the container.
""" """
cfg = get_cfg() cfg = get_cfg()
...@@ -209,7 +209,7 @@ def run_cmd(args): ...@@ -209,7 +209,7 @@ def run_cmd(args):
bin_directory = os.path.join(env_directory, 'envs', 'condainer', 'bin') bin_directory = os.path.join(env_directory, 'envs', 'condainer', 'bin')
env = copy.deepcopy(os.environ) env = copy.deepcopy(os.environ)
env['PATH'] = bin_directory + ':' + env['PATH'] env['PATH'] = bin_directory + ':' + env['PATH']
proc = subprocess.Popen(args.command, env=env, shell=False) proc = subprocess.Popen(args.command, cwd=cwd, env=env, shell=False)
proc.communicate() proc.communicate()
...@@ -328,7 +328,7 @@ def umount(args): ...@@ -328,7 +328,7 @@ def umount(args):
print("hint: condainer not mounted") print("hint: condainer not mounted")
def exec(args): def exec(args, cwd):
"""Run command within container, set quiet mode for minimal inference with the command output. """Run command within container, set quiet mode for minimal inference with the command output.
""" """
cfg = get_cfg() cfg = get_cfg()
...@@ -339,7 +339,7 @@ def exec(args): ...@@ -339,7 +339,7 @@ def exec(args):
mount_required = not is_mounted(cfg) mount_required = not is_mounted(cfg)
if mount_required: if mount_required:
mount(args) mount(args)
run_cmd(args) run_cmd(args, cwd)
if mount_required: if mount_required:
umount(args) umount(args)
finally: finally:
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Argument handling and calling of the functions implemented in condainer.py Argument handling and calling of the functions implemented in condainer.py
""" """
import os
import sys import sys
import argparse import argparse
from . import condainer from . import condainer
...@@ -10,12 +11,13 @@ def get_args(): ...@@ -10,12 +11,13 @@ def get_args():
"""Handle command line arguments, return args. """Handle command line arguments, return args.
""" """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
prog=sys.argv[0], prog=sys.argv[0],
description='Create and manage conda environments based on compressed squashfs images.', description='Create and manage conda environments based on compressed squashfs images.',
epilog='More information at https://gitlab.mpcdf.mpg.de/khr/condainer\n\n"Do not set Anacondas free, put them into containers!"' epilog='More information at https://gitlab.mpcdf.mpg.de/khr/condainer'
) )
parser.add_argument('-q', '--quiet', action='store_true', help='be quiet, do not write to stdout unless an error occurs') parser.add_argument('-q', '--quiet', action='store_true', help='be quiet, do not write to stdout unless an error occurs')
parser.add_argument('-d', '--directory', help='condainer project directory, the default is the current working directory')
subparsers = parser.add_subparsers(dest='subcommand', required=True) subparsers = parser.add_subparsers(dest='subcommand', required=True)
subparsers.add_parser('init', help='initialize directory with config files') subparsers.add_parser('init', help='initialize directory with config files')
...@@ -23,6 +25,7 @@ def get_args(): ...@@ -23,6 +25,7 @@ def get_args():
parser_exec = subparsers.add_parser('exec', help='execute command within containerized conda environment') parser_exec = subparsers.add_parser('exec', help='execute command within containerized conda environment')
parser_exec.add_argument('command', type=str, nargs='+', help='command line of the containerized command') parser_exec.add_argument('command', type=str, nargs='+', help='command line of the containerized command')
subparsers.add_parser('mount', help='mount containerized conda environment') subparsers.add_parser('mount', help='mount containerized conda environment')
subparsers.add_parser('umount', help='unmount ("eject") containerized conda environment') subparsers.add_parser('umount', help='unmount ("eject") containerized conda environment')
subparsers.add_parser('prereq', help='check if the necessary tools are installed') subparsers.add_parser('prereq', help='check if the necessary tools are installed')
...@@ -37,6 +40,9 @@ def cli(): ...@@ -37,6 +40,9 @@ def cli():
"""Entry point function to call `condainer` from the command line. """Entry point function to call `condainer` from the command line.
""" """
args = get_args() args = get_args()
cwd = os.getcwd()
if args.directory:
os.chdir(args.directory)
if args.subcommand == 'init': if args.subcommand == 'init':
condainer.init(args) condainer.init(args)
elif args.subcommand == 'build': elif args.subcommand == 'build':
...@@ -48,7 +54,7 @@ def cli(): ...@@ -48,7 +54,7 @@ def cli():
elif args.subcommand == 'prereq': elif args.subcommand == 'prereq':
condainer.prereq(args) condainer.prereq(args)
elif args.subcommand == 'exec': elif args.subcommand == 'exec':
condainer.exec(args) condainer.exec(args, cwd)
elif args.subcommand == 'test': elif args.subcommand == 'test':
condainer.test(args) condainer.test(args)
elif args.subcommand == 'status': elif args.subcommand == 'status':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment