<commit_before>#! /usr/bin/python3
# -*- coding: utf-8 -*-

import argparse
import os
import pwd
import subprocess
import sys

from config import *


def str_to_bool(s):
    """Convert str to bool."""
    s = s.lower()
    if s not in {'true', 'false', '1', '0'}:
        raise ValueError('Bool expected, but got %r' % s)
    return s in {'true', '1'}


def parse_args():
    parser = argparse.ArgumentParser(
        description='allows restricted access to docker for users',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument(
        "--verbose",
        help="prints the invoked docker commandline",
        action="store",
        default=True,
        type=str_to_bool,
    )

    parser.add_argument(
        "--dry-run",
        help="doesn't actually invoke the docker command",
        action="store",
        default=False,
        type=str_to_bool,
    )

    parser.add_argument(
        "--executor",
        help="prints the invoked docker commandline",
        action="store",
        default=DEFAULT_EXECUTOR,
        type=str,
        choices=EXECUTORS,
    )

    parser.add_argument(
        "image",
        help="the image to run",
        action="store",
        type=str,
    )

    parser.add_argument(
        "image_args",
        help="arguments passed to the image",
        action="store",
        type=str,
        nargs=argparse.REMAINDER
    )

    return parser.parse_args()


def build_docker_commandline(args):
    uid = os.getuid()
    uid = int(os.getenv('SUDO_UID', uid))
    gid = os.getgid()
    gid = int(os.getenv('SUDO_GID', gid))
    # user_name = pwd.getpwuid(uid)[0]
    user_home = pwd.getpwuid(uid)[5]

    cmd = [args.executor]
    cmd += ARGS

    for volume_mount in VOLUME_MOUNTS:
        cmd += ['-v', volume_mount]

    if MOUNT_USER_HOME:
        cmd += ['-v', '%s:%s' % (user_home, user_home)]

    for env_var in ENV_VARS + ENV_VARS_EXT.get(args.executor, []):
        cmd += ['-e', env_var]

    if USER_IN_CONTAINER:
        cmd += ['-u', '%d:%d' % (uid, gid)]

    cmd.append('--')
    cmd.append(args.image)
    cmd.extend(args.image_args)

    if args.verbose:
        print(cmd)
    return cmd


def main():
    args = parse_args()
    cmd = build_docker_commandline(args)

    if not args.dry_run:
        sys.exit(subprocess.check_call(
            cmd,
        ))


if __name__ == '__main__':
    main()<commit_msg>changed --verbose to --quiet and parsing changed to store_true<commit_after>#! /usr/bin/python3
# -*- coding: utf-8 -*-

import argparse
import os
import pwd
import subprocess
import sys

from config import *


def parse_args():
    parser = argparse.ArgumentParser(
        description='allows restricted access to docker for users',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument(
        "--quiet",
        help="silences output of invoked docker command",
        action="store_true",
    )

    parser.add_argument(
        "--dry-run",
        help="doesn't actually invoke the docker command",
        action="store_true",
    )

    parser.add_argument(
        "--executor",
        help="prints the invoked docker commandline",
        action="store",
        default=DEFAULT_EXECUTOR,
        type=str,
        choices=EXECUTORS,
    )

    parser.add_argument(
        "image",
        help="the image to run",
        action="store",
        type=str,
    )

    parser.add_argument(
        "image_args",
        help="arguments passed to the image",
        action="store",
        type=str,
        nargs=argparse.REMAINDER
    )

    return parser.parse_args()


def build_docker_commandline(args):
    uid = os.getuid()
    uid = int(os.getenv('SUDO_UID', uid))
    gid = os.getgid()
    gid = int(os.getenv('SUDO_GID', gid))
    # user_name = pwd.getpwuid(uid)[0]
    user_home = pwd.getpwuid(uid)[5]

    cmd = [args.executor]
    cmd += ARGS

    for volume_mount in VOLUME_MOUNTS:
        cmd += ['-v', volume_mount]

    if MOUNT_USER_HOME:
        cmd += ['-v', '%s:%s' % (user_home, user_home)]

    for env_var in ENV_VARS + ENV_VARS_EXT.get(args.executor, []):
        cmd += ['-e', env_var]

    if USER_IN_CONTAINER:
        cmd += ['-u', '%d:%d' % (uid, gid)]

    cmd.append('--')
    cmd.append(args.image)
    cmd.extend(args.image_args)

    return cmd


def main():
    args = parse_args()
    cmd = build_docker_commandline(args)

    if not args.quiet:
        print(cmd)

    if not args.dry_run:
        sys.exit(subprocess.check_call(
            cmd,
        ))


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.13255360623781676, 'fraction_numerical': 0.0023824994585228505, 'mean_word_length': 2.322302158273381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3916837', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1516, 'n_words': 339}