diff --git a/condainer/condainer.py b/condainer/condainer.py index 351aa1ba1032a783439603c46a18a0935f6bb113..5827f981c38efec71f576e2e89fb0711ad82d4b3 100644 --- a/condainer/condainer.py +++ b/condainer/condainer.py @@ -201,7 +201,7 @@ def compress_environment(cfg): 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. """ cfg = get_cfg() @@ -209,7 +209,7 @@ def run_cmd(args): bin_directory = os.path.join(env_directory, 'envs', 'condainer', 'bin') env = copy.deepcopy(os.environ) 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() @@ -328,7 +328,7 @@ def umount(args): 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. """ cfg = get_cfg() @@ -339,7 +339,7 @@ def exec(args): mount_required = not is_mounted(cfg) if mount_required: mount(args) - run_cmd(args) + run_cmd(args, cwd) if mount_required: umount(args) finally: diff --git a/condainer/main.py b/condainer/main.py index a06f61fa26232294c6ef60753fbb9e7c42b2e52b..eb73c63c62a61da2f13a4986a79ee02d2ca4407b 100644 --- a/condainer/main.py +++ b/condainer/main.py @@ -2,6 +2,7 @@ Argument handling and calling of the functions implemented in condainer.py """ +import os import sys import argparse from . import condainer @@ -10,12 +11,13 @@ def get_args(): """Handle command line arguments, return args. """ parser = argparse.ArgumentParser( - formatter_class=argparse.RawDescriptionHelpFormatter, prog=sys.argv[0], 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('-d', '--directory', help='condainer project directory, the default is the current working directory') + subparsers = parser.add_subparsers(dest='subcommand', required=True) subparsers.add_parser('init', help='initialize directory with config files') @@ -23,6 +25,7 @@ def get_args(): 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') + subparsers.add_parser('mount', help='mount 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') @@ -37,6 +40,9 @@ def cli(): """Entry point function to call `condainer` from the command line. """ args = get_args() + cwd = os.getcwd() + if args.directory: + os.chdir(args.directory) if args.subcommand == 'init': condainer.init(args) elif args.subcommand == 'build': @@ -48,7 +54,7 @@ def cli(): elif args.subcommand == 'prereq': condainer.prereq(args) elif args.subcommand == 'exec': - condainer.exec(args) + condainer.exec(args, cwd) elif args.subcommand == 'test': condainer.test(args) elif args.subcommand == 'status':